Due Date: Mon 7 Dec 09
A rational number is the ratio of two positive integers: p/q
Arithmetic of rationals is defines as follows:
p/q + r/s = (ps + qr)/qs
p/q - r/s = (ps - qr)/qs
(p/q) * (r/s) = (pr)/(qs)
(p/q) / (r/s) = (ps)/(qr)
Implement a class named Rational that
allows us to do rational arithmetic.
It will have 2 private int vars p and q.
It will have a constructor that initializes
p and q to P and Q respectively.
It will overload operators '+','-','*','/'
so that if r1 and r2 are of type Rational,
the expressions r1 + r2, r1 - r2, r1 * r2,
and r1 / r2 evaluate to a value of type
It will have a public member function
declared as follows:
string ToString( )
that returns "3/2" for r.ToString( ) if r is
equal to 3/2.
Implement operators "==", "!=", '<', '>'
Test the class.
that's the official assignment. no user input is required, i can define whatever i want. r1 and r2 need to be rational numbers. first thing i don't know how to do is make it so that, if r1 = 3/2 then 3=P and 2=Q. if i set those = to the capitol P and Q then the private variables should also change. how can i pull those numbers from the denominator or numerator and set either = to a variable? If there is a better way, and im sure there is cause i am a newb programmer i'd love to know. any help is appreciated. Thanks.
edit1* is it possible to do this?
with P and Q defined as public variables
P/Q = r1?
3/2 = P/Q;
This post has been edited by newkid34: 03 December 2009 - 07:09 AM