Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-24, 12:47am
No Subject
Subject: No Subject  ·  Posted: 2003-09-25, 05:30pm
Rank: ? (2)
Member #: 9816
I am trying to write a program to perform basic math operations on fractions. I have been able to figure out how to multiply and divide, but I am stumped on how to make it add and subtract, since the fractions have to be multiplied to a common denominator. Here is what i have so far:

#include<conio.h>
#include<iostream>
using namespace std;

int main()

{
int i1, i2, i3, i4;
char c, op;

cout<<"Please enter a fraction using #/#, the operation (+, -, *, /) you wish \nto be performed, and the second fraction, seperating the fractions \nand operator with a space."<<endl;

cin>>i1;
cin.get(c);
cin>>i2;
cin.get(c);
cin.get(op);
cin.get(c);
cin>>i3;
cin.get(c);
cin>>i4;

if (op = '*')
cout<<"The answer is "<<i1 * i3<<"/"<<i2 * i4<<endl;




return 0;
}

If anyone could help me i would appreciate it. I need a response as fast as possible, so my Email is Blitz07734@yahoo.com and my AIM SN is blitz07735.

Thanks,
John

 
  Reply to this ·  Post link ·  Top
Subject: Re: No Subject  ·  Posted: 2003-09-26, 01:39am
Rank: ? (4827)
Member #: 3416
if you don't need the *lowest* common denominator, you can always find a common denominator this way:

n1/d1 + n2/d2 = (n1/d1) * (d2/d2) + (n2/d2) * (d1/d1)

you will have d1 * d2 as your denominator for both fractions that you want to add together.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs