1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
using namespace std;
void add(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void subtract(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void multiply(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void divide(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b);
void simplify(int& numerator, int& denominator, int& x, int& y, int& a, int& b);
int main()
{
int n, d, n1, d1, numerator, denominator, x, y, a, b;
char operation;
cout << "Please enter the first fractions numerator: ";
cin >> n;
cout << endl << "Please enter the first fractions denominator: ";
cin >> d;
cout << endl << "Please enter the mathematical operation (+, -, *, /): ";
cin >> operation;
cout << endl << "Please enter the second fractions numerator: ";
cin >> n1;
cout << endl << "Please enter the second fractions denominator: ";
cin >> d1;
cout << endl;
if (operation == '+')
{
add(n, d, n1, d1, numerator, denominator, x, y, a, b);
cout << "The sum of the fractions is: " << a << '/' << b << endl;
}
else if (operation == '-')
{
subtract(n, d, n1, d1, numerator, denominator, x, y, a, b);
cout << "The difference of the fractions is: " << y << "/" << x << endl;
}
else if (operation == '*')
{
multiply(n, d, n1, d1, numerator, denominator, x, y, a, b);
cout << "The product of the fractions is: " << y << "/" << x << endl;
}
else if (operation == '/')
{
divide(n, d, n1, d1, numerator, denominator, x, y, a ,b);
cout << "The quotient of the fractions is: " << y << "/" << x << endl;
}
}
void add(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
denominator = d * d1;
numerator = (n * d1) + (n1 * d);
simplify(numerator, denominator, x, y, a, b);
}
void subtract(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
denominator = d * d1;
numerator = (n* d1) - (n1* d);
simplify(numerator, denominator, x, y, a, b);
}
void multiply(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
denominator = d * d1;
numerator = n * n1;
simplify(numerator, denominator, x, y, a, b);
}
void divide(int n, int d, int n1, int d1, int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
denominator = d * n1;
numerator = n * d1;
simplify(numerator, denominator, x, y, a, b);
}
void simplify(int& numerator, int& denominator, int& x, int& y, int& a, int& b)
{
a = numerator;
b = denominator;
do
{
int t = b;
b = a % b;
a = t;
} while (b != 0);
}
|