Server IP : 172.16.15.8 / Your IP : 3.129.69.134 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/jmsanchez/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// Author: Jason Sanchez // Instructor: Dr. Wang // Due Date: April 21, 2008 // Goal: redo assign 4 with operator #include <iostream> using namespace std; class Fraction { int num; int deno; public: Fraction() { num = 0; deno = 1; } Fraction(int n, int d) { num = n; deno = d; } void Set(int n, int d) { num = n; deno = d; } void Print() const { cout << num << '/' << deno; } Fraction operator+ (Fraction f) { Fraction t; int n; int d; n = num*f.deno + deno*f.num; d = deno*f.deno; t.Set(n, d); return t; } Fraction operator- (Fraction f) { Fraction t; int n, d; n = (num*f.deno) - (deno*f.num); d = deno*f.deno; t.Set(n, d); return t; } Fraction operator* (Fraction f) { Fraction t; int n, d; n = num*f.num; d = deno*f.deno; t.Set(n, d); return t; } Fraction operator/ (Fraction f) { Fraction t; int n, d; n = num*f.deno; d = deno*f.deno; t.Set(n, d); return t; } }; int main() { int n; int d; Fraction x; Fraction y; Fraction z; char dummy; char op; cout << "Input x (n/d, -1/1 to quit): "; cin >> n >> dummy >> d; while( n!=-1 && d!=1 ) { x.Set(n, d); cout << "Input y (n/d): "; cin >> n >> dummy >> d; y.Set(n, d); cout << "Which operation: (+, -, *, /): "; cin >> op; if (op == '+') z = x+y; else if (op == '-') z=x-y; else if( op == '*') z = x*y; else if (op =='/') z=x/y; else cout << "Invalid, Please try again.\n\n"; cout <<"\n"; x.Print(); cout <<" " << op <<" "; y.Print(); cout << " = "; z.Print(); cout <<"\n"; cout << "********************************************" << endl; cout << "\n"; cout << "Input x (n/d, -1/1 to quit): "; cin >> n >> dummy >> d; } return 0; }