#include <iostream>
usingnamespace std;
class FRACT
{
private: int n, d;
public: void ReadFract()
{
cout << "Enter a fraction: ";
cin >> n; cin.ignore();
cout << "Enter another fraction: "; cin >> d;
}
//display the data
void DisplayFract()
{
cout << n << '/' << d;
}
//overload addition operator
friend FRACT operator +(FRACT F2, FRACT F1)
{
FRACT r;
// r.n/r.d = F2.n/F2.d + F1.n/F1.d
// = (F2.n * F1.d + F2.d * F1.n) / (F2.d * F1.d)
r.n = F2.n * F1.d + F2.d * F1.n;
r.d = F2.d * F1.d;
return r;
}
//create a member to find F1-F2
FRACT operator - (FRACT F2)
{
FRACT r;
// r.n / r.d = n / d - F2.n / F2.d
r.n = F2.d * n - F2.n * d;
r.d = d * F2.d;
return r;
}
//create a member to find F1 * F2
FRACT operator * (FRACT F2)
{
FRACT r;
// r.n / r.d = n / d * F2.n / F2.d
r.n = F2.n * n;
r.d = d * F2.d;
return r;
}
//determine if true or false
friendbooloperator < (FRACT F2, FRACT F1)
{
bool TrueFalse;
if(F2.n * F1.d < F2.d *F1.n) return (cout << "TRUE");
elsereturn (cout << "FALSE");
}
};
int main()
{
bool TrueFalse;
char again;
FRACT f1, f2;
do
{
f1.ReadFract();
f2.ReadFract();
FRACT R = f1 + f2;
FRACT R2 = f1 - f2;
FRACT R3 = f1 * f2;
f1.DisplayFract();
cout << " + "; f2.DisplayFract(); cout << " = ";
R.DisplayFract();
cout << endl;
//subtract operator
f1.DisplayFract();
cout << " - "; f2.DisplayFract(); cout << " = ";
R2.DisplayFract();
cout << endl;
f1.DisplayFract();
cout << " * "; f2.DisplayFract(); cout << " = ";
R3.DisplayFract();
cout << endl;
f1.DisplayFract();
cout << " < "; f2.DisplayFract(); cout << " is ";
bool TrueFalse = f1 < f2;
cout << endl;
cout << "CONTINUE(y/n)? "; cin >> again;
}while( again == 'y' || again == 'Y');
system("PAUSE");
return 0;
}
Enter a fraction comes two times, how do I fix this? I tried a for loop in the read function but it comes out even worse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Enter a fraction: 2/3
Enter another fraction: Enter a fraction: 4/5
Enter another fraction: 2/3 + 4/5 = 22/15
2/3 - 4/5 = -2/15
2/3 * 4/5 = 8/15
2/3 < 4/5 is TRUE
CONTINUE(y/n)? y
Enter a fraction: 7/3
Enter another fraction: Enter a fraction: 4/5
Enter another fraction: 7/3 + 4/5 = 47/15
7/3 - 4/5 = 23/15
7/3 * 4/5 = 28/15
7/3 < 4/5 is FALSE
CONTINUE(y/n)? n
Press any key to continue . . .