0

can someone help me figure out why the output of my program is coming out wrong

#include<iostream>
using namespace std;

class fractions
{
public:
	fractions fractions :: operator+(fractions f);
	fractions fractions :: operator-(fractions f);
	fractions fractions :: operator*(fractions f);
	fractions fractions :: operator/(fractions f);
	fractions addFraction(fractions);
	fractions subFraction(fractions);
	fractions multiFraction(fractions);
	fractions divideFraction(fractions);
	fractions();
	fractions(int, int);
	void getFraction();
	void printFraction();
private:
	int numerator;
	int denominator;
};

//method to add two fractions
fractions fractions :: addFraction(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator + denominator * f.numerator;
	return temp;
}
//method to add two fractions
fractions fractions :: operator+(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator + denominator * f.numerator;
	return temp;
}
//method to subtract two fractions
fractions fractions :: subFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator - denominator * f.numerator;
    return temp;
}
//method to subtract two fractions
fractions fractions :: operator-(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator - denominator * f.numerator;
	return temp;
}
//method to multiply two fractions
fractions fractions :: multiFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator * denominator * f.numerator;
    return temp;
}
//method to multiply two fractions
fractions fractions :: operator*(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator * denominator * f.numerator;
	return temp;
}
//method to divide to fractions
fractions fractions :: divideFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator / denominator * f.numerator;
    return temp;
}
//method to divide two fractions
fractions fractions :: operator/(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator / denominator * f.numerator;
	return temp;
}


fractions::fractions()
{
	numerator = 0;
	denominator = 1;
}

fractions::fractions(int n, int d)
{
	numerator = n;
	denominator = d;
}

void fractions::printFraction()
{
	cout <<numerator<<'/'<<denominator<<endl;
}

void getFraction(int &numerator, int &denominator)
{
    cout<<"Please enter numerator";
	cin >>numerator;
	cout<<"Please enter denominator";
	cin >>denominator;
}

int main()
{
	int n,d;
	fractions f1(1,2);
	fractions f2(3,4);
	fractions f3;
	fractions f4;
    getFraction(n,d);
	fractions f5(n,d);
	

	f3 = f1.addFraction(f2);
	f4 = f3 + f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.subFraction(f2);
	f4 = f3 - f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.multiFraction(f2);
	f4 = f3 * f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.divideFraction(f2);
	f4 = f3 / f1;
	f3.printFraction();
	f4.printFraction();
	system("pause");

Edited by peter_budo: Please do not use Code Snipets for asking questions. Use Forum Thread option.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

Your multiplication and division functions are incorrect. A/B * C/D is simply (A*C) /(B*D) you have some extra factors in there. Your numerator for the division function is actually the correct amount, so just separate that into the numerator and denominator. Scratch some examples out on paper to convince yourself. There may be more that is wrong here but there's two obvious places to start.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.