473,782 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rational Numbers question

3 New Member
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 4626
Ganon11
3,652 Recognized Expert Specialist
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
anumliaqat
3 New Member
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 Recognized Expert MVP
@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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
2423
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 Content-Type: text/x-rst Created: 16-Dec-2004 Python-Version: 2.5
20
2148
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 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org>
2
2803
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 script to convert numbers to arbitrary bases. It aims to take any of whole numbers (python ints, longs, or Decimals), rational numbers (n / m n, m whole) and floating points (the best I can do for reals), and convert them to any base between 2 and...
6
2745
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 rational number class from the Boost libararies. Is there anything out there like that or for that matter in the framework that I am missing. Cheers
1
3485
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 numerator and denumerator as integers. Structures like this can be used; struct Rational{int a, int b}; to represent a number a/b. Declaration to include Typedef struck Rational Rational; It should include only operation of addition and...
6
4648
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 real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications. In this...
1
2861
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 real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications. In this...
135
4324
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.