Rational number excercise need guidance

This is a discussion on Rational number excercise need guidance within the C++ Programming forums, part of the General Programming Boards category; Been working on this rational number program. Here is what the program is suppose to do. To me this is ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    Rational number excercise need guidance

    Been working on this rational number program.

    Here is what the program is suppose to do. To me this is one serious program using classes. My problem is when i get to the I , M , R, G part how do i build something that will take input of that and output what is in this chart. Once again the book gives mickey mouse examples and then the excercises are not even close to them. I am hoping someone can help me out with what to do.

    Second question is how could i make this program better by having it read all of these inputs and perfrom the right operations. As you can see i have been manually testing the classes by changing the operator and entering in the numbers directly in the program. When i tried to tie a user input into the program it wouldnt let me. Compiler errors.

    Input ---------------------------- Output
    3/8 + 1/6--------------------------13/24
    3/8 - 1/6---------------------------5/24
    3/8 * 1/6--------------------------1/16
    3/8 / 1/6---------------------------9/4
    3/8 I -------------------------------8/3 ---------------invert a/b
    8/3 M ----------------------------- 2 + 2/3-----------Write a/b as a mixed fraction
    6/8 R ------------------------------3/4 ---------------reduce a/b to lowest terms
    6/8 G ------------------------------2------------------Greatest common divisor of num ..
    1/6 L 3/8-------------------------24 ------------- Lowest common denom of a/b and c/d
    1/6 < 3/8------------------------true------------a/b < c/d
    1/6 < = 3/8 -------------------- true------------a/b <= c/d
    1/6 > 3/8 -----------------------false------------a/b > c/d
    1/6 >= 3/8----------------------false -----------a/b >= c/d
    3/8 = 9/24----------------------true-------------a/b = c/d
    2/3 X + 2 = 4/5--------------X = -9/5--solution of linear equation (a/b)X = c/d = e/f

    Here is what i have so far:

    Code:
    #include<iostream>
    
    using namespace std;
    
    
    
    
    int gcd (int n1, int n2)  //greatest common divisor
    {
    	int rem;
        while (n2 != 0)
        {
    		rem = n1 % n2;
            n1 = n2;
            n2 = rem;
    	}
        return (n1);
    }
    
    int lcm(int n1, int n2)  // least common multiple 
    {
    	return(n1*n2/gcd(n1,n2));   // because a*b=lcm(a,b)*gcd(a,b)
    }
    
    class rational
    {
    public: 
    	int a, b;   // a - numerator,  b - denominator
    public: 
    	rational(){}
    	rational(int m, int n)
    	{
    		if (n!=0)
    		{
    			a=m;b=n;
    		}
    		else
    		{
    			cout << "Error defining rational" << endl;
    			exit(0);
    		}
    	}
    	
    	void disp(void)
    	{
    		int temp=gcd(a,b);
    		a=a/temp;
    		b=b/temp;
    		if (b!=1) 
    		{
    			
    			cout << a << "/" << b;
    		}
    		else
    			cout << a;
    	}
    };
    
    rational operator +( rational p, rational  q)
    {
    	int temp=lcm(p.b,q.b);
    	rational temp1(p.a*(temp/p.b)+q.a*(temp/q.b),temp);
    	return temp1;
    }
    rational operator -(rational p, rational q)
    {
    	rational temp(-q.a,q.b);
        return (p+temp);
    }
    rational operator *(rational p, rational q)
    {
    	rational temp(p.a*q.a,p.b*q.b);
    	return temp;
    }
    rational operator /(rational p, rational q)
    {
    	rational temp(p.a*q.b,p.b*q.a);
    	return temp;
    }
    
    
    
    void main()
    {
    	rational a(3,8);
    	rational b(1,6);
    	rational temp=a+b;
    	temp.disp();
    	cout << "\n\n";
    	
    
    	
    
    
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    We wont know how to help if you don't post the error the compiler is giving you, and the faulty line of code.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    No errors , it compiles perfectly right now. I am asking questions in original post, need help in making the I - G parts work in the program..... building classes

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Is this homework?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe...
    Code:
    class rational
    {
       // ...
       rational I()
       {
          int temp = a;
          a = b;
          b = temp;
       }
    };
    Code:
       a.I();
       a.disp();
       cout << "\n\n";
    ...for example.

    Or...
    Code:
    rational I(const rational &r)
    {
       return rational(r.b, r.a);
    }
    Code:
       temp = I(a);
       temp.disp();
       cout << "\n\n";
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21