Server IP : 172.16.15.8 / Your IP : 3.147.13.220 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/bsjackson/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// // 1/2 + 3/4 = ? #include <iostream> using namespace std; class Fraction { int nume, deno; //reduce the fraction p115 void reduce(fracType& 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 Addition(Fraction x) { Fraction ans; //temp obj ans.nume = nume * x.deno + deno * x.nume; ans.deno = deno * x.deno; //use the reduce() //??? return ans; } Fraction Subtraction(Fraction x) { Fraction ans; //temp obj ans.nume = nume * x.deno - deno * x.nume; ans.deno = deno * x.deno; return ans; } Fraction Multiplication(Fraction x) { Fraction ans; //temp obj ans.nume = (nume * x.nume); ans.deno = (deno * x.deno); return ans; } Fraction Division(Fraction x) { Fraction ans; //temp obj ans.nume = (nume / x.nume); ans.deno = (deno / x.deno); return ans; } }; int main() { Fraction a, b, c, d, e, f; // c = a + b int t1, t2, t3,t4; cin >> t1 >> t2 >> t3 >> t4; a.Set(t1, t2); // d = a - b ; e = a * b b.Set(t3, t4); // f = a / b c = a.Addition(b); a.Print(); cout << " + "; b.Print(); cout << " = "; c.Print(); cout << "\n\n"; d = a.Subtraction(b); a.Print(); cout << " - "; b.Print(); cout << " = "; d.Print(); //cout << " - "; cout << "\n\n"; e = a.Multiplication(b); a.Print(); cout << " * "; b.Print(); cout << " = "; e.Print(); cout << "\n\n"; f = a.Division(b); a.Print(); cout << " / "; b.Print(); cout << " = "; f.Print(); cout << "\n\n"; cout << "Hi.\n\n"; return 0; }