C++ Write a program that will utalize a Fraction struct that has a numerator and
ID: 3668079 • Letter: C
Question
C++
Write a program that will utalize a Fraction struct that has a numerator and a denominator. Make an overloaded operator + for the struct that can add two Fractions and return a Fraction result. Make an overloaded * that can also multiply two Fractions together. Make an overloaded operator > that can compare two Fractions and determine whether the first one is greater than the second one. Make another overloaded operator * that has a Fraction as a first operand and an integer as a second operand, which together produce a Fraction result, and a third overloaded operator * which has an integer as a first operand and a Fraction as a second operand.
Explanation / Answer
#include <cstdlib>
#include <iostream>
using namespace std;
struct Fraction
{
int numerator;
int denominator;
};
void PrintFraction(Fraction sFraction)
{
cout << sFraction.numerator << "/" << sFraction.denominator;
}
Fraction addition(Fraction fract1, Fraction fract2)
{
Fraction result;
result.numerator = (fract1.numerator * fract2.denominator) + (fract2.numerator * fract1.denominator);
result.denominator = fract1.denominator * fract2.denominator;
return result;
}
Fraction subtraction(Fraction fract1, Fraction fract2)
{
Fraction result;
result.numerator = (fract1.numerator * fract2.denominator) - (fract2.numerator * fract1.denominator);
result.denominator = fract1.denominator * fract2. denominator;
return result;
}
Fraction multiplication(Fraction fract1, Fraction fract2)
{
Fraction result;
result.numerator = fract1.numerator * fract2.numerator;
result.denominator = fract1.denominator * fract2. denominator;
return result;
}
Fraction division(Fraction fract1, Fraction fract2)
{
Fraction result;
result.numerator = fract1.numerator * fract2.denominator;
result.denominator = fract1.denominator * fract2.numerator;
return result;
}
int main(int argc, char *argv[])
{
Fraction sum, difference, product, quotient;
Fraction sFract1;
Fraction sFract2;
cout << "Enter the numerator of the first fraction: ";
cin >> sFract1.numerator;
cout << "Enter the denominator of the first fraction: ";
cin >> sFract1.denominator;
cout << "Enter the numerator of the second fraction: ";
cin >> sFract2.numerator;
cout << "Enter the denominator of the second fraction: ";
cin >> sFract2.denominator;
cout << endl;
cout << "Fraction 1 = ";
PrintFraction(sFract1);
cout << endl;
cout << "Fraction 2 = ";
PrintFraction(sFract2);
cout << endl;
cout << endl;
sum = addition(sFract1, sFract2);
PrintFraction(sFract1);
cout << " + ";
PrintFraction(sFract2);
cout << " = ";
PrintFraction(sum);
cout << endl;
difference = subtraction(sFract1, sFract2);
PrintFraction(sFract1);
cout << " - ";
PrintFraction(sFract2);
cout << " = ";
PrintFraction(difference);
cout << endl;
product = multiplication(sFract1, sFract2);
PrintFraction(sFract1);
cout << " * ";
PrintFraction(sFract2);
cout << " = ";
PrintFraction(product);
cout << endl;
quotient = division(sFract1, sFract2);
PrintFraction(sFract1);
cout << " / ";
PrintFraction(sFract2);
cout << " = ";
PrintFraction(quotient);
cout << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.