Code:
#include<iostream>
#include<conio.h>
using namespace std;
class Fa
{
protected:
int n1,d1;
char op;
public:
Fa()
{
n1=0;d1=0;
}
void input()
{
cout<<"\nEnter Fraction in this format (Numerator / Denuminator): ";
x:cin>>n1>>op>>d1;
if(!(op=='/'))
{
cout<<"\nInvalid Entry. Enter Correct Operator i.e /: ";
goto x;
}
}
void dis()
{
cout<<"\nResult is : "<<n1<<op<<d1;
cout<<"\nPress Any Key to Continue...";
}
};
class Fb:public Fa
{
int n2,n,d,d2,cn1,cn2;
char op;
public:
Fb()
{
n2=0;d2=0;n=0;d=0;
}
void input()
{
cout<<"Enter two fractions to add:"<<endl;
Fa::input();
cout<<"\nEnter another Fraction in this format (Numerator / Denuminator): ";
y:cin>>n2>>op>>d2;
if(!(op=='/'))
{
cout<<"\nInvalid Entry. Enter Correct Operator i.e /";
goto y;
}
}
void add()
{
if(d1==d2)
d=d1;
else if(d1%d2==0)
d=d1;
else if(d2%d1==0)
d=d2;
else
{
for(int i=1;i<=100;i++)
{
if(d1>d2)
{
if(d1%d2==i)
if(d1%i==0)
d=(d1/i)*d2;
else
d=d1*d2;
}
else if(d2>d1)
{
if(d2%d1==i)
if(d2%i==0)
d=(d2/i)*d1;
else
d=d1*d2;
}
}
}
cn1=(d/d1)*n1;
cn2=(d/d2)*n2;
n=cn1+cn2;
}
void dis()
{
cout<<endl<<n1<<op<<d1<<" + "<<n2<<op<<d2<<" = "<<n<<op<<d<<endl;
}
};
void main()
{
char op;
Fb ob;
do
{
system("cls");
ob.input();
ob.add();
ob.dis();
cout<<"\nWant to Add Again (Y/N): ";
a:op=getche();
if(!(op=='y'||op=='Y'||op=='N'||op=='n'))
{
cout<<"\nInvalid Selection at Y/N. Enter Again:";
goto a;
}
}
while(toupper(op)=='Y');
}
Bookmarks