Server IP : 172.16.15.8 / Your IP : 18.188.107.57 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/csdixon/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
/* Programmer: Christopher S. Dixon Prof: Dr. Zizhong Wang Procedure: This program is designed to take two fractions that are inputted by the user in the format (numerator)/(denominator) and calculate them using addition, subtraction, multiplication, or division, the process is also decided by user input (+, -, *, /). Compile(in linux environment): g++ clemsoncode2.cpp Execute(in linux environment): ./a.out */ #include <iostream> using namespace std; class Fraction { int num; int den; public: Fraction() // default fraction is 0/1 { num=0; den=1; } Fraction(int n, int d) { num=n; den=d; } //Function for Setting Numerator and Denominator void Set(int n, int d) { num=n; den=d; } //Function for Outputting Fraction Solution void Print() const { cout<<num<<"/"<<den; } //Function Procedure for Addition Fraction Add (Fraction x) { Fraction answer; int n; int d; d = Fraction :: den * x.den; n = num * x.den + den*x.num; answer.Set(n,d); return answer; } //Funtion Procedure for Subtraction Fraction Sub (Fraction x) { Fraction answer; int n; int d; d = Fraction :: den * x.den; n = num * x.den - den*x.num; answer.Set(n,d); return answer; } //Function Procedure for Multiplication Fraction Mult (Fraction x) { Fraction answer; int n; int d; n = num*x.num; d = den*x.den; answer.Set(n,d); return answer; } //Function Procedure for Division Fraction Div (Fraction x) { Fraction answer; int n; int d; n = num/x.num; d = den/x.den; answer.Set(n,d); return answer; } }; int main() { //Initialize numerator and denominator int nu; int de; //Create dummy variable for "/" sign char dummy; //Create space for operation sign input char op; //Create Fraction 1 Fraction x; //Create Fraction 2 Fraction y; //Create Fraction 3 Fraction z; cout<< "*******************************************"<< endl; cout<< "* Thank you for using *"<< endl; cout<< "* my fraction program please *"<<endl; cout<<"* Input Fraction 1: (-1/1 or 1/1 to quit) *"<<endl; cout<< "* "; //User input for fraction 1 cin>>nu>>dummy>>de; while (nu!=(-1) && de!=1) { //Set Fraction 1 x.Set(nu,de); cout<<"* Input Fraction 2: * "<<endl; //User Input for fraction 2 cout<< "* "; cin>>nu>>dummy>>de; //Set Fraction 2 y.Set(nu,de); cout<<"* Input the operation: * "<<endl; cout<<"* "; //Select operation for program to run cin>>op; //If Operation selected is addition call Addition Function if (op=='+') { z=x.Add(y); cout<< "* "; x.Print(); cout<<" + "; y.Print(); cout<<" = "; z.Print(); } //If Operation selected is subtraction call Subtraction Function else if (op=='-') { z=x.Sub(y); cout <<"* "; x.Print(); cout<<" - "; y.Print(); cout<<" = "; z.Print(); } //If Operation selected is multiplication call Multiplication Function else if (op=='*') { z=x.Mult(y); cout << "* "; x.Print(); cout<<" * "; y.Print(); cout<<" = "; z.Print(); } //If Operation selected is division call Division Function else if (op=='/') { z=x.Div(y); cout << "* "; x.Print(); cout<<" / "; y.Print(); cout<<" = "; z.Print(); } else //If Operation selected is not a known math operation cout<<"* This is not a valid operation. *"<<endl; cout<<"\n* Input Fraction 1: (-1/1 or 1/1 to quit) *"<<endl; cout<<"* "; cin>>nu>>dummy>>de; } cout<<"* *"<<endl; cout<<"* *"<<endl; cout<<"* Thank you for using *"<<endl; cout<<"* my fraction program *"<<endl; cout<<"*******************************************"<< endl; return 0; }