Server IP : 172.16.15.8 / Your IP : 18.222.56.251 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/amjamgochian/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// CS 212 CS II Assignment 3 // Due: Mon. Feb 28, 2011 // // File Name: assign3_fractions.cpp // Author: Abby Jamgochian // Instructor: Dr. Wang // // Compile: g++ assign3_fractions.cpp // Run: ./a.out < out3.txt // // Goal: The program will read a file with fractions in the order of // first fraction, operation, second fraction and output the // answer and the problem to another file. #include <iostream> using namespace std; class Fraction { int nume; int denom; public: Fraction() { nume = 0; denom = 1; } Fraction(int n, int d) { nume = n; denom = d; } void Set(int n, int d) { nume = n; denom = d; } void Print() const { cout << nume << "/" << denom; } Fraction operator+(Fraction x) { Fraction ans; ans.nume = nume * x.denom + denom * x.nume; ans.denom = denom * x.denom; return ans; } Fraction operator-(Fraction x) { Fraction ans; ans.nume = nume * x.denom - denom * x.nume; ans.denom = denom * x.denom; return ans; } Fraction operator*(Fraction x) { Fraction ans; ans.nume = nume * x.nume; ans.denom = denom * x.denom; return ans; } Fraction operator/(Fraction x) { Fraction ans; ans.nume = nume * x.denom; ans.denom = denom * x.nume; return ans; } }; /* int main() { Fraction a, b, c; Fraction d, e, f; int an, ad, bn, bd; cout << "The first fraction (nume, deno); "; cin >> an >> ad; cout << "The second fraction (nume, deno); "; cin >> bn >> bd; a.Set(an, ad); b.Set(bn, bd); c = a.Addition(b); e = a.Multiplication(b); a.Print(); cout << " + "; b.Print(); cout << " = "; c.Print(); cout << "\n"; a.Print(); cout << " * "; b.Print(); cout << " = "; e.Print(); return 0; }*/ int main() { int a1, b1; int a2, b2; char dummy, op; cin >> a1 >> dummy >> b1; cin >> op; cin >> a2 >> dummy >> b2; while(cin) { Fraction f1, f2, ans; f1.Set(a1, b1); f2.Set(a2, b2); if(op == '+') ans = f1 + f2; if(op == '-') ans = f1 - f2; if(op == '*') ans = f1 * f2; if(op == '/') ans = f1 / f2; cin >> a1 >> dummy >> b1; cin >> op; cin >> a2 >> dummy >> b2; f1.Print(); cout << " " << op << " "; f2.Print(); cout << " = "; ans.Print(); cout << "\n"; cin >> a1 >> dummy >> b1; cin >> op; cin >> a2 >> dummy >> b2; } cout << "\n\nAll done.\n\n"; return 0; }