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. -
-
#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();
-
}
-
-
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.
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.
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
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).
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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>
|
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...
|
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
|
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...
| |
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...
|
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...
|
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
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
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
| |