Hello I'm new in this forum and working many hours at this problem's solving but my current problem is that i cant make the do-while loop stops fractions inserting when giving 0/1 fraction. If anybody can post the answer.. Thank you!!
I am uploading 2 files that working and i think are the combination for this solution
Code:
#include <iostream>
#include <cmath>
using namespace std;
class frac
{
private:
long num;
long den;
public:
frac() : num(0),
den(1) {}
frac(long d) : num(d),
den(1) {}
frac(long n, long d) : num(n),
den(d) {}
long getnum(void){return num;}
long getden(void){return den;}
void fadd( frac f1, frac f2);
bool equals( frac f);
bool notequals( frac f);
void putfrac();
void getfrac();
void lowterms();
};
void frac::fadd(frac f1, frac f2)
{
num = f1.num*f2.den + f1.den*f2.num;
den = f1.den*f2.den;
lowterms();
}
bool frac::equals(frac f)
{
return (num==f.num && den==f.den) ? true : false;
}
bool frac::notequals(frac f)
{
return (num!=f.num || den!=f.den) ? true : false;
}
void frac::putfrac()
{
cout << num << '/' << den;
}
void frac::getfrac()
{
char dummychar;
while(1)
{
cin >> num;
cin >> dummychar;
cin >> den;
if(den != 0)//......??
break;
cout << "Denominator can't be 0. Try again: ";
}
}
void frac::lowterms()
{
long tnum, tden, temp, gcd;
tnum = labs(num); //......??
tden = labs(den);
if( tnum!=0 && tden==0 )
{
cout << "Division by 0, illegal fraction: ";
exit(1);
}
else if( tnum==0 )
{
num=0;
den = 1; //......??
return;
}
while(tnum != 0)
{
if(tnum < tden)
{
temp=tnum;
tnum=tden;
tden=temp;
}
tnum = tnum - tden;
}
gcd = tden;
num = num / gcd;
den = den / gcd;
}
int main()
{
frac frarr[100];
frac total(0, 0), average;
int count = 0;
char ch;
while(true)
{
cout << "\nEnter fraction (0/1 for exit): " ;
frarr[count++].getfrac();
}while(frarr[count].getnum()==0 && frarr[count].getden()==1);
cout << endl;
system("PAUSE");
}
************ ********* ********* **
Start with the fraction class from Exercises 11 and 12 in Chapter 6. Write a main() program that obtains an arbitrary number of fractions from the user, stores them in an array of type fraction, averages them, and displays the result.
I am sorry this is what Ex06Ch6 wants.
************ ********* ********* **
Start with the fraction class from Exercises 11 and 12 in Chapter 6. Write a main() program that obtains an arbitrary number of fractions from the user, stores them in an array of type fraction, averages them, and displays the result.
I am sorry i forgot write what this lab asks.