fraction class [was:need help plzz]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: fraction class [was:need help plzz]

  1. #1
    Join Date
    May 2005
    Posts
    29

    Unhappy 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.

  2. #2
    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.

  3. #3
    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 01:23 AM.

  4. #4
    Join Date
    Sep 2005
    Posts
    32
    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 :?

  5. #5
    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

  6. #6
    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.

  7. #7
    Join Date
    May 2005
    Posts
    29
    I have to use the same name as it is
    Thank you alot

  8. #8
    Join Date
    May 2005
    Posts
    29
    any help Plzzz :/ ????

  9. #9
    Join Date
    May 2005
    Posts
    29
    Quote 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.

  10. #10
    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.

  11. #11
    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 08:22 PM.

  12. #12
    Join Date
    Oct 2005
    Posts
    32
    Check your Private Messages

  13. #13
    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++.

  14. #14
    Join Date
    May 2005
    Posts
    29
    Thank you jonnin and Mansur I did it right :)

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 PM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  5. Calling base class methods
    By GR in forum Java
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center