1 条题解
-
25-何承哲 (938936) LV 9 MOD @ 2020-03-10 22:59:24
此处摘录做得较好同学的答案
@林泽鑫//林泽鑫201930381454 #include<iostream> using namespace std; //RationalNumber.h #ifndef RATIONALNUMBER_H #define RATIONALNUMBER_H class RationalNumber { friend istream& operator>>(istream&, RationalNumber&); friend ostream& operator<<(ostream&, const RationalNumber&); friend int gcd(int, int); public: RationalNumber(int, int); RationalNumber operator+(RationalNumber&); RationalNumber operator-(RationalNumber&); RationalNumber operator*(RationalNumber&); RationalNumber operator/(RationalNumber&); bool operator>(RationalNumber&); bool operator>=(RationalNumber&); bool operator<(RationalNumber&); bool operator<=(RationalNumber&); bool operator==(RationalNumber&); bool operator!=(RationalNumber&); bool check(); private: int numerator; int denominator; bool isError; }; #endif // !RATIONALNUMBER_H //RationalNumber.cpp int gcd(int num1, int num2) { if (num2 == 0)return num1; return gcd(num2, num1 % num2); } RationalNumber::RationalNumber(int a = 0, int b = 1) { if (b == 0) { cout << "ERROR\n"; isError = true; } else if (a == 0) { denominator = 1; numerator = 0; isError = false; } else { numerator = a / gcd(a, b); denominator = b / gcd(a, b); isError = false; } } ostream& operator<<(ostream& output, const RationalNumber& a) { if (a.denominator == 1)output << a.numerator; else output << a.numerator << "/" << a.denominator; return output; } istream& operator>>(istream& input, RationalNumber& b) { int temp1, temp2; input >> temp1 >> temp2; RationalNumber temp(temp1,temp2); b = temp; return input; } RationalNumber RationalNumber::operator+(RationalNumber& operand) { RationalNumber ret(numerator*operand.denominator+denominator*operand.numerator ,denominator*operand.denominator); return ret; } RationalNumber RationalNumber::operator-(RationalNumber& operand) { RationalNumber ret(numerator * operand.denominator - denominator * operand.numerator , denominator * operand.denominator); return ret; } RationalNumber RationalNumber::operator*(RationalNumber& operand) { RationalNumber ret(numerator * operand.numerator, denominator * operand.denominator); return ret; } RationalNumber RationalNumber::operator/(RationalNumber& operand) { RationalNumber ret(numerator * operand.denominator, denominator * operand.numerator); return ret; } bool RationalNumber::operator<(RationalNumber& operand) { return numerator * operand.denominator < denominator * operand.numerator; } bool RationalNumber::operator<=(RationalNumber& operand) { return numerator * operand.denominator <= denominator * operand.numerator; } bool RationalNumber::operator>(RationalNumber& operand) { return numerator * operand.denominator > denominator * operand.numerator; } bool RationalNumber::operator>=(RationalNumber& operand) { return numerator * operand.denominator >= denominator * operand.numerator; } bool RationalNumber::operator==(RationalNumber& operand) { return numerator * operand.denominator == denominator * operand.numerator; } bool RationalNumber::operator!=(RationalNumber& operand) { return numerator * operand.denominator != denominator * operand.numerator; } bool RationalNumber::check() { return isError; } //test.cpp int main() { RationalNumber a, b; cin >> a >> b; if (!a.check() && !b.check()) { cout << a + b << endl; cout << a - b << endl; cout << a * b << endl; } }
- 1
信息
- ID
- 1003
- 难度
- 2
- 分类
- (无)
- 标签
- 递交数
- 154
- 已通过
- 28
- 通过率
- 18%
- 上传者