Tuesday, 25 August 2015

(Fraction calculator) Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which a and b are integers and b 6¼ 0. Your program must be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.

#include "stdafx.h"
#include <iostream>
using namespace std;
void choice(int &a,int &b,int &c,int &d,char ch,int &upper,int &lower);
int sum(int a,int b,int c,int d,int &upper,int &lower);
int mul(int a,int b,int c,int d,int &upper,int &lower);
int div(int a,int b,int c,int d,int &upper,int &lower);
int sub(int a,int b,int c,int d,int &upper,int &lower);
int a,b,c,d,nom,den;
char operation;
int main()
{
            choice(a,b,c,d,operation,nom,den);
            cout<<"answer is "<<nom<<"/"<<den<<endl;
            system("pause");
            return 0;
}
void choice(int &a,int &b,int &c,int &d,char ch,int &nom,int &den)
{
            cout<<"enter a,b,c,d"<<endl;
            cin>>a>>b>>c>>d;
            cout<<"enter opperation "<<endl;
            cin>>ch;
            switch(ch)
            {
            case '+':
                        sum(a,b,c,d,nom,den);
                        break;
            case '*':
                        mul(a,b,c,d,nom,den);
                        break;
            case '/':
                        div(a,b,c,d,nom,den);
                        break;
            case '-':
                        sub(a,b,c,d,nom,den);
                        break;
            }
}
int sum(int a,int b,int c,int d,int &nom,int &den)
{
            nom=a*d+b*c;
            den=b*d;
            return nom;
            return den;
}
int mul(int a,int b,int c,int d,int &nom,int &den)
{
            nom=a*c;
            den=b*d;
            return nom;
            return den;
}
int sub(int a,int b,int c,int d,int &nom,int &den)
{
            nom=a*d-b*c;
            den=b*d;
                        return nom;
            return den;
}
int div(int a,int b,int c,int d,int &nom,int &den)
{
            nom=a*d;
            den=b*c;
                        return nom;
            return den;

}

No comments:

Post a Comment