Please need help on following program using c language Ratio
Please need help on following program using c++ language.
Rational fractions are of the form a / b, in which a and b are integers and b 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations on fractions are
defined by the following rules:
a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b × c/d = ac/bd
(a/b)/(c/d) = ad/bc, in which c/d 0.
Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc.
Design a class called fractionType that performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output. Write a C++ program that, using the class fractionType, performs operations on fractions.
Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/3, the statement: cin >> x; hould store 2/3 in x.
The statement: cout << x + y << endl; should output the value of x + y in fraction form.
The statement: z = x + y; should store the sum of x and y in z in fraction form. Your answer need not be in the lowest terms.
The UML diagram for the fractionType class is provided. The test program should produce output similar to the following:
Constructor with parameters 5, 6: Num1 = 5 / 6
Default constructor (no parameters): Num2 = 0 / 1
Enter a fraction in the form a / b: 1 / 7
New value of num2 = 1 / 7
Assign num1 + num2 to num3: 41 / 42
5 / 6 + 1 / 7 = 41 / 42
(5 / 6) * (1 / 7) = 5 / 42
Assign num1 - num2 to num3 = 29 / 42
(5 / 6) - (1 / 7) = 29 / 42
(5 / 6) / (1 / 7) = 35 / 6
Turn in:
fractionType.h
fractionType.cpp
fractionTest.cpp
Solution
#include <iostream>
using namespace std;
class Rational
{
public:
Rational(int n = 0, int d = 1)
{
numerator = n;
denominator = d;
}
void print()
{
cout << numerator << \"/\" << denominator << endl;
}
void operator = (Rational c)
{
numerator = c.numerator;
denominator = c.denominator;
}
void Rational::simplify() // to simplify the fraction
{
int n=(int)this->numerator;
int d=(int)this->denominator;
int i=2;
while((i<=n) && (n!=1))
{
while((n/i==(int)n/i) && (d/i ==(int)d/i))
{
n/=i;
d/=i;
}
i++;
}
numerator=n;
denominator=(n==0)?1:d;
}
Rational operator + (Rational c) // adds fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator + c.numerator;
t.denominator = denominator + c.denominator;
return t;
}
Rational operator - (Rational c) // subtracts fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator - c.numerator;
t.denominator = denominator - c.denominator;
return t;
}
Rational operator * (Rational c) // multiplies fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator * c.numerator;
t.denominator = denominator * c.denominator;
return t;
}
Rational operator / (Rational c) // divides fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator / c.numerator;
t.denominator = denominator / c.denominator;
return t;
}
private:
int numerator;
int denominator;
}; // end class Rational
int main()
{
Rational a(1,3),b(2,5); // a is (1/3); b is (2/5) // a reduces, b does not
Rational z; // test variable to check default constructor
Rational c; // instantiates variable c to class Rational initializing to zero
c = a + b; // currently adds two numerators (1 + 2) and two denominators (3 + 5)
Rational d; // instantiates d
d = a - b; // subtracts a from b
Rational e; //
e = a*b;
Rational f;
f = a/b;
cout << \"First fraction is: \" << endl;
a.print();
cout << \"\ Second fraction is: \" << endl;
b.print();
cout << \"\ Adding first fraction to second fraction: \" << endl;
c.print();
cout << \"\ Subracting first fraction from second fraction: \" << endl;
d.print();
cout << \"\ Multiplying first fraction to second fraction: \" << endl;
e.print();
cout << \"\ Dividing first fraction by second fraction: \" << endl;
f.print();
cout << \"\ Test default fraction: \" << endl;
z.print();
return 0;
}



