Task: Add several pairs of fractions and simplify the resulting sums. The progra
ID: 3588365 • Letter: T
Question
Task: 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 abend. 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 pseudocode. Input: For each pair of fractions, accept input values for numerator 1, denominator 1, numerator 2, and denominator 2. The input data file is: P5Fractions.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 –1 1/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. Sample output: 3/8 + 1/6 = 13/24 9/2 + 7/2 = 8 8/9 + 11/3 = 4 5/9 1/6 + –5/6 = –2/3 –9/11 + –10/11 = –1 8/11 Suggestions: • You might want to simplify the answer after combining the fractions. • The following formula provides one way to add fractions: • Be particularly careful calculating the signs of the answers since the Euclidian algorithm may return a negative GCF
P5Fractions.txt // file
5 17 2 17
3 8 1 6
9 2 7 2
8 9 11 3
-25 41 1 2
1 6 -5 6
1 4 -1 4
-1 3 -7 8
0 2 -3 -4
-9 11 -10 11
3 7 4 0
-3 -5 -15 -7
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int gcd(int a, int b);
int lcm(int, int);
//declare class to represnt fraction and hold numerator and denominator
class fraction
{
int numerator;
int denominator;
string fractionStr;
public:
fraction()
{
numerator = 0;
denominator = 1;
}
//overloaded constructor
void set(int num, int deno)
{
numerator = num;
if (deno == 0)
{
cout << "denominator cannot be zero ";
}
else
denominator = deno;
if (numerator > denominator)
reduce();
}
fraction operator+(fraction &obj)
{
fraction sum;
//find lcum of denominator
int Lcm = lcm(denominator, obj.denominator);
sum.numerator = numerator*(Lcm / denominator) + obj.numerator*(Lcm/obj.denominator) ;
sum.denominator = Lcm;
reduce(sum);
return sum;
}
string stringForm(fraction obj)
{
int rem, q;
if (obj.numerator > obj.denominator)
{
rem = obj.numerator % obj.denominator;
//quotient
q = obj.numerator / obj.denominator;
if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0))
{
fractionStr.push_back('-');
fractionStr.push_back(' ');
}
fractionStr.push_back(q + 48);
fractionStr.push_back(' ');
fractionStr.push_back(rem+48);
fractionStr.push_back('/');
fractionStr.push_back(obj.denominator + 48);
}
else
{
fractionStr.push_back(obj.numerator + 48);
fractionStr.push_back('/');
fractionStr.push_back(obj.denominator + 48);
}
return fractionStr;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
void reduce(fraction &obj)
{
//find out common factor between numerator and denominator
int Gcd = gcd(abs(obj.numerator), obj.denominator);
obj.numerator /= Gcd;
obj.denominator /= Gcd;
}
void reduce()
{
//find out common factor between numerator and denominator
int Gcd = gcd(numerator, denominator);
numerator /= Gcd;
denominator /= Gcd;
if ((numerator < 0 && denominator) > 0 || (numerator > 0 && denominator < 0))
denominator *= -1;
}
string stringForm()
{
int rem, q;
if (numerator > denominator)
{
rem = numerator % denominator;
//quotient
q = numerator / denominator;
if ((numerator < 0 && denominator > 0 )|| (numerator > 0 && denominator < 0))
{
fractionStr.push_back('-');
fractionStr.push_back(' ');
}
fractionStr = q;
fractionStr += " ";
fractionStr += to_string(rem);
fractionStr += "/";
fractionStr += to_string(denominator);
/*fractionStr.push_back(q + 48);
fractionStr.push_back(' ');
fractionStr.push_back(rem + 48);
fractionStr.push_back('/');
fractionStr.push_back(denominator + 48);*/
}
else
{
fractionStr = to_string(numerator);
fractionStr += "/";
fractionStr += to_string(denominator);
/*fractionStr.push_back(numerator+48);
fractionStr.push_back('/');
fractionStr.push_back(denominator + 48);*/
}
return fractionStr;
}
};
int main()
{
//input file to read fraction numbers
ifstream in;
//output file to store results
ofstream out;
fraction a, b;
int i = 0,n1,n2,n3,n4;
/*fraction a(1, 2), b(2, 5);
cout << a.stringForm() << endl;;
cout << b.stringForm() << endl;
fraction sum = a + b;
cout << sum.stringForm() << endl;*/
in.open("P5Fractions.txt");
if (!in)
{
cout << "Not able to open input file" << endl;
return -1;
}
//opn file for writing
out.open("P5FractionsOut.txt");
if (!out)
{
cout << "Not able to open output file" << endl;
return -1;
}
fraction sum;
while (!in.eof())
{
in >> n1 >> n2 >> n3 >> n4;
a.set(n1, n2);
b.set(n3, n4);
sum = a + b;
out << a.stringForm() << " + " << a.stringForm() << " = " << sum.stringForm() << endl;
}
}
int gcd(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
}
else {
b -= a;
}
}
return a;
}
int lcm(int n1, int n2)
{
int max;
max = (n1 > n2) ? n1 : n2;
do
{
if (max % n1 == 0 && max % n2 == 0)
{
break;
}
else
++max;
} while (true);
return max;
}
----------------------------------------------------------
//tested with two values
//input file
5 17 2 17
-25 41 1 2
//output file
5/17 + 2/17 = 7/17
-25/41 + 1/2 = -9/82
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.