 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: fraction class [was:need help plzz]

1. Registered User
Join Date
May 2005
Posts
29

## fraction class [was:need help plzz]

I want to Modify the four-function fraction calculator of this code
Code:
```# include<iostream>

using namespace std;
struct fraction
{ int a;
int b;
int e;
int f;
};
fraction fadd (fraction,fraction);
fraction fsub (fraction,fraction);
fraction fmul (fraction,fraction);
fraction fdiv (fraction,fraction);

int main()
{	fraction g1,g2;
fraction total;
char op,slash,val;
do {
cout<<"Enter First Fraction:";
cin>>g1.a>>slash>>g1.b;
cout<<"\nEnter an operator:";
cin>>op;
cout<<"\nEnter Second fraction:";
cin>>g2.a>>slash>>g2.b;

switch (op)
{
case '+':
total= fadd(g1,g2);
break;
case '-':
total= fsub(g1,g2);

break;
case '*':
total= fmul(g1,g2);

break;
case '/':
total= fdiv(g1,g2);
break;
default:
cout<<"invalid operator.";
}

cout<<"Answer="<<total.e<<"/"<<total.f<<endl;

cout<<"\nwould you like to continue(Enter'y'or'n')?";
cin>>val;

}
while(val!='n');

return 0;
}
fraction fadd (fraction g1,fraction g2)
{fraction total;
total.e =(g1.a*g2.b+g1.b*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fsub (fraction g1,fraction g2)
{ fraction total;
total.e=(g1.a*g2.b-g1.b*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fmul (fraction g1,fraction g2)
{fraction total;
total.e=(g1.a*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fdiv (fraction g1,fraction g2)
{fraction total;
total.e=(g1.a*g2.a);
total.f=(g1.b*g2.b);
return total;}```
to use a fraction class rather than a structure. there should be member functions for input and output, as well as for the four arithmetical operations.while you 're at it,you might as well install the capability to reduce fractions to lowest terms.here's a member function that will reduce the fraction object of which it is a member to lowest terms. it finds the greatest common divisor (gcd) of the fraction's numerator and denominator, and uses this gcd to divide both numbers.

Code:
```void fraction::lowterms()   //change ourself to lowest terms
{
long tnum ,tden,  temp,  gcd;

tnum = labs(num);   //use non-negative copies
tden = labs(den);     //(needs cmath)

if (tden==0)   //check for n/0
{cout<<"illegal fraction:division by 0";exit(1);}
else if ( tnum==0)     //check for 0/n
{num=0;den=1;return;}

// this 'while' loop finds the gcd of tnum and tden
while(tnum !=0)

{
if(tnum<tden)    //ensure numerator larger
{ temp=tnum;tnum=tden;tfen=temp;}  //swap them

tnum = tnum-tden;   // subtract them
}
gcd=tden;   // this is grestest common divisor

num=num/gcd;   //divide both num and den by gcd
den=den/gcd;       //to reduce frac to lowest terms
}```
I can call this function at the end of each arithmetic function,or just before you perfprm output. I 'll also need the usual member functions:four arithmetic operations, input,and display .I also need to find a two-argument constructor useful.

thank you
( this is the right one !!)
Last edited by malehda3y; 11-13-2005 at 07:54 PM.  Reply With Quote

2. Senior Member
Join Date
Dec 2003
Posts
3,366
Try again. I completely do not understand what you said. How does swap relate to fractions? Try to use english this time.  Reply With Quote

3. Registered User
Join Date
May 2005
Posts
29
I am sorry it was the wrong Code :rolleyes:
but I put the right one NOW sorry for that !! :)
Last edited by malehda3y; 11-14-2005 at 02:23 AM.  Reply With Quote

4. sry but i still dnt understand wat u mean :?
you are saying you send wrong codes, but you contradict it immediately by saying you have put the right one :?  Reply With Quote

5. Registered User
Join Date
May 2005
Posts
29
sorry I send wrong one than I put the right one so now it's right nothing wrong with it  Reply With Quote

6. Senior Member
Join Date
Dec 2003
Posts
3,366
class fraction
{
public:
int a; //needs better names. what is 'a' ???
int b;
int e;
int f;

fraction fmul (fraction f1,fraction f2);
};

fraction fraction::fmul (fraction f1,fraction f2)
{
fraction tmp;
tmp.numerator = ... //create a numerator field in the class, instead of a b c d etc.
//compute it here.
//return tmp

}

I am *not* going to write all these bodies with good names for you.
Once you get this much working, I will show you how to overload the operators to use
* / + - instead of fmul, fdiv, etc.  Reply With Quote

7. Registered User
Join Date
May 2005
Posts
29
I have to use the same name as it is
Thank you alot  Reply With Quote

8. Registered User
Join Date
May 2005
Posts
29
any help Plzzz :/ ????  Reply With Quote

9. Registered User
Join Date
May 2005
Posts
29 Originally Posted by jonnin
class fraction
{
public:
int a; //needs better names. what is 'a' ???
int b;
int e;
int f;

fraction fmul (fraction f1,fraction f2);
};

fraction fraction::fmul (fraction f1,fraction f2)
{
fraction tmp;
tmp.numerator = ... //create a numerator field in the class, instead of a b c d etc.
//compute it here.
//return tmp

}

