-
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 06:54 PM.
-
Try again. I completely do not understand what you said. How does swap relate to fractions? Try to use english this time.
-
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 01:23 AM.
-
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 :?
-
sorry I send wrong one than I put the right one so now it's right nothing wrong with it
-
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.
-
I have to use the same name as it is
Thank you alot
-
-
 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 10:19 AM.
-
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.
-
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 08:22 PM.
-
Check your Private Messages
-
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++.
-
Thank you jonnin and Mansur I did it right :)
Similar Threads
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 07:53 PM
-
By Patrick Ireland in forum .NET
Replies: 5
Last Post: 05-10-2001, 06:19 PM
-
Replies: 1
Last Post: 11-09-2000, 05:38 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks