// Τ瞶计摸 #include #include #include // 捌祘Α "int isdigit(char c);" 浪琩把计 "c" 琌计じ using namespace std; class Rational { friend ostream &operator<<(ostream&, const Rational&); // Τ瞶计块ねㄧΑ public: Rational(const int = 0, const int = 1); // Τ瞶计ぇ﹍砞﹚篶ㄧΑ Rational(const Rational&); // Τ瞶计ぇ狡籹篶ㄧ计 Rational(const short &); // Τ瞶计ぇ祏俱计篶ㄧ计 Rational(const double &); // Τ瞶计ぇ疊翴计篶ㄧ计 Rational(const char &, const char &); // Τ瞶计ぇ蛮じ篶ㄧ计 Rational operator-() const; // Τ瞶计じ笲衡搭猭 Rational operator+(const Rational &) const; // Τ瞶计じ笲衡猭 Rational operator-(const Rational &) const; // Τ瞶计じ笲衡搭猭 Rational operator*(const Rational &) const; // Τ瞶计じ笲衡猭 Rational operator/(const Rational &) const; // Τ瞶计じ笲衡埃猭 private: int nume; // numerator だ int deno; // denominator だダ void setRational(const int, const int); // 砞﹚Τ瞶计ン穝 int gcd(int, int); // GCD 程そ计 Rational &simplify(); // Τ瞶计て虏 }; // Τ瞶计﹍砞﹚篶ㄧΑa 琌だb 琌だダ Rational::Rational(const int a, const int b) { setRational(a, b); } // Τ瞶计ぇ狡籹篶ㄧ计 Rational::Rational(const Rational& r) { setRational(r.nume, r.deno); } // Τ瞶计ぇ祏俱计篶ㄧ计 Rational::Rational(const short &a) { setRational(a, 1); } // Τ瞶计ぇ疊翴计篶ㄧ计 Rational::Rational(const double &a) { setRational((int) (a*100), 100); } // Τ瞶计ぇ蛮じ篶ㄧ计 Rational::Rational(const char & a, const char & b) { if (isdigit(a) && isdigit(b)) setRational(a-48, b-48); else setRational(0, 1); } // Τ瞶计じ笲衡搭猭 Rational Rational::operator-() const { Rational nega; nega.nume = -1 * nume; nega.deno = deno; return nega.simplify(); } // Τ瞶计じ笲衡猭 Rational Rational::operator+(const Rational &r) const { Rational sum; sum.nume = nume * r.deno + r.nume * deno; sum.deno = deno * r.deno; return sum.simplify(); } // Τ瞶计じ笲衡搭猭 Rational Rational::operator-(const Rational &r) const { Rational diff; diff.nume = nume * r.deno - r.nume * deno; diff.deno = deno * r.deno; return diff.simplify(); } // Τ瞶计じ笲衡猭 Rational Rational::operator*(const Rational &r) const { Rational prod; prod.nume = nume * r.nume ; prod.deno = deno * r.deno; return prod.simplify(); } // Τ瞶计じ笲衡埃猭 Rational Rational::operator/(const Rational &r) const { Rational quot; if (r.nume!=0) { quot.nume = nume * r.deno ; quot.deno = deno * r.nume; } else { quot.nume = 0; quot.deno = 1; } return quot.simplify(); } // 砞﹚Τ瞶计ン穝 void Rational::setRational(const int p , const int q) { nume = p; deno = q; this->simplify(); } // GCD 程そ计 int Rational::gcd(int m, int n) { m = abs(m); n = abs(n); if (m % n == 0) return n; else return gcd(n, m % n); } // Τ瞶计て虏 Rational &Rational::simplify() { int g; if (deno==0) {nume = 0; deno = 1;} else if (deno<0) {nume = -nume; deno = -deno;} g = gcd(nume, deno); nume = nume / g; deno = deno / g; return *this; } // Τ瞶计块ねㄧΑ ostream &operator<<(ostream& output, const Rational& r) { if (r.nume==0) output << "0"; else if (r.deno==1) output << r.nume; else output << r.nume << "/" << r.deno; return output; } int main(void) { Rational a(3, 5), b(a), c(-2), d(2.50), e('9', '6'); // きΤ瞶计ン跑计 // 块きン跑计 cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << ", e = " << e << endl << endl; // 块衡砃Α "a + (b - c * d) / e" ㎝ㄤ璸衡挡狦 cout << "a + (b - c * d) / e = " << a + (b - c * d) / e << endl << endl; system("pause"); }