Expand on Assignment #2 using Fractions and write functions for all the math operators available and properly overloadin
-
- Site Admin
- Posts: 899566
- Joined: Mon Aug 02, 2021 8:13 am
Expand on Assignment #2 using Fractions and write functions for all the math operators available and properly overloadin
//Fraction Classclass Fraction{private: //2 Int members int numerator; int denominator;
public: void input(); void print();
Fraction();
Fraction(int n); Fraction(double); Fraction(int&, int&); Fraction Add(Fraction&); Fraction Subract(Fraction&); Fraction multiply(Fraction&); Fraction divide(Fraction&);
void Add(Fraction, Fraction); void Subract(Fraction, Fraction); void multiply(Fraction, Fraction); void divide(Fraction, Fraction);
};
//Input Functionvoid Fraction::input()
{ //Asking user to enter numerator cout << "Please enter Numerator: "; cin >> numerator;
//Asking user to enter denoinator cout << "Please enter Denominator: "; cin >> denominator;
//If condition to check if denominator equals0 if (denominator == 0) { cout << "Denomiantor can't bezero. Try again.\n"; cin >> denominator; }
}
//Print function which will print out the Fraction in the formnumerator/denominatorvoid Fraction::print(){ cout << numerator << "/" <<denominator << endl;}
//Empty constructor which initializes the Fraction zeroFraction::Fraction(){ numerator = 0; denominator = 1;}
//A one-parameter constructor for just a numerator (int). Thiswould make the Fraction represent a whole number.Fraction::Fraction(int n){ numerator = n; denominator = 1;
}
//A one-parameter constructor with a double. This would make aFraction by multiplying the double by 100000, then creating anumerator and denominator as the result.Fraction::Fraction(double x){ numerator = x * 100000; denominator = 100000;
}
//A two-parameter constructor with both numerator (int) anddenominator (int). This would make a Fraction with both a numeratorand denominatorFraction::Fraction(int& n, int& d)
{
//If condition to check if denominator equals0 if (d == 0) { cout << "Denomiantor can't bezero. Try again.\n"; cin >> denominator; }
//If denominator is less than 0 if (d < 0) { numerator = 0 - d; denominator = 0 - n; } else { numerator = n; denominator = d;
}
}Fraction Fraction::Add(Fraction& obj_f){
obj_f.numerator = obj_f.numerator *denominator; numerator = numerator * obj_f.denominator; obj_f.numerator = obj_f.numerator + numerator; obj_f.denominator = obj_f.denominator *denominator;
return obj_f;
}Fraction Fraction::Subract(Fraction& obj_f)
{
obj_f.numerator = obj_f.numerator * denominator;numerator = numerator * obj_f.denominator;obj_f.numerator = obj_f.numerator - numerator;obj_f.denominator = obj_f.denominator * denominator;
return obj_f;
}
Fraction Fraction::multiply(Fraction& obj_f)
{
obj_f.numerator = obj_f.numerator * numerator;obj_f.denominator = obj_f.denominator * denominator;
return obj_f;
}
Fraction Fraction::divide(Fraction& obj_f)
{
obj_f.numerator = numerator * obj_f.denominator;obj_f.denominator = denominator * obj_f.numerator;return obj_f;
}
void Fraction::Add(Fraction obj1, Fraction obj2)
{numerator = obj2.numerator * obj1.denominator + obj1.numerator *obj2.denominator;denominator = obj2.denominator * obj1.denominator;
}
void Fraction::Subract(Fraction obj1, Fraction obj2)
{
numerator = obj1.numerator * obj2.denominator - obj2.numerator *obj1.denominator;denominator = obj1.denominator * obj2.denominator;
}
void Fraction::multiply(Fraction obj1, Fraction obj2)
{numerator = numerator = obj1.numerator * obj2.numerator;denominator = numerator = obj1.denominator * obj2.denominator;
}
void Fraction::divide(Fraction obj1, Fraction obj2)
{
numerator = obj1.numerator * obj2.denominator;denominator = obj1.denominator * obj2.numerator;
}
//Main Functionint main()
{
char choice;
//Looping menu driven interfacedo
{Fraction fractionOne;Fraction fractionTwo(3.14534);
//Display Menucout << "What would you like to do today?: \n";cout << "A. Add\n";cout << "B. Subtract\n";cout << "C. Multiply\n";cout << "D. Divide\n";cout << "Q. Quit\n";
cout << "Choice: ";cin >> choice;
//Quit the program when user select q or Qif (choice == 'Q' || choice == 'q'){exit(0);
}
cout << "\nEnter first Fraction, \n";fractionOne.input();
cout << "\nEnter second Fraction, \n";fractionTwo.input();
cout << "\nFirst fraction: ";fractionOne.print();
cout << "Second fraction: ";fractionTwo.print();
//Switch statement based on users choiceswitch (choice)
{case 'A':
{Fraction fractionResult = fractionOne.Add(fractionTwo);
cout << "Fraction Result: ";fractionResult.print();
break;
}
case 'B':
{
Fraction fractionResult = fractionOne.Subract(fractionTwo);
cout << "Fraction Result: ";fractionResult.print();
break;
}
case 'C':
{Fraction fractionResult = fractionOne.multiply(fractionTwo);cout << "Fraction Result: ";fractionResult.print();
break;
}
case 'D':
{Fraction fractionResult = fractionOne.divide(fractionTwo);cout << "Fraction Result: ";fractionResult.print();break;
}
default:
{cout << "Invalid Choice.\n";break;
}
}cout << endl;
} while (1);
return 0;
}
Expand on Assignment #2 using Fractions and write functions for all the math operators available and properly overloading them. Write operator overloaded functions for the following operators: These binary functions return Fraction objects: +===*=/= + - / * = These unary functions return Fraction objects: ++ -- (both prefix & postfix) These extraction/insertion functions for printing and inputting Fraction objects: << >> Theses return boolean True / False: These casting functions: < > <= >= == | = | (double) (float) Examples: Fraction a, b, c; a = b + c; a = b-c; a = b; a/= b; int x = la; // 0 or 1 float f= (float) a; double d = (double) a ; a += b; a = b; a = b/c; a = b *c; if (a == b) if (a - b) if (a < b) if (a> b) if (a <= b) if (a <= b) In the ++ and --, these function increment or decrement by 1/1. So if b is 6/5, then at the end of each line a and b are:
b = -a if (a == b) if (a l= b) if (a<b) if (a> b) if (a <= b) if (a <= b) In the ++ and -- these function increment or decrement by 1/1. So if b is 6/5, then at the end of each line a and b are: a = ++b; // a is 11/5 bis 11/5 a = b++; // a is 6/5 bis 11/5 a = b; la is 1/5 bis 1/5 a = b--; // a is 6/5 bis 1/5 // a is 2/5 and b is-2/5 When a = 2/5, for - When a = 2/5, for ~ // a is 2/5 and b is 5/2 Printing and Inputting cout << a << endl; cin >> b; Create a private Low Term/Reduce() function within the class which reduces the Fraction to lowest terms. For example, Fractions like these would become these... 2/4 -> 1/2 5/8 -> 5/8 76/32 -> 19/8 17/17 -> 1/1 24/12 -> 2/1 45/65 -> 9/13 Once again, make sure to use exception handling in the event there is ever a divide by zero or any other error condition which would cause the program to terminate unexpectedly. Finally, expand the main program, so it allows the user to call any of the operator functions you've created. As before, the program ends when the user enters Q or q to quit for the math operation. int x la // 0 or 1 float f= (float) a; double d = (double) a b=~a