473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.



Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream.h>
  3. #include<conio.h>
  4.  
  5.  
  6. enum bool { false,true};
  7.  
  8. class Rational{
  9.  int num;//numerator
  10.  int denom;//denomenator
  11.  int whl;//whole number
  12.  public:
  13.  Rational();//constructor
  14.  Rational& setRational (int a,int b, int c);//setting the rational numbers
  15.  void setNumerator(int);
  16.  void setDenominator(int);
  17.  void setwholenumber(int);
  18.  int getNumerator();
  19.  int getDenominator();
  20.  int getwholenumber();
  21.  Rational operator + (Rational &r1);//addition operator overloaded
  22.  Rational operator - (Rational & r1);//subtraction operator overloaded
  23.  Rational operator * (Rational & r1);//multiply operator overloaded
  24.  Rational operator / (Rational & r1) ;//division operator overloaded
  25.  bool operator > (Rational & r1);//greater then operator overloaded
  26.  bool operator <     (Rational & r1);//less the operator overloaded
  27.  bool operator >= (Rational & r1);//greater then equal to overloaded
  28.  bool operator <= ( Rational & r1);//less then equal to overloaded
  29.  bool operator != (Rational & r1);//not equality operator overloaded
  30.  Rational  operator ++ ();//increment operator overloaded
  31.  Rational& operator -- ();//decrement operator overloaded
  32.  bool operator == (Rational & r1);//equality operator overloaded
  33.  Rational & operator += (Rational & r1);//+=overloaded
  34.  friend istream & operator >> (istream & input, Rational  & r1);//friend function for input
  35.  friend ostream & operator << (ostream & output, Rational & r1);//friend function for output
  36.  Rational &simplifyRational();//simplification
  37.  ~Rational();
  38.  void print()
  39.  {
  40.  cout<<whl<<" "<<num<<" "<<denom<<endl;
  41.  }
  42.  };
  43.  
  44.  
  45. /*********************************************************************************
  46. ****************************constructor*****************************************************/
  47.    Rational::Rational()
  48.    {
  49.     whl=0;
  50.     num=0;
  51.     denom=1;
  52.    }
  53. /*****************************getters and setters****************************************************/
  54.  
  55.     void Rational::setNumerator(int n)
  56.     {
  57.      num = n;
  58.     }
  59.  
  60.     void Rational::setDenominator(int d)
  61.     {
  62.      denom = d;
  63.     }
  64.     void Rational::setwholenumber(int w)
  65.     {
  66.      whl = w;
  67.     }
  68.     int Rational::getNumerator()
  69.     {
  70.      return num;
  71.      }
  72.  
  73.     int Rational::getDenominator()
  74.     {
  75.      return denom;
  76.      }
  77.     int Rational::getwholenumber()
  78.     {
  79.      return whl;
  80.      }
  81.  
  82.  
  83.    Rational& Rational::setRational(int a,int b,int c)
  84.     {
  85.      this->whl=a;
  86.      this->num=b;
  87.      if (c==0)
  88.      this->denom=1;
  89.      else
  90.      {
  91.       this->denom=c;
  92.       this->num=(denom*whl)+num;
  93.      // this->whl=num/denom;
  94.       this->denom=denom;
  95.       }
  96.      return *this;
  97.     }
  98. /****************************arithmatic operators****************************************************/
  99.  
  100.     Rational Rational:: operator + (Rational &r1)
  101.     {
  102.      Rational temp;
  103.      temp.num=(num*r1.denom)+(denom*r1.num);
  104.      temp.denom=denom*r1.denom;
  105.      temp.simplifyRational();
  106.       return temp;
  107.      }
  108.     Rational Rational:: operator - (Rational & r1)
  109.      {
  110.       Rational temp;
  111.       temp.num=num*r1.denom-denom*r1.num;
  112.       temp.denom=denom*r1.denom;
  113.       temp.simplifyRational();
  114.       return temp;
  115.      }
  116.     Rational Rational::operator * (Rational & r1)
  117.     {
  118.      Rational temp;
  119.      temp.num=num*r1.num;
  120.      temp.denom=denom*r1.denom;
  121.      temp.simplifyRational();
  122.      return temp;
  123.     }
  124.  
  125.     Rational Rational::operator / (Rational & r1)
  126.     {
  127.      Rational temp;
  128.      temp.num= num*r1.denom;
  129.      temp.denom=denom*r1.num;
  130.      temp.simplifyRational();
  131.      return temp;
  132.     }
  133. /**************************comparison operators****************************************************/
  134.  
  135.    bool Rational:: operator > (Rational & r1)
  136.    {
  137.     if  (num/denom > r1.num/r1.denom)
  138.      return true;
  139.      else
  140.      return false;
  141.    }
  142.  
  143.    bool Rational:: operator <     (Rational & r1)
  144.    {
  145.     if (num/denom < r1.num/r1.denom)
  146.     return true;
  147.     else
  148.     return false;
  149.     }
  150.  
  151.     bool Rational:: operator >= (Rational & r1)
  152.     {
  153.      if ((num/denom > r1.num/r1.denom)  ||  ((num==r1.num) && ( denom==r1.denom)))
  154.       return true;
  155.      else
  156.       return false;
  157.      }
  158.  
  159.      bool Rational:: operator <= ( Rational & r1)
  160.     {
  161.      if (( num/denom < r1.num/r1.denom)|| ((num==r1.num)&&(denom==r1.denom)))
  162.      return true;
  163.      else
  164.      return false;
  165.     }
  166.      bool Rational:: operator != (Rational & r1)
  167.     {
  168.       if ((num!=r1.num)&&(denom!=r1.denom))
  169.       return true;
  170.       else
  171.       return false;
  172.     }
  173. /****************************increment & decrement operator*******************************************/
  174.      Rational Rational::operator ++ ()
  175.      {
  176.       this->num=num+denom;
  177.       this->denom=denom;
  178.       return *this;
  179.      }
  180.       Rational& Rational:: operator -- ()
  181.      {
  182.       this->num=num-denom;
  183.       this->denom=denom;
  184.       return *this;
  185.      }
  186. /******************************assignment operator*******************************************/
  187.    bool Rational:: operator == (Rational & r1)
  188.    {
  189.     if ((num==r1.num)&&(denom==r1.denom))
  190.      return true;
  191.     else
  192.      return false;
  193.    }
  194. /*********************************************************************************************/
  195.    Rational& Rational:: operator += (Rational & r1)
  196.    {
  197.     this->num=(num*r1.denom)+(r1.num*denom);
  198.     this->denom=denom*r1.denom;
  199.     return *this;
  200.    }
  201. /*********************************insertion & extraction operator*****************************************/
  202.     istream & operator >> (istream & input, Rational  & r1)
  203.     {
  204.      int num1,num2,num3;
  205.      input>>num1>>num2>>num3;
  206.      r1.setRational(num1,num2,num3);
  207.      return input;
  208.     }
  209.  
  210.     ostream & operator << (ostream & output, Rational & r1)
  211.     {
  212.      //r1.whl=r1.num/r1.denom;
  213.      //r1.num=r1.num%r1.denom;
  214.      output<<"  "<<r1.num<<"/"<<r1.denom;
  215.      return output;
  216.     }
  217.  
  218.  
  219. /*************************
  220. **********simplify function***************************************************/
  221.       Rational& Rational::simplifyRational()
  222.       {
  223.        int largest,gcd;
  224.        if(num>denom)
  225.       { largest=num/2;
  226.      for(int i=1;i<=largest;i++)
  227.  
  228.      if ((num%i==0)&&(denom%i==0))
  229.      { gcd=i;
  230.      this->num=num/gcd;
  231.      this->denom=denom/gcd;
  232.      }
  233.      }
  234.        else
  235.       { largest=denom/2;
  236.      for(int i=1;i<=largest;i++)
  237.      {
  238.      if ((num%i==0)&&(denom%i==0))
  239.       gcd=i;
  240.      this->num=num/gcd;
  241.      this->denom=denom/gcd;
  242.      }
  243.       }
  244.     return *this;
  245.      }
  246.  
  247. /**********************************destructor**********************************************/
  248.      Rational::~Rational()
  249.      {
  250.      }
  251. /****************************************************************************
  252. ******************************************************************************/
  253.  
  254.  class RatUI{
  255.  Rational r1,r2;
  256.  public:
  257.  
  258.  void showmenu()
  259.  {
  260.   int opt;
  261.   char opt1;
  262.    clrscr();
  263.  
  264.      cout<<"Enter two rational no.s.first enter whole part,then numerator and after that denominator."<<endl;
  265.       cin>>r1>>r2;
  266.    do
  267.    {
  268.       clrscr();
  269.       cout<<"1:  To sum two rational numbers"<<endl;
  270.       cout<<"2:  To subtract two rational numbers"<<endl;
  271.       cout<<"3:  to multiply two rational numbers"<<endl;
  272.       cout<<"4:  to divide two rational numbers"<<endl;
  273.       cout<<"5:  to compare two rational numbers by '>'"<<endl;
  274.       cout<<"6:  to compare two rational numbers by '<'"<<endl;
  275.       cout<<"7:  to compare two rational numbers by '<='"<<endl;
  276.       cout<<"8:  to compare two rational numbers by '>='"<<endl;
  277.       cout<<"9:  to copmare two rational numbers by '!='"<<endl;
  278.       cout<<"10: to increment first rational number"<<endl;
  279.       cout<<"11: to increment second rational number"<<endl;
  280.       cout<<"12: to decrement first rational number"<<endl;
  281.       cout<<"13: to decrement second rational number"<<endl;
  282.       cin>>opt;
  283.       switch(opt)
  284.     {
  285.       case 1:
  286.       {
  287.       cout<<r1+r2;
  288.       } break;
  289.       case 2:
  290.       {
  291.       cout<<r1-r2;
  292.       }break;
  293.       case 3:
  294.       {
  295.       cout<<r1*r2;
  296.       }break;
  297.       case 4:
  298.       {
  299.       cout<<r1/r2;
  300.       }break;
  301.       case 5:
  302.       {
  303.       if(r1>r2)
  304.       cout<<"true"<<endl;
  305.       else
  306.       cout<<"false"<<endl;
  307.       }break;
  308.       case 6:
  309.       {
  310.       if(r1<r2)
  311.       cout<<"true"<<endl;
  312.       else
  313.       cout<<"false"<<endl;
  314.       }break;
  315.       case 7:
  316.       {
  317.       if(r1<=r2)
  318.       cout<<"true"<<endl;
  319.       else
  320.       cout<<"false"<<endl;
  321.       }break;
  322.       case 8:
  323.       {
  324.       if(r1>=r2)
  325.       cout<<"true"<<endl;
  326.       else
  327.       cout<<"false"<<endl;
  328.       }break;
  329.       case 9:
  330.       {
  331.       if(r1!=r2)
  332.       cout<<"true"<<endl;
  333.       else
  334.       cout<<"false"<<endl;
  335.       }break;
  336.       case 10:cout<<++r1;break;
  337.       case 11:cout<<++r2;break;
  338.       case 12:cout<<--r1;break;
  339.       case 13:cout<<--r2;break;
  340.       default:
  341.       break;
  342.     }
  343.     cout<<endl<<"do you want to continue"
  344.         <<endl<<"press 'n'to terminate"
  345.         <<endl<<"press any alphabet to continue."<<endl;
  346.     cin>>opt1;
  347.   }
  348.  
  349.      while(opt1!='n');
  350.  
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  };
  358.  
  359.  
  360.   void  main()//main driver program
  361.   {
  362.    RatUI r;
  363.    r.showmenu();
  364.   }
  365.  
  366.  
Nov 30 '08 #1
5 4602
Ganon11
3,652 Expert 2GB
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.
Dec 1 '08 #2
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.
Dec 1 '08 #3
JosAH
11,448 Expert 8TB
@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
Dec 1 '08 #4
Banfa
9,065 Expert Mod 8TB
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).
Dec 1 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
20
by: Mike Meyer | last post by:
This version includes the input from various and sundry people. Thanks to everyone who contributed. <mike PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
6
by: Mike Friel | last post by:
I am writing an assembly that will be used in a suite of financial applcations. Naturally I must use integer arithmetic to ensure the accuracy of my calculations and in the past have used the...
1
by: lordhavemercy | last post by:
Rational Arithmetic I need a possible representstion of Rational numbers in C using Structures. It has to be a foating-point representatiobn. Each rational number has to represent/store the...
6
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
1
by: ben kipkorir | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.