RSS

PROGRAM TO OVERLOAD MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS

23 May
//THIS PROGRAM OVERLOADS MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS AS WELL AS INTEGERS

#include <iostream>
#include <cstdlib>

using namespace std;

class fraction
{
	int numerator,denominator;
	public:
		fraction(int num = 0, int den = 1):numerator(num), denominator(den)
		{}
		void print()
		{
			cout<<numerator<<"/"<<denominator<<endl;
		}
		friend fraction operator*(fraction, fraction);
		friend fraction operator*(fraction, int);
		friend fraction operator*(int, fraction);
};

fraction operator*(fraction a, fraction b)
{
	int num = a.numerator * b.numerator;
	int den = a.denominator * b.denominator;
	return fraction(num,den);
}

fraction operator*(fraction a, int b)
{
	int num = a.numerator * b;
	int den = a.denominator;
	return fraction(num,den);
}

fraction operator *(int a, fraction b)
{
	return operator*(b,a);
}

int main()
{
	int choice;
	char wish;
	do{
		system("clear"); //change to system("cls") if on a windows machine
		cout<<"\nChoose an Option\n";
		cout<<"\n1. MUltiply two fractions\n";
		cout<<"\n2. Multiply a fraction and an integer\n";
		cin>>choice;
		while(choice != 1 && choice != 2)
		{
			cout<<"\nIncorrect choice\nEnter a valid one\n";
			cin>>choice;
		}
		if(choice == 1)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of first fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f1(num,den);
			cout<<"\nEnter the numerator and denominator of second fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f2(num,den);
			fraction product = f1*f2;
			cout<<"\nThe product is: \t";
			product.print();
		}
		else if(choice == 2)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of fraction\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction frac(num,den);
			cout<<"\nEnter the integer\n";
			cin>>num;
			fraction product = frac*num;
			cout<<"\nTHe product is: \t";
			product.print();
		}
		cout<<"\nDo once again? (Y/n)\t";
		cin>>wish;
		while(wish != 'Y' && wish != 'N' && wish != 'y' && wish != 'n')
		{
			cout<<"\nInvalid input\nEnter valid one\n";
			cin>>wish;
		}
	}
	while(wish == 'Y' || wish == 'y');
	
	return 0;
}

Advertisements
 
Leave a comment

Posted by on May 23, 2016 in Miscellaneous Programs

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: