Write a program that works with fractions. Your program should be able to add,
subtract, multiply, and divide two fractions. Write a separate function for addition,
subtraction, multiplication and division. Specifically, your program must request
two fractions from the user, getting the numerator and denominator separately for
each fraction, and the operation to perform (add, subtract, multiply, or divide).
Your program will then compute the resulting fraction, keeping the numerator and
denominator separate, and output the result.
Note:
Make the structure for fractions. Then declare variable of fractions and use
them for addition and multiplication etc....
#include <iostream>
using namespace std;
struct Fraction
{
int _numerator;
int _denominator;
Fraction(int num, int den)
{
_numerator = num;
_denominator = den;
}
};
Fraction Add(Fraction frac1, Fraction frac2)
{
Fraction result;
result._numerator = (frac1._numerator * frac2._denominator) + (frac2._numerator * frac1._denominator);
result._denominator = frac1._numerator * frac2._denominator;
return result;
}
Fraction Sub(Fraction frac1, Fraction frac2)
{
Fraction result;
result._numerator = (frac1._numerator * frac2._denominator) + (frac2._numerator * frac1._denominator);
result._denominator = frac1._numerator * frac2._denominator;
return result;
}
Fraction Mul(Fraction frac1, Fraction frac2)
{
Fraction result;
result._numerator = frac1._numerator * frac2._numerator;
result._denominator = frac1._denominator * frac2._denominator;
return result;
}
Fraction Div(Fraction frac1, Fraction frac2)
{
Fraction result;
result._numerator = frac1._numerator * frac2._denominator;
result._denominator = frac1._denominator * frac2._numerator;
return result;
int main()
{
while(true)
{
int n1, n2, d1, d2;
cout << “Enter the numerator of the first fraction: “;
cin >> n1;
cout << “Enter the denominator of the fraction: “
cin >> d1
cout << “Enter the numerator of the second fraction: “
cin >> n2;
cout << “Enter the denominator of the second fraction: “
cin >> d2;
int choice;
switch(choice)
{
}
}
return 0;
}
Comments
Leave a comment