Server IP : 172.16.15.8 / Your IP : 3.135.205.231 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/tasantos/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// File Name: hw4.cpp // Author: Tara Santos // Due Date: Wed, Feb. 27 // Instructor: Dr. Wang // Compilation: vi hw4.cpp // Execution: g++ hw4.cpp // Goal: This program will read two fractions and an operator // +,-,*, or /, output the results #include <iostream> using namespace std; class Fraction { int nume; int deno; 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 Mult (Fraction f) { Fraction t; int n; int d; n = nume * f.nume; d = deno * f.deno; t.Set(n,d); return t; } Fraction Add (Fraction f) { Fraction s; int n; int d; n = nume * f.deno + deno * f.nume; d = deno * f.deno; s.Set(n,d); return s; } Fraction Sub (Fraction f) { Fraction r; int n; int d; n = nume * f.deno - deno * f.nume; d = deno * f.deno; r.Set(n,d); return r; } Fraction Div (Fraction f) { Fraction q; int n; int d; n = nume * f.nume / deno * f.nume; d = deno * f.deno; q.Set(n,d); return q; } }; int main() { int n; int d; Fraction x; Fraction y; Fraction z; char dummy; char op; cout << "Input x (n/d, -0/0 to quit): " << endl; cin >> n >> dummy >> d; while(n!= -0 && d!=0) { x.Set(n,d); cout << "Input y (n/d): " << endl; cin >> n >> dummy >> d; y.Set(n,d); cout << "Operation? (+,-,*,/): " << endl; cin >> op; if (op == '*') z = x.Mult(y); else if (op == '+') z = x.Add(y); else if (op == '-') z = x.Sub(y); else if (op == '/') z = x.Div(y); else cout << "\n\n"; cout << "z: "; x.Print(); cout << " " << op << " "; y.Print(); cout << " = "; z.Print(); cout << "\n"; cout << "=============================" << endl; cout << "Input x (n/d, -0/0 to quit): "; cin >> n >> dummy >> d; } cout << "\nGoodbye." << endl; return 0; }