I am *not* going to write all these bodies with good names for you.
Once you get this much working, I will show you how to overload the operators to use
* / + - instead of fmul, fdiv, etc.
Code:
```# include<iostream>

using namespace std;

class fraction
{
public:
int a;
int b;
int e;
int f;

fraction fadd (fraction g1,fraction g2);
fraction fsub (fraction g1,fraction g2);

fraction fmul (fraction g1 ,fraction g2 );
fraction fdiv (fraction g1,fraction g2);

};

fraction fraction::fadd (fraction g1,fraction g2)
{fraction total;
total.e =(g1.a*g2.b+g1.b*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fraction:: fsub (fraction g1,fraction g2)
{ fraction total;
total.e=(g1.a*g2.b-g1.b*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fraction:: fmul (fraction g1,fraction g2)
{fraction total;
total.e=(g1.a*g2.a);
total.f=(g1.b*g2.b);
return total;}

fraction fraction:: fdiv (fraction g1,fraction g2)
{fraction total;
total.e=(g1.a*g2.a);
total.f=(g1.b*g2.b);
return total;}

int main()
{	fraction g1,g2;
fraction total;
char op,slash,val;
do {
cout<<"Enter First Fraction:";
cin>>g1.a>>slash>>g1.b;
cout<<"\nEnter an operator:";
cin>>op;
cout<<"\nEnter Second fraction:";
cin>>g2.a>>slash>>g2.b;

switch (op)
{
case '+':
total= fadd(g1,g2);
break;
case '-':
total= fsub(g1,g2);

break;
case '*':
total= fmul(g1,g2);

break;
case '/':
total= fdiv(g1,g2);
break;
default:
cout<<"invalid operator.";
}

cout<<"Answer="<<total.e<<"/"<<total.f<<endl;

cout<<"\nwould you like to continue(Enter'y'or'n')?";
cin>>val;

}
while(val!='n');

return 0;
}```
can you plzz tell me how to overload the operators to use
* / + - instead of fmul, fdiv, etc.???
because I can't change the name my teacher choose this names

Thank you :)
Last edited by malehda3y; 11-15-2005 at 11:19 AM.  Reply With Quote

10. Senior Member
Join Date
Dec 2003
Posts
3,366
fraction operator + (fraction &AddToMe); //header inside class

fraction fraction::operator + (fraction AddToMe)
{
fraction result = this + AddToMe; //add properly here, this is wrong
return result;
}

And I still don't know what 'a' is.  Reply With Quote

11. Registered User
Join Date
May 2005
Posts
29
a is what user enter

I did the program and I think it's right

I did like this

Code:
```# include<iostream>

using namespace std;

class fraction
{
private:
int a;
int b;
public:
void get_fraction()

{char slash;

cout<<"Enter Fraction:";
cin>>a>>slash>>b;};
void print_fraction()
{cout<<"Answer="<<a<<"/"<<b<<endl;

};

fraction fadd (fraction g1,fraction g2)
{fraction total;
total.a =(g1.a*g2.b+g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;}

fraction fsub (fraction g1,fraction g2)
{ fraction total;
total.a=(g1.a*g2.b-g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;}

fraction fmul (fraction g1,fraction g2)
{fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;}

fraction fdiv (fraction g1,fraction g2)
{fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;}
void lowterms()
{
long tnum,tden,temp,gcd;
tnum =labs(a);
tden =labs(b);
if(tden==0 )
{ cout<<"illegal fraction: division by 0";exit(1); }
else if (tnum==0)
{ a=0; b=1;return; }
while(tnum !=0)
{
if(tnum<tden)
{temp=tnum;tnum=tden; tden=temp;}
tnum=tnum-tden;
}
gcd=tden;
a=a/gcd;
b=b/gcd;
}
};

int main()
{	fraction g1,g2;
fraction total;
char op,slash,val;

do {
g1.get_fraction();
cout<<"\nEnter an operator:";
cin>>op;
g2.get_fraction();

switch (op)
{
case '+':
total= g1.fadd(g1,g2);
total.print_fraction();
break;
case '-':
total= g1.fsub(g1,g2);
total.print_fraction();
break;
case '*':
total= g1.fmul(g1,g2);
total.print_fraction();
break;
case '/':
total= g1.fdiv(g1,g2);
total.print_fraction();
break;
default:
cout<<"invalid operator.";
}

cout<<"\nwould you like to continue(Enter'y'or'n')?";
cin>>val;

}
while(val!='n');

return 0;
}```

thank you :)
Last edited by malehda3y; 12-06-2005 at 09:22 PM.  Reply With Quote

12. Registered User
Join Date
Oct 2005
Posts
32
Check your Private Messages  Reply With Quote

13. Senior Member
Join Date
Dec 2003
Posts
3,366
A once over read -- it looks ok, test it and see if it works. If it were MY fraction class, I would also have the double a/b inside it so you can use the number the fraction represents. I also would not crash out on a divide by zero, but instead simply print a message and return zero or infinity. Note that terminate() is better than exit(whatever) in C++.  Reply With Quote

14. Registered User
Join Date
May 2005
Posts
29
Thank you jonnin and Mansur I did it right :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•