Server IP : 172.16.15.8 / Your IP : 3.139.83.248 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/drsparks/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
#include <iostream> using namespace std; class Fraction { int nume, deno; void reduce(Fraction x) { int n = x.nume; int d = x.deno; while( d != 0 ) { int r = n % d; n = d; d = r; } if ( n != 0 ) { x.nume /= n; x.deno /= n; } if (x.deno < 0) { x.nume *= (-1); x.deno *= (-1); } } public: Fraction() { nume=0; deno=1; } Fraction(int n, int d) { nume=n; deno=d; } void Set(int n, int d) { nume=n; deno=d; } void Print() const { cout << nume << "/" << deno; } Fraction operator+(Fraction x) // p.134 { int n, d; // temp d = deno * x.deno; n = nume*x.deno + deno*x.nume; Fraction ans; ans.Set(n, d); Fraction::reduce(ans); return ans; } Fraction operator-(Fraction x) // p.134 { int n, d; // temp d = deno * x.deno; n = nume*x.deno - deno*x.nume; Fraction ans; ans.Set(n, d); Fraction::reduce(ans); return ans; } Fraction operator* (Fraction x) { int n, d; n = nume*x.nume; d = deno*x.deno; Fraction ans; ans.Set(n,d); Fraction::reduce(ans); return ans; } Fraction operator/ (Fraction x) { int n, d; n = nume*x.deno; d = deno*x.nume; Fraction ans; ans.Set(n,d); Fraction::reduce(ans); return ans; } }; int main() { Fraction fr1, fr2, fr3; int n, d; char dummy, op; cin >> n >> dummy >> d; fr1.Set(n, d); while( cin ) { cin >> op; cin >> n >> dummy >> d; fr2.Set(n, d); cout << "\n\n"; fr1.Print(); cout << " " << op << " "; fr2.Print(); if( op == '+') fr3 = fr1 + fr2; else if( op == '*' ) fr3 = fr1 * fr2; else if ( op == '-' ) fr3 = fr1 - fr2; else if ( op == '/' ) fr3 = fr1 / fr2; else cout << "This Will Not Work." << endl; cout << " = "; fr3.Print(); cout << endl; cout << "\n\n--------------\n\n"; cin >> n >> dummy >> d; fr1.Set(n, d); } return 0; }