adding and multiplying fractions

doing an arithmetic operation on two fractions that may be imperfect(f

Page 1 of 1

2 Replies - 16109 Views - Last Post: 23 February 2007 - 07:51 AM Rate Topic: -----

#1 psydoll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-February 07

adding and multiplying fractions

Posted 23 February 2007 - 01:08 AM

//This is compiling but the adding and the multiplying operation towards the bottom in red is giving values that are totally incorrect including negatives and zeros for whole //numbers
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

using namespace std;
struct Fraction
{
int numerator;
int denominator;
int wholeNumber;
};

void main()
{
Fraction firstFraction;
Fraction secondFraction;
Fraction finalFraction;
Fraction finalFractionNumeratorOne;
Fraction finalFractionNumeratorTwo;
firstFraction.wholeNumber=0;
secondFraction.wholeNumber=0;
finalFraction.wholeNumber=0;
char operation;

cout<<firstFraction.wholeNumber;
cout<<"Enter a numerator for a fraction: "<<endl;
cin>>firstFraction.numerator;
cout<<"Now enter the denominator for that fraction: "<<endl;
cin>>firstFraction.denominator;


//first loop that keeps asking for a denominator until they enter one greater than zero
while(firstFraction.denominator<=0)
{
cout<<"Please enter a number higher than 0 for your denominator: "<<endl;
cin>>firstFraction.denominator;
}
if (firstFraction.numerator<firstFraction.denominator)
cout<<"Your first fraction is:"<<firstFraction.numerator<<" / "<<firstFraction.denominator<<endl;

else
{
do
{
firstFraction.numerator = (firstFraction.numerator-firstFraction.denominator);
firstFraction.wholeNumber = firstFraction.wholeNumber+1;
}
while(firstFraction.numerator >= firstFraction.denominator);

cout<<"Your first fraction is:"<<firstFraction.wholeNumber<<" "<<firstFraction.numerator<<"/"<<firstFraction.denominator<<endl;
}
//end of first fractions loop


cout<<"Enter a numerator for a second fraction: "<<endl;
cin>>secondFraction.numerator;
cout<<"Now enter the denominator for that fraction: "<<endl;
cin>>secondFraction.denominator;

while(secondFraction.denominator<=0)
{
cout<<"Please enter a number higher than 0 for your denominator: "<<endl;
cin>>secondFraction.denominator;
}
if (secondFraction.numerator<secondFraction.denominator)
cout<<"Your first fraction is:"<<secondFraction.numerator<<" / "<<secondFraction.denominator<<endl;

else
{
do
{
secondFraction.numerator = (secondFraction.numerator-secondFraction.denominator);
secondFraction.wholeNumber = secondFraction.wholeNumber+1;
}
while(secondFraction.numerator >= secondFraction.denominator);

cout<<"Your first fraction is:"<<secondFraction.wholeNumber<<" "<<secondFraction.numerator<<"/"<<secondFraction.denominator<<endl;
}
//end of second fraction loop


cout<<"Please enter an arithmetic operation to preform on the two fraction (either a + for addition or a * for multiplication)";
cin>>operation;





//start of the operation loop this is where I am having difficulties
do
{
cout<<"Please enter a + for addition or a * for multiplication";
cin>>operation;
}while (operation!='+' && operation!='*');

if(operation=='+')
{

finalFractionNumeratorOne.numerator=firstFraction.numerator * secondFraction.denominator;

finalFraction.denominator=firstFraction.denominator * secondFraction.denominator;

finalFractionNumeratorTwo.numerator = secondFraction.numerator * firstFraction.denominator;

finalFraction.numerator=finalFractionNumeratorOne.numerator + finalFractionNumeratorTwo.numerator;

finalFraction.wholeNumber = firstFraction.wholeNumber + secondFraction.wholeNumber;

do

{

finalFraction.wholeNumber=finalFraction.wholeNumber+1;

finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;

} while(finalFraction.numerator>=finalFraction.denominator);


if (finalFraction.wholeNumber = 0)

cout<<"the result of adding these two fractions is: " <<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

else

cout<<"the result of adding these two fractions is: " <<finalFraction.wholeNumber<<" "<<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

}

else

{
firstFraction.numerator= firstFraction.numerator + (firstFraction.wholeNumber* firstFraction.numerator);
secondFraction.numerator= secondFraction.numerator + (secondFraction.wholeNumber* secondFraction.numerator);
finalFraction.numerator= firstFraction.numerator * secondFraction.numerator;
finalFraction.denominator= firstFraction.denominator * secondFraction.denominator;
do
{
finalFraction.wholeNumber++;
finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;
}
while(finalFraction.numerator>=finalFraction.denominator);

if (finalFraction.wholeNumber = 0)

cout<<"the result of multiplying these two fractions is: " <<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

else

cout<<"the result of multiplying these two fractions is: " <<finalFraction.wholeNumber<<" "<<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

}

getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: adding and multiplying fractions

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: adding and multiplying fractions

Posted 23 February 2007 - 02:12 AM

If we say a mixed fraction is W+N/D and we want to convert this to a fraction we multiply W by D and add it to N ---> ((W*D)+N)/D

Your program uses ((W*N)+N)/D.

You use this structure:
		do
		{
			finalFraction.wholeNumber++;
			finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;
		}
		while(finalFraction.numerator>=finalFraction.denominator);

to convert between a fraction and a mixed number. This is not the same thing as you used above in your program when getting the other values. This bit of code seems to be at fault.
Was This Post Helpful? 0
  • +
  • -

#3 msg555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 04-May 06

Re: adding and multiplying fractions

Posted 23 February 2007 - 07:51 AM

Is there any reason you want to convert to whole number + fraction < 1 form? It's much easier to work with them in N/D form.

To add two fractions

N1/D1 + N2/D2 = (N1 * D2 + N2 * D1) / (D1 * D2)

To Multiply two fractions
N1/D1 * N2/D2 = (N1 * N2) / (D1 * D2)

To simplify a fraction

Let gcd be defined as
int gcd(int n, int d)
{
	if(n == 0) return d;
	return gcd(d % n, n);
}


Then N can be simplified to N/gcd(N, D) and D can be simplified to D/gcd(N, D)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1