Here i made a class rational to add, subtract, multiply and divide 2 rational numbers. But data members not able to take the values through constructor. I don’t know what’s the problem. I am sharing my code with you. Here my code is long if a very few lines of code can be made, so please tel me.
#include
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class Rational
{
int num;
int den;
public:
Rational(int a = 0, int b = 0);
void add();
void sub();
void multiply();
void divide();
int getnum()
{
return num;
}
int getden()
{
return den;
}
};
Rational r1, r2, r3;
int main()
{
int ch, num, den, num1, den1;
while(1)
{
cout<<"\nMAIN MENU";
cout<<"\n1.ADD 2 RATIONAL NUMBERS";
cout<<"\n2.SUBTRACT 2 RATIONAL NUMBERS";
cout<<"\n3.MULTIPLY 2 RATIONAL NUMBERS";
cout<<"\n4.DIVIDE 2 RATIONAL NUMBERS";
cout<<"\n5.EXIT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nADDING 2 RATIONAL NUMBERS";
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 1ST NUMBER : ";
cin>>num>>den;
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 2ND NUMBER : ";
cin>>num1>>den1;
Rational r1(num, den);
Rational r2(num1, den1);
r3.add();
}
break;
case 2:
{
cout<<"\nADIING 2 RATIONAL NUMBERS";
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 1ST NUMBER : ";
cin>>num>>den;
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 2ND NUMBER : ";
cin>>num1>>den1;
Rational r1(num, den);
Rational r2(num1, den1);
r3.sub();
}
break;
case 3:
{
cout<<"\nADIING 2 RATIONAL NUMBERS";
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 1ST NUMBER : ";
cin>>num>>den;
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 2ND NUMBER : ";
cin>>num1>>den1;
Rational r1(num, den);
Rational r2(num1, den1);
r3.multiply();
}
break;
case 4:
{
cout<<"\nADIING 2 RATIONAL NUMBERS";
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 1ST NUMBER : ";
cin>>num>>den;
cout<<"\nENTER NUMERATOR AND DENOMINATOR OF 2ND NUMBER : ";
cin>>num1>>den1;
Rational r1(num, den);
Rational r2(num1, den1);
r3.divide();
}
break;
case 5:
exit(0);
default:
cout<<"\nWRONG CHOICE ENTERED!!!";
break;
}
}
}
Rational :: Rational(int a, int b)
: num(a),
den(b)
{
}
void Rational :: add()
{
int i, a, b, ch;
float c, d;
a = r1.getnum() * r2.getden() + r2.getnum() * r1.getden();
b = r1.getden() + r2.getden();
cout<<"\na = "<<a<<"\nb = "<<b;
if(b > a)
{
for(i = a; i > 1; i--)
{
if((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
}
else if(a >= b)
{
for(i = b; i > 1; i--)
{
if((b % i == 0) && (a % i == 0))
{
a /= i;
b /= i;
}
}
}
cout<<"\n1.PRINT RATIONAL NUMBER IN FORM OF a/b";
cout<<"\n2.PRINT RATIONAL NUMBER IN FLOATING POINT FORMAT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>ch;
if(ch == 1)
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<a<<"/"<<b;
else if(ch == 2)
{
c = a;
d = b;
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<c/d;
}
}
void Rational :: sub()
{
int i, a, b, ch;
float c, d;
a = r1.getnum() * r2.getden() - r2.getnum() * r1.getden();
b = r1.getden() * r2.getden();
if(b > a)
{
for(i = a; i > 1; i--)
{
if((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
}
else if(a >= b)
{
for(i = b; i > 1; i--)
{
if((b % i == 0) && (a % i == 0))
{
a /= i;
b /= i;
}
}
}
cout<<"\n1.PRINT RATIONAL NUMBER IN FORM OF a/b";
cout<<"\n2.PRINT RATIONAL NUMBER IN FLOATING POINT FORMAT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>ch;
if(ch == 1)
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<a<<"/"<<b;
else if(ch == 2)
{
c = a;
d = b;
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<c/d;
}
}
void Rational :: multiply()
{
int i, a, b, ch;
float c, d;
a = r1.getnum() * r2.getden();
b = r1.getden() * r2.getnum();
if(b > a)
{
for(i = a; i > 1; i--)
{
if((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
}
else if(a >= b)
{
for(i = b; i > 1; i--)
{
if((b % i == 0) && (a % i == 0))
{
a /= i;
b /= i;
}
}
}
cout<<"\n1.PRINT RATIONAL NUMBER IN FORM OF a/b";
cout<<"\n2.PRINT RATIONAL NUMBER IN FLOATING POINT FORMAT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>ch;
if(ch == 1)
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<a<<"/"<<b;
else if(ch == 2)
{
c = a;
d = b;
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<c/d;
}
}
void Rational :: divide()
{
int i, a, b, ch;
float c, d;
a = r1.getnum() * r2.getden() - r2.getnum() * r1.getden();
b = r1.getden() * r2.getden();
if(b > a)
{
for(i = a; i > 1; i--)
{
if((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
}
else if(a >= b)
{
for(i = b; i > 1; i--)
{
if((b % i == 0) && (a % i == 0))
{
a /= i;
b /= i;
}
}
}
cout<<"\n1.PRINT RATIONAL NUMBER IN FORM OF a/b";
cout<<"\n2.PRINT RATIONAL NUMBER IN FLOATING POINT FORMAT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>ch;
if(ch == 1)
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<a<<"/"<<b;
else if(ch == 2)
{
c = a;
d = b;
cout<<"\nRATIONAL NUMBER IN THE FORM OF : "<<c/d;
}
}