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.
Expand|Select|Wrap|Line Numbers
- #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();
- }