Been working on the code for a bit, its still not ready to run. Hoping I can get some more feedback/direction.
Here are the class parameters I need to stick to, in case its unclear:
CFrac
--------------------------------------------
- numerator : int
- denominator : int
- simplify () : void
--------------------------------------------
+ CFrac (int = 0, int = 1)
+ ~CFrac()
+ add (const CFrac&) const : CFrac
+ subtract (const CFrac&) const : CFrac
+ multiply (const CFrac&) const : CFrac
+ divide (const CFrac&) const : CFrac
+ showFrac () const : void
+ setFrac () : void
I still need to get the calculation functions to work and have the answer simplified for the showFrac output.
Thanks for the feedback.
Current code is looking as such:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <conio.h>
using namespace std;
//class definition
class CFrac
{
private:
int numerator, denominator;
void simplify();
public:
CFrac (int = 0, int = 1);
~CFrac();
int add (const CFrac&) const;
int subtract (const CFrac&) const;
int multiply (const CFrac&) const;
int divide (const CFrac&) const;
void showFrac () const;
void setFrac ();
};
//start of class function definition
void CFrac::simplify ()
{
int num, denom, mod, remainder;
num = numerator;
denom = denominator;
while(mod != 0)
{
mod = num / denom;
remainder = num % denom;
num = denom;
denom = remainder;
}
numerator /= remainder;
denominator /= remainder;
}
CFrac::CFrac (int n, int d)
{
numerator = n;
denominator = d;
}
CFrac::~CFrac() {}
int CFrac::add (const CFrac& fraction) const
{
int num, denom;
num = numerator * fraction.denominator + fraction.numerator * denominator;
denom = denominator * fraction.denominator;
return //??
}
int CFrac::subtract (const CFrac& fraction) const
{
int num, denom;
num = fraction.numerator * denominator - numerator * fraction.denominator;
denom = fraction.denominator * denominator;
return //??
}
int CFrac::multiply (const CFrac& fraction) const
{
int num, denom;
num = numerator * fraction.numerator;
denom = denominator * fraction.denominator;
return //??
}
int CFrac::divide (const CFrac& fraction) const
{
int num, denom;
num = fraction.numerator * denominator;
denom = fraction.denominator * numerator;
return //??
}
void CFrac::showFrac () const
{
int mod, wholeNum;
mod = numerator % denominator;
wholeNum = (numerator - mod) / denominator;
cout << wholeNum << " " << mod << "/" << denominator;
}
void CFrac::setFrac ()
{
cout << "Enter a fraction: ";
cin >> numerator;
cin.ignore(1);
cin >> denominator;
}
//end of class function definition
int userMenu ();
int main()
{
CFrac fracOne, fracTwo, fracCalc;
int firstFraction, secondFraction;
int menuChoice = 0;
while(menuChoice != 5)
{
menuChoice = userMenu();
fracOne.setFrac();
fracTwo.setFrac();
if(menuChoice == 1)
fracCalc = fracOne.add(fracTwo);
else if(menuChoice == 2)
fracCalc = fracOne.subtract(fracTwo);
else if(menuChoice == 3)
fracCalc = fracOne.multiply(fracTwo);
else if(menuChoice == 4)
fracCalc = fracOne.divide(fracTwo);
else
cout << "Not a valid option, select again." << endl;
fracCalc.showFrac();
}
_getch();
return 0;
}
int userMenu()
{
int choice;
cout << "Fraction Calculation Menu:" << endl
<< "1 -- ADDITION" << endl
<< "2 -- SUBTRACTION" << endl
<< "3 -- MULTIPLICATION" << endl
<< "4 -- DIVISION" << endl
<< "5 -- EXIT" << endl
<< "Enter a choice --> ";
cin >> choice;
return choice;
}
|