Add several pairs of fractions and simplify the resulting sums. The program shou
ID: 3796166 • Letter: A
Question
Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators, and input values of zero in either a numerator or a denominator. A zero denominator should be handled by printing an appropriate error message in the output file and without causing a program abed. The final sums should be reduced to lowest terms, and improper fractions should be converted to mixed numbers. Use the Euclidean algorithm to find the GCF to simplify fractions. Organize the program with appropriate functions. The program will be submitted in C++, with pseudo code. Input: For each pair of fractions, accept input values for numerator 1 denominator 1, numerator 2, and denominator 2. The input data file is: P_5Fractions.txt. All the input values are integers. Here are the fractions that will be added: Output: Print an appropriate header in the output file, and then print each pair of addend fractions and their sum. Note that negative signs should not be displayed in a denominator. If a mixed number is negative, print only one minus sign (i.e.. print -11/4 rather than - 1 -1/4). If the final result is an integer, no fraction portion should be displayed. If the final result is only a fraction with no whole number part, do not print a whole number of 0.Explanation / Answer
Answer:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
void printFractionSumToFile(int n1, int d1, int n2, int d2, string filename);
void reduceFractions(int& numerator, int& denominator);
int main() {
//Taking input assuming the following format for input file
//numerator1, denominator1, numerator2, denominator2
//numerator1, denominator1, numerator2, denominator2
//...
ifstream file("P5Fractions.txt");
std::string line;
while(getline(file,line))
{
std::stringstream linestream(line);
int numerator1, denominator1, numerator2, denominator2;
//Taking input separated by comma
while(getline(linestream,numerator1, denominator1, numerator2, denominator2,','))
{
printFractionSumToFile(numerator1, denominator1, numerator2, denominator2, "output.txt");
}
}
}
void printFractionSumToFile(int n1, int d1, int n2, int d2, string filename)
{
ofstream fout;
fout.open(filename);
//Case when one of the denominators is 0
if(d1==0 || d2==0)
{
fout<<"One of the fractions is invalid."<<endl;
return;
}
string output = n1+"/"+d1+" + "+n2+"/"+d2+" = ";
int numeratorSum = (n1*d2) + (n2*d1);
int denominatorSum = (d1*d2);
//determining if numerator is positive
bool nPositive = numeratorSum > 0;
bool dPositive = denominatorSum > 0;
bool sumPositive = ((nPositive == true && dPositive == true)
||(nPositive == false && dPositive == false)) ? true : false;
numeratorSum = abs(numeratorSum);
denominatorSum = abs(denominatorSum);
reduceFractions(&numeratorSum, &denominatorSum);
if(numeratorSum == denominatorSum)
{
if(sumPositive)
fout<<output+"1"<<endl;
else
fout<<output+"-1"<<endl;
}
else if(denominatorSum == 1)
{
if(sumPositive)
fout<<output+numeratorSum<<endl;
else
fout<<output+"-"+numeratorSum<<endl;
}
else if(numeratorSum < denominatorSum)
{
if(sumPositive)
fout<<output+numeratorSum+"/"+denominatorSum<<endl;
else
fout<<output+"-"+numeratorSum+"/"+denominatorSum<<endl;
}
else
{
int quotient = (numeratorSum/denominatorSum);
int remainder = (numeratorSum%denominatorSum);
if(sumPositive)
fout<<output+quotient+" "+remainder+"/"+denominatorSum<<endl;
else
fout<<output+"-"+quotient+" "+remainder+"/"+denominatorSum<<endl;
}
fout.close();
}
void reduceFractions(int& numerator, int& denominator)
{
for (int i = denominator * numerator; i > 1; i--)
{
if ((denominator % i == 0) && (numerator % i == 0))
{
denominator /= i;
numerator /= i;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.