Saturday 12 August 2017

If you have two fractions, a/b and c/d, their sum can be obtained from the formula: a/b+c/d = a*d+b*c/b*d. For example, 1/4plus2/3 is: 1/4+2/3 = 1*3+4*2/4*3=3+8/12=11/12 Create a fraction class. Member data is the fraction’s numerator and denominator. Member functions should accept input from the user in the form 3/5, and output the fraction’s value in the same format. Another member function should add two fraction values. Write a main() program that allows the user to repeatedly input two fractions and then displays their sum. After each operation, ask whether the user wants to continue.



If you have two fractions, a/b and c/d, their sum can be obtained from the formula: a/b+c/d = a*d+b*c/b*d.
For example, 1/4plus2/3 is:
           1/4+2/3 = 1*3+4*2/4*3=3+8/12=11/12
Create a fraction class. Member data is the fraction’s numerator and denominator. Member functions should accept input from the user in the form 3/5, and output the fraction’s value in the same format. Another member function should add two fraction values.
Write a main() program that allows the user to repeatedly input two fractions and then displays their sum. After each operation, ask whether the user wants to continue.
SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class fraction
{           private:            int nume,denom;         char dummychar;
            public:
                        fraction( int n, int d ): nume(n), denom(d)  {/*Empty Body*/}
                        void get_fraction( )
                        {           cin >> nume >> dummychar >> denom;                   }
                        void add_fraction( fraction fr1, fraction fr2 )
                        {           nume=(fr1.nume*fr2.denom)+(fr1.denom*fr2.nume);
                                    denom=fr1.denom*fr2.denom;           }
                        void show_fraction( fraction fr1, fraction fr2 ) const
                        {           cout << "\n" << fr1.nume << "/" << fr1.denom << " + ";
cout << fr2.nume << "/" << fr2.denom << " = ";
cout << nume << "/" << denom << "\n";
                        }
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
            {           char c;
                        do
                        {           fraction f1(0,1),f2(0,1),f3(0,1);
                                    cout << "Enter First Fraction (in the form of a/b): ";               f1.get_fraction( );
                                    cout << "Enter Second Fraction (in the form of a/b): ";         f2.get_fraction( );
                                    f3.add_fraction( f1,f2 );           f3.show_fraction( f1,f2 );
                                    cout << "Do you want to continue or end ? ( y/n): ";              cin >> c;
                        }
                        while(c=='y' || c=='Y');
                        system("pause");
            }
OUTPUT:

0 comments: