this piece of code was fine when i replaced operator+-*/ with add, subtract, multiply, and divide. now i am getting errors that there are too many parameters in the overloading operators. i am not too familar with overloading operators. should i create a new class that contains all the parameters? a friend function (which i am also not too familar with)? also, is anybody familar with using the assert() method in the <cassert> library so the program will print out an error if a user inputs "0" as the denominator?
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
///////////////////////////// fraction Class ///////////////////////////////
class fraction
{
private:
int num;
int denom;
char dummychar;
public:
fraction() : num(0), denom(0)
{ } // Constructor
void getfraction() // Input fractions and sign
{
cout << "Please Enter Fraction: ";
cin >> num >> dummychar >> denom;
cout << "\n";
}
void operator+(fraction& fr1, fraction& fr2, fraction& fr3); // decl. of addition function
void operator-(fraction& fr1, fraction& fr2, fraction& fr3); // decl. of subtract.function
void operator*(fraction& fr1, fraction& fr2, fraction& fr3); // decl. of multipl. function
void operator/(fraction& fr1, fraction& fr2, fraction& fr3); // decl. of division function
void lowterms(fraction& fr3); // declaration of lowterms function
void showfraction(fraction& fr3) // Display result on the screen
{
cout << "Answer= " << fr3.num << "/"
<< fr3.denom << "\n\n";
}
};
// ------------------------------ Addition ------------------------------
void fraction::operator+(fraction& fr1, fraction& fr2, fraction& fr3)
{
fr3.num = fr1.num*fr2.denom + fr1.denom*fr2.num;
fr3.denom = fr1.denom*fr2.denom;
}
// ----------------------------- Subtraction-----------------------------
void fraction::operator-(fraction& fr1, fraction& fr2, fraction& fr3)
{
fr3.num = fr1.num*fr2.denom - fr1.denom*fr2.num;
fr3.denom = fr1.denom*fr2.denom;
}
// ---------------------------- Multiplication---------------------------
void fraction::operator*(fraction& fr1, fraction& fr2, fraction& fr3)
{
fr3.num = fr1.num*fr2.num;
fr3.denom = fr1.denom*fr2.denom;
}
// ------------------------------ Division ------------------------------
void fraction::operator/(fraction& fr1, fraction& fr2, fraction& fr3)
{
fr3.num = fr1.num*fr2.denom;
fr3.denom = fr1.denom*fr2.num;
}
// ------------------------ Lowest Term Function ------------------------
void fraction::lowterms(fraction& fr3) // Simplify to lowest terms
{
long tnum, tden, temp, gcd;
tnum = labs(fr3.num);
tden = labs(fr3.denom);
if(tden==0)
{ cout << "Illegal fraction: division by 0 \n"; exit(1); }
else if(tnum==0)
{ fr3.num=0; fr3.denom=1; return; }
while(tnum != 0)
{
if(tnum < tden)
{ temp=tnum; tnum=tden; tden=temp; }
tnum = tnum - tden;
}
gcd = tden;
fr3.num = fr3.num / gcd;
fr3.denom = fr3.denom / gcd;
}
char getsign(); // declaration of getsign function
//////////////////////////////////////////////////////////////////////////
int main()
{
fraction fract1, fract2, fract3;
char response;
char sgn;
while (response != 'N')
{
fract1.getfraction();
fract2.getfraction();
sgn = getsign();
switch (sgn) // Arithmetic Switch
{
case '+':
fract3.operator+(fract1, fract2, fract3);
break;
case '-':
fract3.operator-(fract1, fract2, fract3);
break;
case '*':
fract3.operator*(fract1, fract2, fract3);
break;
case '/':
fract3.operator/(fract1, fract2, fract3);
break;
}
fract3.lowterms(fract3); // function call to simplify terms
fract3.showfraction(fract3);
cout << "Calculate Another (Y or N)? ";
cin >> response;
cout << "\n\n";
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
char getsign()
{
char sign;
cout << "Please Enter Mathematic Operation: ";
cin >> sign;
cout << "\n";
return sign;
}