Create a four-function calculator for fractions so that each
fraction is stored internally as an object of class fraction. Here are the formulas for the four arithmetic
operations applied to fractions:
Addition: a/b + c/d = (a*d + b*c) / (b*d)
Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
Multiplication: a/b * c/d = (a*c) / (b*d)
Division: a/b / c/d = (a*d) / (b*c)
There should be member
functions for input and output, as well as for the four arithmetical
operations. While you’re at it, you might as well install the capability to
reduce fractions to lowest terms. Here’s a member function that will reduce the
fraction object of which it is a member to lowest terms. It finds the greatest
common divisor (gcd) of the fraction’s numerator and denominator, and uses this
gcd to divide both numbers.
void fraction::lowterms( ) // change ourself to lowest
terms
{
long tnum, tden, temp,
gcd;
tnum = labs(num); // use non-negative copies
tden = labs(den); //
(needs cmath)
if(tden==0 ) // check
for n/0
{ cout
<< “Illegal fraction: division by 0”; exit(1); }
else if( tnum==0 ) // check for 0/n
{ num=0;
den = 1; return; }
// this ‘while’ loop
finds the gcd of tnum and tden
while(tnum != 0)
{
if(tnum
< tden) // ensure
numerator larger
{
temp=tnum; tnum=tden; tden=temp; } // swap them
tnum =
tnum - tden; // subtract them
}
gcd = tden; // this is
greatest common divisor
num = num / gcd; // divide both num and den by
gcd
den = den / gcd; // to reduce frac to
lowest terms
}
You can call this function at the end of each arithmetic
function, or just before you perform output. You’ll also need the usual member
functions: four arithmetic operations, input, and display. You may find a
two-argument constructor useful.
SOLUTION:
#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class fraction
{
private:
int
num, den; char dummy;
public:
fraction
( ): num(0), den(1)
{/*Empty
Body*/}
fraction
(int n, int d): num(n), den(d)
{/*Empty
Body*/}
void
get_F ( )
{ cout << "Enter Fraction
(in the form of a/b): "; cin
>> num >> dummy >> den;
}
void
show_F ( fraction f1, fraction f2, char op ) const
{
cout
<< f1.num << "/" << f1.den << op;
cout
<< f2.num << "/" << f2.den << " =
";
cout
<< num << "/" << den << endl;
}
void
add_F ( fraction f1, fraction f2 )
{ num = (f1.num*f2.den +
f1.den*f2.num); den =
(f1.den*f2.den); }
void
subtract_F ( fraction f1, fraction f2 )
{ num = (f1.num*f2.den -
f1.den*f2.num); den =
(f1.den*f2.den); }
void
multiply_F ( fraction f1, fraction f2 )
{ num = (f1.num*f2.num); den = (f1.den*f2.den); }
void
divide_F ( fraction f1, fraction f2 )
{ num = (f1.num*f2.den); den = (f1.den*f2.num); }
void
lowterms ( );
};
//---------------------------------------------------------------------------------------
void fraction::lowterms ( )
{
long
tnum, tden, temp, gcd;
tnum =
labs(num);
tden =
labs(den);
if(tden==0
)
{ cout << "Illegal fraction:
division by 0"; exit(1); }
else if(
tnum==0 )
{ num=0; den = 1; return; }
while(tnum
!= 0)
{
if(tnum
< tden)
{ temp=tnum; tnum=tden; tden=temp; }
tnum
= tnum - tden;
}
gcd =
tden;
num = num
/ gcd;
den = den
/ gcd;
}
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
fraction
frac1, frac2, frac3;
frac1.get_F(
); frac2.get_F( );
frac3.add_F
(frac1,frac2); frac3.lowterms(
); frac3.show_F(frac1,frac2,'+');
frac3.subtract_F
(frac1,frac2); frac3.lowterms( ); frac3.show_F(frac1,frac2,'-');
frac3.multiply_F
(frac1,frac2); frac3.lowterms( ); frac3.show_F(frac1,frac2,'*');
frac3.divide_F
(frac1,frac2); frac3.lowterms( ); frac3.show_F(frac1,frac2,'/'
);
system("pause");
}
OUTPUT:
0 comments: