Friday, July 31, 2009

Fractions in C++?

We are writing a program that manipulates fractions using the various operators. I know you can add, subtract, multiple, and divide fractions just like you would on paper. ie: cout %26lt;%26lt; (a/b)-(d/d) %26lt;%26lt; endl;





However, I am wondering how I can store the values of the numerator and denominator after an operation. I know you can store them when you multiple by saying something like:


new_numer = numer1 * numer2;


new_denom= denom1*denom2;





but what about when you're adding and subtracting fractions and the denominators aren't the same?

Fractions in C++?
numer1 *denom2 +/- numer2*denom1


-----------------------------


denom1*denom2





That's how you'd do it. Seems easy enough if you aren't reducing the fractions. Otherwise you'll have to do some factoring.

pansy

No comments:

Post a Comment