Server IP : 172.16.15.8 / Your IP : 3.15.143.18 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/mdwiberg/../cjabbott/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// File name: assign4.cpp // Author: Christie Abbott // Instructor: Dr. Wang // Due Date: February 27, 2008 // Compiling: g++ assign4.cpp // Execution: ./a.out // Goal: The program will perform the following operations on two fractions, // (+, -, /, *). #include <iostream> using namespace std; class Fraction { int numerator, denominator; public: Fraction() { numerator = 0; denominator = 1; } Fraction(int n, int d) { numerator = n; denominator = d; } void Set(int n, int d) { numerator = n; denominator = d; } void Write() const { cout << numerator << '/' << denominator; } Fraction mult(Fraction f) { Fraction t; int n, d; n = numerator * f.numerator; d = denominator * f.denominator; t.Set(n, d); return t; } Fraction add(Fraction g) { Fraction t; int n, d; n = numerator + g.numerator; d = denominator + g.denominator; t.Set(n, d); return t; } Fraction sub(Fraction s) { Fraction t; int n, d; n = numerator - s.numerator; d = denominator - s.denominator; t.Set(n, d); return t; } Fraction div(Fraction a) { Fraction t; int n, d; n = numerator / a.numerator; d = denominator / a.denominator; t.Set(n, d); return t; } }; int main() { Fraction x; Fraction y; Fraction z; int n, d; 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 << "Operation? (+, -, *, /) "; 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); cout << "\n"; x.Write(); cout << " " << op << " "; y. Write(); cout << " = "; z.Write(); cout << "\n"; cout << "\n\nInput x (n/d), -1/1 to quit): "; cin >> n >> dummy >> d; } cout << "\n\nBye.\n\n"; return 0; }