Rational Numbers question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anumliaqat
    New Member
    • Oct 2008
    • 3

    Rational Numbers question

    hello!!
    i hav a problem.my program is giving correct outputs for some inputs but wrong results for others.
    for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct.
    but for (2 2 4)&(2 1 4) it gives wrong outputs.
    now i am unable to find which function is to be corrected and which one not.
    plz plz help me i have to submit it on 01 dec.



    Code:
    #include<iostream.h>
    #include<conio.h>
    
    
    enum bool { false,true};
    
    class Rational{
     int num;//numerator
     int denom;//denomenator
     int whl;//whole number
     public:
     Rational();//constructor
     Rational& setRational (int a,int b, int c);//setting the rational numbers
     void setNumerator(int);
     void setDenominator(int);
     void setwholenumber(int);
     int getNumerator();
     int getDenominator();
     int getwholenumber();
     Rational operator + (Rational &r1);//addition operator overloaded
     Rational operator - (Rational & r1);//subtraction operator overloaded
     Rational operator * (Rational & r1);//multiply operator overloaded
     Rational operator / (Rational & r1) ;//division operator overloaded
     bool operator > (Rational & r1);//greater then operator overloaded
     bool operator <     (Rational & r1);//less the operator overloaded
     bool operator >= (Rational & r1);//greater then equal to overloaded
     bool operator <= ( Rational & r1);//less then equal to overloaded
     bool operator != (Rational & r1);//not equality operator overloaded
     Rational  operator ++ ();//increment operator overloaded
     Rational& operator -- ();//decrement operator overloaded
     bool operator == (Rational & r1);//equality operator overloaded
     Rational & operator += (Rational & r1);//+=overloaded
     friend istream & operator >> (istream & input, Rational  & r1);//friend function for input
     friend ostream & operator << (ostream & output, Rational & r1);//friend function for output
     Rational &simplifyRational();//simplification
     ~Rational();
     void print()
     {
     cout<<whl<<" "<<num<<" "<<denom<<endl;
     }
     };
    
    
    /*********************************************************************************
    ****************************constructor*****************************************************/
       Rational::Rational()
       {
        whl=0;
        num=0;
        denom=1;
       }
    /*****************************getters and setters****************************************************/
    
        void Rational::setNumerator(int n)
        {
         num = n;
        }
    
        void Rational::setDenominator(int d)
        {
         denom = d;
        }
        void Rational::setwholenumber(int w)
        {
         whl = w;
        }
        int Rational::getNumerator()
        {
         return num;
         }
    
        int Rational::getDenominator()
        {
         return denom;
         }
        int Rational::getwholenumber()
        {
         return whl;
         }
    
    
       Rational& Rational::setRational(int a,int b,int c)
        {
         this->whl=a;
         this->num=b;
         if (c==0)
         this->denom=1;
         else
         {
          this->denom=c;
          this->num=(denom*whl)+num;
         // this->whl=num/denom;
          this->denom=denom;
          }
         return *this;
        }
    /****************************arithmatic operators****************************************************/
    
        Rational Rational:: operator + (Rational &r1)
        {
         Rational temp;
         temp.num=(num*r1.denom)+(denom*r1.num);
         temp.denom=denom*r1.denom;
         temp.simplifyRational();
          return temp;
         }
        Rational Rational:: operator - (Rational & r1)
         {
          Rational temp;
          temp.num=num*r1.denom-denom*r1.num;
          temp.denom=denom*r1.denom;
          temp.simplifyRational();
          return temp;
         }
        Rational Rational::operator * (Rational & r1)
        {
         Rational temp;
         temp.num=num*r1.num;
         temp.denom=denom*r1.denom;
         temp.simplifyRational();
         return temp;
        }
    
        Rational Rational::operator / (Rational & r1)
        {
         Rational temp;
         temp.num= num*r1.denom;
         temp.denom=denom*r1.num;
         temp.simplifyRational();
         return temp;
        }
    /**************************comparison operators****************************************************/
    
       bool Rational:: operator > (Rational & r1)
       {
        if  (num/denom > r1.num/r1.denom)
         return true;
         else
         return false;
       }
    
       bool Rational:: operator <     (Rational & r1)
       {
        if (num/denom < r1.num/r1.denom)
        return true;
        else
        return false;
        }
    
        bool Rational:: operator >= (Rational & r1)
        {
         if ((num/denom > r1.num/r1.denom)  ||  ((num==r1.num) && ( denom==r1.denom)))
          return true;
         else
          return false;
         }
    
         bool Rational:: operator <= ( Rational & r1)
        {
         if (( num/denom < r1.num/r1.denom)|| ((num==r1.num)&&(denom==r1.denom)))
         return true;
         else
         return false;
        }
         bool Rational:: operator != (Rational & r1)
        {
          if ((num!=r1.num)&&(denom!=r1.denom))
          return true;
          else
          return false;
        }
    /****************************increment & decrement operator*******************************************/
         Rational Rational::operator ++ ()
         {
          this->num=num+denom;
          this->denom=denom;
          return *this;
         }
          Rational& Rational:: operator -- ()
         {
          this->num=num-denom;
          this->denom=denom;
          return *this;
         }
    /******************************assignment operator*******************************************/
       bool Rational:: operator == (Rational & r1)
       {
        if ((num==r1.num)&&(denom==r1.denom))
         return true;
        else
         return false;
       }
    /*********************************************************************************************/
       Rational& Rational:: operator += (Rational & r1)
       {
        this->num=(num*r1.denom)+(r1.num*denom);
        this->denom=denom*r1.denom;
        return *this;
       }
    /*********************************insertion & extraction operator*****************************************/
        istream & operator >> (istream & input, Rational  & r1)
        {
         int num1,num2,num3;
         input>>num1>>num2>>num3;
         r1.setRational(num1,num2,num3);
         return input;
        }
    
        ostream & operator << (ostream & output, Rational & r1)
        {
         //r1.whl=r1.num/r1.denom;
         //r1.num=r1.num%r1.denom;
         output<<"  "<<r1.num<<"/"<<r1.denom;
         return output;
        }
    
    
    /*************************
    **********simplify function***************************************************/
          Rational& Rational::simplifyRational()
          {
           int largest,gcd;
           if(num>denom)
          { largest=num/2;
    	 for(int i=1;i<=largest;i++)
    
    	 if ((num%i==0)&&(denom%i==0))
    	 { gcd=i;
    	 this->num=num/gcd;
    	 this->denom=denom/gcd;
    	 }
    	 }
           else
          { largest=denom/2;
    	 for(int i=1;i<=largest;i++)
    	 {
    	 if ((num%i==0)&&(denom%i==0))
    	  gcd=i;
    	 this->num=num/gcd;
    	 this->denom=denom/gcd;
    	 }
          }
    	return *this;
         }
    
    /**********************************destructor**********************************************/
         Rational::~Rational()
         {
         }
    /****************************************************************************
    ******************************************************************************/
    
     class RatUI{
     Rational r1,r2;
     public:
    
     void showmenu()
     {
      int opt;
      char opt1;
       clrscr();
    
         cout<<"Enter two rational no.s.first enter whole part,then numerator and after that denominator."<<endl;
          cin>>r1>>r2;
       do
       {
          clrscr();
          cout<<"1:  To sum two rational numbers"<<endl;
          cout<<"2:  To subtract two rational numbers"<<endl;
          cout<<"3:  to multiply two rational numbers"<<endl;
          cout<<"4:  to divide two rational numbers"<<endl;
          cout<<"5:  to compare two rational numbers by '>'"<<endl;
          cout<<"6:  to compare two rational numbers by '<'"<<endl;
          cout<<"7:  to compare two rational numbers by '<='"<<endl;
          cout<<"8:  to compare two rational numbers by '>='"<<endl;
          cout<<"9:  to copmare two rational numbers by '!='"<<endl;
          cout<<"10: to increment first rational number"<<endl;
          cout<<"11: to increment second rational number"<<endl;
          cout<<"12: to decrement first rational number"<<endl;
          cout<<"13: to decrement second rational number"<<endl;
          cin>>opt;
          switch(opt)
    	{
    	  case 1:
    	  {
    	  cout<<r1+r2;
    	  } break;
    	  case 2:
    	  {
    	  cout<<r1-r2;
    	  }break;
    	  case 3:
    	  {
    	  cout<<r1*r2;
    	  }break;
    	  case 4:
    	  {
    	  cout<<r1/r2;
    	  }break;
    	  case 5:
    	  {
    	  if(r1>r2)
    	  cout<<"true"<<endl;
    	  else
    	  cout<<"false"<<endl;
    	  }break;
    	  case 6:
    	  {
    	  if(r1<r2)
    	  cout<<"true"<<endl;
    	  else
    	  cout<<"false"<<endl;
    	  }break;
    	  case 7:
    	  {
    	  if(r1<=r2)
    	  cout<<"true"<<endl;
    	  else
    	  cout<<"false"<<endl;
    	  }break;
    	  case 8:
    	  {
    	  if(r1>=r2)
    	  cout<<"true"<<endl;
    	  else
    	  cout<<"false"<<endl;
    	  }break;
    	  case 9:
    	  {
    	  if(r1!=r2)
    	  cout<<"true"<<endl;
    	  else
    	  cout<<"false"<<endl;
    	  }break;
    	  case 10:cout<<++r1;break;
    	  case 11:cout<<++r2;break;
    	  case 12:cout<<--r1;break;
    	  case 13:cout<<--r2;break;
    	  default:
    	  break;
    	}
    	cout<<endl<<"do you want to continue"
    	    <<endl<<"press 'n'to terminate"
    	    <<endl<<"press any alphabet to continue."<<endl;
    	cin>>opt1;
      }
    
         while(opt1!='n');
    
    }
    
    
    
    
    
     };
    
    
      void  main()//main driver program
      {
       RatUI r;
       r.showmenu();
      }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What is your program supposed to do given those inputs? How exactly is it 'not working'? We'll need a little more specific information as opposed to tons of code in order to solve this.

    Comment

    • anumliaqat
      New Member
      • Oct 2008
      • 3

      #3
      It gives correct answwers to multiply subtract and divide two rational numbers for some inputs.but at the same time give wrong results for others.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        @OP: I changed your thread title; telling us that your question is urgent to you doesn't make your question more important to us. You might as well have named your thread "I have to pee". Please give your threads meaningful titles.

        kind regards,

        Jos

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          None of your calculations take into account the whole number part of your rational number.

          Additionally since you are doing integer division it is unlikely that any of you comparison functions will work, they will all compare 0 with 0 (resulting in always true or always false depending on the comparison).

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Also, you don't ever divide rational numbers using the / operator.

            Rational number division consists of multiplying the divisor by the inverse of the dividend. That is, the numerator of the dividend is multiplied by the denominator is the divisor and the denominator of the dividend is multiplied by the numerator of the divisor.

            Comment

            Working...