Create a class Rational Number (fractions) with mentioned capabilities

To create a class Rational Number (fractions) with the following capabilities:

  1. a) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
  2. b) Overload the addition, subtraction, multiplication and division operators for this class.

c) Overload the relational and equality operators for this class.

 

 

Code:

#include<iostream>
using namespace std;
class a
{
    int i=0,n,d,sn,sd,cond;
public:
    void getdata()
    {
        cout<<"Enter the numerator"<<endl;
        cin>>n;
        cout<<"Enter the denominator"<<endl;
        cin>>d;
    }
    void operator+(a a1)
    {
        if(a1.d!=d)
        {
        sn=(a1.n*d)+(n*a1.d);
        sd=(a1.d)*(d);
        }
        else
        {
            sn=a1.n+n;
            sd=a1.d;
        }
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
    }
    void sum()
    {

        for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }

        cout<<"Sum is "<<sn<<"/"<<sd<<endl;
    }

    a operator-(a a1)
    {
        a a3;
         if(a1.d!=d)
        {
        a3.sn=(a1.n*d)-(n*a1.d);
        a3.sd=(a1.d)*(d);
        }
        else
        {
            a3.sn=a1.n-n;
            a3.sd=a1.d;
        }
        return a3;


    }
    void sub()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Subtraction is "<<sn<<"/"<<sd<<endl;
    }
    a operator /(a a1)
    {
        a a4;
        a4.sn=a1.n*d;
        a4.sd=a1.d*n;
        return a4;
    }
    void div()
    {
         if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
        else if(sn==sd)
        {
            sn=1;
            sd=1;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
            }

    }

    cout<<"Division is "<<sn<<"/"<<sd<<endl;
    }

    a operator*(a a1)
    {
        a a5;
        a5.sn=a1.n*n;
        a5.sd=a1.d*d;
        return a5;

    }

    void mul()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Multiplication - "<<sn<<"/"<<sd<<endl;
    }

};
main()
{
    a a1,a2,a3,a4,a5,a6;
    a1.getdata();
    a2.getdata();
    a2+(a1);
    a2.sum();
    a3=a2-(a1);
    a3.sub();
    a4=a2/(a1);
    a4.div();
    a5=a2*(a1);
    a5.mul();
}

 

Program to perform deposit/withdraw and other functions of a bank.

This program will take the input from user and withdraw/deposit the amount as per the liking of the user.

 

Code:

 

#include<iostream>
#include<math.h>
#include<strings.h>
using namespace std;
class bank
{
    char n[100];
    char actp[100];
    int accn;
    int bal;
    int dep;
    int wit;
public:
    void getdata();
    void display();
    void withdraw();
    void deposit();
};

int main()
{
    int a,balance=12555;
    bank b1;
    cout<<"Hello! Please choose an option from the menu"<<endl<<"1. Deposit"<<endl<<"2. Withdraw"<<endl<<"3. Balance Inquiry"<<endl<<"4. Your info."<<endl;
    cin>>a;
    switch(a)
    {
    case 1:
        {
            b1.deposit();
            break;
        }
    case 2:
        {
            b1.withdraw();
            break;
        }
    case 3:
        {
            cout<<"Your current balance is "<<balance;
            break;
        }

    case 4:
        {
            b1.getdata();
            b1.display();
            break;
        }
    default:
        {
            cout<<"You have entered an invalid option! Please try again later..";
        }
    }
}
void bank::getdata()
{
    strcpy(n,"Shubham");
    strcpy(actp,"Saving");
    accn=001452;
    bal=12555;
}

void bank::display()
{
    cout<<"Your name is "<<n<<endl<<"Your Account type is "<<actp<<endl<<"Your Account Number is "<<accn<<endl<<"Your Current Balance is "<<bal;

}

void bank::deposit()
{
    accn=001452;
    bal=12555;
    cout<<"Enter the amount you want to deposit in your account."<<endl;
    cin>>dep;
    cout<<"Your current balance is "<<dep+bal<<" now!"<<endl;

}
void bank::withdraw()
{
    accn=001452;
    bal=12555;
    cout<<"Enter the amount you want to withdraw from your account."<<endl;
    cin>>wit;
    if(bal-wit<2000)
    cout<<"Your current balance is "<<bal<<" and no balance has been withdrawn since you need to maintain minimum 2000 balance."<<endl;
    else
        cout<<"Your current balance is "<<bal-wit<<"!";

}