Fractions Task Add several pairs of fractions and simplify t

Fractions

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:
        
          

5/17 + 2/17 -25/41 + 1/2 0/2 + -3/-4   

3/8 + 1/6 1/6 + -5/6 -9/11 + -10/11   

9/2 + 7/2 1/4 + -1/4 3/7 + 4/0

8/9 + 11/3 -1/3 + -7/8 -3/-5 + -15/-7

  
          
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:
n1/d1 + n2/d2 = n1d2 + n2d1/d1d2   

•   Be particularly careful calculating the signs of the answers since the Euclidian algorithm may return a negative GCF.

The input data file is:

P5Fractions.txt.

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

Solution

#include <iostream>
#include <conio.h>
using namespace std;

//Creating a Class fraction

class fraction
{
private:
int* num;
int* deno;
public:
fraction();
fraction(int,int);
fraction(const fraction&);
void printfrac();
fraction operator+(fraction);
fraction operator-(fraction);
fraction operator*(fraction);
fraction operator/(fraction);
void operator=(fraction);
};

//Used to display a fraction

void fraction::printfrac()
{
cout<<*(fraction::num)<<\"/\"<<*(fraction::deno);
}

//Default constructor

inline fraction::fraction()
{
this->num=new int(0);
this->deno=new int(0);
}

//Parameterized constructor, used to initialize a fraction

inline fraction::fraction(int num,int deno)
{
this->num=new int(num);
this->deno=new int(deno);
}
//Copy constructor, used to make a new copy of a fraction

inline fraction::fraction(const fraction&f)
{
fraction::num=new int;
fraction::deno=new int;
*(this->num)=*(f.num);
*(this->deno)=*(f.deno);
}
//This function is used to swap two variables

void swap(int &x,int &y)
{
int tmp=x;
x=y;
y=tmp;
}
//This function is used to find L.C.M of two numbers

int lcm(int l,int s)
{
if(l<s)
swap(l,s);
int m=2,result=l;
while(result%s)
{
result=l*m;
m++;
}
return result;
}
//This function is used to find G.C.D of two numbers

int gcd(int a,int b)
{
if(a<b)
swap(a,b);
while(1)
{
int c=a%b;
if(c==0)
return b;
a=b;
b=c;
}
}
//Assignment operator

void fraction::operator=(fraction f)
{
*(this->num)=*(f.num);
*(this->deno)=*(f.deno);
}
//Used to add two fractions
fraction fraction::operator+(fraction f1)
{
int slcm=lcm(*(f1.deno),*(this->deno));
int num1=*(f1.num)*(slcm/ *(f1.deno));
int num2=*(this->num)*(slcm/ *(this->deno));
int num=num1+num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
//Used to subtract two fractions
fraction fraction::operator-(fraction f2)
{
int slcm=lcm(*(this->deno),*(f2.deno));
int num2=*(f2.num)*(slcm / *(f2.deno));
int num1=*(this->num)*(slcm / *(this->deno));
int num=num1-num2;
int t=num;
num=num/gcd(num,slcm);
slcm=slcm/gcd(t,slcm);
fraction f(num,slcm);
return f;
}
//Used to multiply two fractions
fraction fraction::operator*(fraction f1)
{
int num1=*(f1.num)*(*(this->num));
int num2=*(f1.deno)*(*(this->deno));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
//Used to divide two fractions

fraction fraction::operator/(fraction f2)
{

int num1=*(this->num)*(*(f2.deno));
int num2=*(this->deno)*(*(f2.num));
int t=num1;
num1=num1/gcd(num1,num2);
num2=num2/gcd(t,num2);
fraction f(num1,num2);
return f;
}
int main()
{
int n1,n2,d1,d2,c;
char ch;
while(true)
{
cout<<\"\ Enter numerator and denominator of first fraction:\";
cin>>n1>>d1;
cout<<\"\ Enter numerator and denominator of second fraction:\";
cin>>n2>>d2;
fraction f1(n1,d1);
fraction f2(n2,d2);
cout<<\"Fraction 1: \";
f1.printfrac();
cout<<\"\ Fraction 2: \";
f2.printfrac();
cout<<\"\ \";
do
{
cout<<\"\ Menu\ \"<<\"----------------\ \"<<\"1.Addition\ 2.Multiplication\ 3.Subtraction\ 4.Division\ 5.Copy using copy constructor\ 6.Copy using assgnment operator\ \";
cout<<\"\ Enter which operation you want to perform:\";
cin>>c;
switch(c)
{
case A:
{
fraction res=f1+f2;
cout<<\"\ Result of addition:\";
res.printfrac();
break;
}
case B:
{
fraction res=f1*f2;
cout<<\"\ Result of multiplication:\";
res.printfrac();
break;
}
case C:
{
fraction res=f1-f2;
cout<<\"\ Result of subtraction:\";
res.printfrac();
break;
}
case D:
{
fraction res=f1/f2;
cout<<\"\ Result of division:\";
res.printfrac();
break;
}
case E:
{
cout<<\"\ New copy of 1st fraction:\";
fraction res1=f1;
res1.printfrac();
cout<<\"\ New copy of 2nd fraction:\";
fraction res2=f2;
res2.printfrac();
break;
}
case F:
{
cout<<\"\ New copy of first fraction:\";
fraction res1;
res1=f1;
res1.printfrac();
cout<<\"\ New copy of second fraction:\";
fraction res2;
res2=f2;
res2.printfrac();
break;
}
default:
cout<<\"\ Invalid choice!!!\";
}
cout<<\"\ Do you want to exit from program(Y/N):\";
cin>>ch;
if(ch==\'Y\'||ch==\'y\')
return 0;
cout<<\"Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):\";
cin>>ch;
}
while(ch==\'Y\'||ch==\'y\');
}
return 0;
}

OUTPUT

  

Enter numerator and denominator of 1st fraction:2
3

Enter numerator and denominator of 2nd fraction:-3
4
Fraction 1: 2/3
Fraction 2: -3/4

Menu
--
A.Addition
B.Multiplication
C.Subtraction
D.Division
E.Copy using copy constructor
F.Copy using assgnment operator

Enter which operation you want to perform:B

Result of multiplication:1/-2
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
--
A.Addition
B.Multiplication
C.Subtraction
D.Division
E.Copy using copy constructor
F.Copy using assgnment operator

Enter which operation you want to perform:D

Result of division:-8/9
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):n

Enter numerator and denominator of 1st fraction:4
5

Enter numerator and denominator of 2nd fraction:9
10
Fraction 1: 4/5
Fraction 2: 9/10

Menu
--
A.Addition
B.Multiplication
C.Subtraction
D.Division
E.Copy using copy constructor
F.Copy using assgnment operator

Enter which operation you want to perform:C

Result of subtraction:1/-10
Do you want to exit from program(Y/N):n
Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):y

Menu
--
A.Addition
B.Multiplication
C.Subtraction
D.Division
E.Copy using copy constructor
F.Copy using assgnment operator

Enter which operation you want to perform:A

Result of addition:17/10
Do you want to exit from program(Y/N):y

Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators
Fractions Task: Add several pairs of fractions and simplify the resulting sums. The program should be able to manage signed numbers, like or unlike denominators

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site