Tuesday, May 5, 2020

FracTable.cpp Essay Example For Students

FracTable.cpp Essay //FracTable#include #include #include conio.h //required for getch()#include //required for setw()using namespace std;class fraction //fraction classprivate:int num,den;public:fraction(): num(0),den(0)}fraction(int n,int d):num(n),den(d) }void display() coutnum/;cout.setf(ios::left);coutsetw(4)den;} fraction mult(fraction, fraction);void dispheader()cout num /;cout.setf(ios::left);coutsetw(4)den;}void lowterms();};//-Source copy from textbook-void fraction::lowterms() // Change ourself to lowest termslong tnum, tden, temp, gcd;tnum = labs(num); // use non-negative copiestden = labs(den); // (needs cmath)if(tden == 0) // check for 0/ncout Illegal fraction: division by 0; exit(1);}else if(tnum==0) //check for 0/nnum = 0; den = 1; return;}//This while loop finds the gcd of tnum tdenwhile(tnum != 0)if(tnum tden) // ensure numerator largertemp = tnum; tnum = tden; tden = temp; //swam them}tnum = tnum tden; // subtract them}gcd = tden; // this is greates comon divisornum = num / gcd; // divide both num and den by gcdden = den / gcd; // to reduce frac to lowest terms} //End of source copy from textbook-int get_den() //get denominator from userint valid =2; int denom;cout Enter a denominator:a ;cindenom; coutendl;if (denom valid)do //loop until valid entrycout Invalid entry. Try again.endl;cout Enter a denominator:a ;cin denom; coutendl;}while(denom valid); return denom;}/*-*/fraction fraction::mult(fraction x,fraction y)fraction temp;temp.num = x.num * y.num;temp.den = x.den * y.den;return temp;}/*-*/int main()int atemp,btemp,din;int headmax(8);int dashmax(7); din=(get_den()); coutsetw(headmax) ;for (atemp=1; atemp din ; atemp++) fraction x(atemp,din); x.lowterms(); x.dispheader(); }cout endl;for (int temp = 1; temp (din*dashmax); temp++) cout-;} for (atemp=1; atemp din ; atemp++) fraction x(atemp,din); coutendl;if (atemp din) x.lowterms(); x.display(); cout| ; } for (btemp=1; btemp din; btemp++) fraction y(btemp,din); fraction z= z.mult(x,y); z.lowterm s(); z.display(); } }cout endl Press any key to terminate program.a;getch(); //keep window open until key pressedreturn 0;}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.