Question: C++ : Complete the implementation of the given Fraction class Notes : You are not allowed to make any change to the given Fraction class.
C++ :
Complete the implementation of the given Fraction class
Notes :
You are not allowed to make any change to the given Fraction class.
make sure that there is NO common factors between *numerator and *denominator.
if the Fraction is negative, the negative sign always goes to *numerator , *denominator always greater than or equal to 1 .
for the output operator << ,
if *denomiator is equal to 1 , the denominator will not be displayed. (i.e. 2 instead of 2/1 )
if the Fraction is negative, make sure that negative sign always goes with numerator ( i.e. -2/3 instead of 2/-3 )
use the given driver to test your code.
You must put the Fraction class and its implementation in separate files named Fraction.h and Fraction.cpp .
Fraction class:
#ifndef FRACTION_H #define FRACTION_H #include#include #include using namespace std; class Fraction { private: int *numerator ; int *denominator; public: int getNumerator() ; int getDenominator() ; void reduce() ; int gcd(int ,int ) ; // constructors Fraction() ; // default c'tor Fraction (int n) ; // create a fraction of n/1 Fraction(int n, int m) ; // Fraction n/m Fraction(Fraction & other) ; // copy c'tor ~Fraction() ; // destructor Fraction & operator=(Fraction & rhs) ; // overload assignment operator Fraction & operator+(Fraction &rhs) ; Fraction & operator-(Fraction &rhs) ; // overload binary operator - Fraction & operator-() ; // overload unary operator - (negative) Fraction & operator *(Fraction &rhs) ; Fraction & operator/(Fraction & rhs) ; Fraction & operator++() ;// overload prefix ++ Fraction & operator++(int) ; // overload postfix ++ Fraction & operator--() ;// overload prefix -- Fraction & operator--(int) ; // overload postfix -- // overload relational operators bool operator >(Fraction & rhs) ; // return true if *this > rhs , false elsewise bool operator == (Fraction & rhs) ; bool operator < (Fraction & rhs) ; bool operator !=(Fraction &rhs) ; Fraction & operator+=(Fraction & rhs) ; Fraction & operator-=(Fraction & rhs) ; Fraction & operator*=(Fraction & rhs) ; Fraction & operator/=(Fraction & rhs) ; string toString() ; char * toCstring() ; bool isZero() ; // return true if *this is zero int power(int base, int exp) ; Fraction & operator^(int n) ; friend istream & operator >> (istream & in , Fraction & rhs) ; friend ostream & operator << (ostream & out ,Fraction & rhs) ; }; #endif
driver:
#include#include #include #include "Fraction.h" using namespace std; void test1(Fraction op1, Fraction op2) ; void test2(Fraction op1, Fraction op2) ; void test3(Fraction op1, Fraction op2) ; void test4(Fraction op1, Fraction op2) ; void test5(Fraction op1, Fraction op2) ; void test0(Fraction op1, Fraction op2) ; void (*fp[])(Fraction, Fraction) = {test0, test1,test2,test3,test4,test5}; void menu(){ cout << " \t\tTest menu "; cout << " \t\t0: Test copy c'tor , overload assignment operator: "; cout << " \t\t1: Test arithematic operators: + - * / , unary - "; cout << " \t\t2: Test += , -+, *= , /= operators "; cout << " \t\t3: Test relational operators > < == != "; cout << " \t\t4: Test prefix/postfix increment/decrement operators ++ and -- "; cout << " \t\t5: Test exponenett operator ^ "; cout << " \t\t6: Test ALL of the above "; cout << " \t\t7: reset operands "; cout << " \t\t8: Quit "; } int main() { Fraction f1(1,3) ; Fraction f2(1,2) ; int choice = 0; while(true) { cout <<"\tPlease enter a fraction in the form a/b: "; cin >> f1 ; cout << "\tYou have entered : " << f1 << endl ; cout << "\tPlease enter 2nd fraction : " ; cin >> f2 ; cout << "\tyou have entered another fraction : " << f2.toString() < > choice; if ( choice >= 0 && choice < 6 ) fp[choice](f1,f2) ; else if (choice == 6 ) { for(int i = 0 ; i < 6 ; i++ ) fp[i](f1,f2) ; } else if (choice == 7 ) break ; else exit(0) ;; } } return 0 ; } void test0(Fraction op1, Fraction op2) { printf(" \t\tTesting copy c'tor and overload assignment operator ") ; Fraction a(op1) ; Fraction b(op2) ; printf("\t\top1 = %s , op2 = %s ", op1.toCstring() , op2.toCstring()) ; printf("\t\ta = %s b = %s ", a.toCstring() , b.toCstring()) ; { Fraction c ; Fraction d ; Fraction e ; d = a ; e = b ; c = a+b ; printf("\t\t %s + %s = %s ", d.toCstring(), e.toCstring(), c.toCstring()); d = e = c; } printf("\t\ta = %s b = %s ", a.toCstring() , b.toCstring()) ; } void test1( Fraction op1, Fraction op2){ cout <<" \t\tTesting overload arithematic operators + - * / negative - "; printf("\t\top1 : %s and op2 : %s ", op1.toCstring(), op2.toCstring()) ; printf("\t\t-(%s) is %s ", op1.toCstring(), (-op1).toCstring()) ; printf("\t\t%s + %s = %s ", op1.toCstring() , op2.toCstring() , (op1+op2).toCstring()) ; printf("\t\t%s - %s = %s ", op1.toCstring() , op2.toCstring() , (op1-op2).toCstring()) ; printf("\t\t%s * %s = %s ", op1.toCstring() , op2.toCstring() , (op1*op2).toCstring()) ; if (!op2.isZero()) printf("\t\t%s / %s = %s ", op1.toCstring() , op2.toCstring() , (op1/op2).toCstring()) ; } void test5( Fraction op1, Fraction op2){ cout <<" \t\tTesting overload ^ (exponentiation opertator) "; printf("\t\top1 : %s and op2 : %s ", op1.toCstring(), op2.toCstring()) ; for(int i = -2 ; i < 3 ; i++) printf("\t\t(%s)^%d = %s ", op1.toCstring() , i , (op1^i).toCstring()) ; for(int i = -2 ; i < 3 ; i++) printf("\t\t(%s)^%d = %s ", op2.toCstring() , i , (op2^i).toCstring()) ; } void test2( Fraction op1, Fraction op2){ cout <<" \t\tTesting overload += -= *= /= "; printf("\t\top1 : %s and op2 : %s ", op1.toCstring(), op2.toCstring()) ; Fraction a(op1) ; Fraction b(op2) ; Fraction c , d ; printf("\t\t a = %s b = %s ", a.toCstring() , b.toCstring()) ; // a+=b ; printf("\t\t after a += b , a = %s and b = %s ", (a+=b).toCstring() , b.toCstring()) ; printf("\t\t after a -= b , a = %s and b = %s ", (a-=b).toCstring() , b.toCstring()) ; printf("\t\t after a *= b , a = %s and b = %s ", (a*=b).toCstring() , b.toCstring()) ; printf("\t\t after a /= b , a = %s and b = %s ", (a/=b).toCstring() , b.toCstring()) ; } void test3(Fraction op1, Fraction op2){ printf(" \t\tTesting overload relational operators > < == != ") ; printf("\t\top1 = %s , op2 = %s ", op1.toCstring() , op2.toCstring()) ; printf("\t\t%s > %s is %s ", op1.toCstring(), op2.toCstring(), ((op1 > op2)? "true" :"false")) ; printf("\t\t%s < %s is %s ", op1.toCstring(), op2.toCstring(), ((op1 < op2)? "true" :"false")) ; printf("\t\t%s == %s is %s ", op1.toCstring(), op2.toCstring(), ((op1 == op2)? "true" :"false")) ; printf("\t\t%s != %s is %s ", op1.toCstring(), op2.toCstring(), ((op1 != op2)? "true" :"false")) ; } void test4(Fraction op1, Fraction op2) { cout << " \t\tTesting prefix and postfix ++ , -- " << endl << endl ; Fraction a(op1) ; Fraction b(op2) ; Fraction c,d,e; c = a + b ; printf("\t\ta = %s , b = %s a+b = %s ", a.toCstring() , b.toCstring() , (a+b).toCstring()) ; d = (++a) + (--b) ; printf("\t\ta = %s b = %s after d = (++a) + (--b) = %s ", a.toCstring(), b.toCstring(), d.toCstring()); d = (a--) + (b++) ; printf("\t\ta = %s b = %s after d = (a--) + (b++) = %s ", a.toCstring(), b.toCstring(), d.toCstring()); d = (--a) + (b++) ; printf("\t\ta = %s b = %s after d = (--a) + (b++) = %s ", a.toCstring(), b.toCstring(), d.toCstring()); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
