Introduction to Programming Teach Python Fractions

advertisement
Introduction to Programming
Teach Python Fractions
Complete the RAT class that we worked on so that it can do arithmetic by adding, subtracting,
multiplying and dividing RATs and integers. Your program should be able to print the results
of the operations.
If the resulting RAT is an improper fraction (the numerator is greater than the denominator) print
the result as a “mixed number.” That is 4/3 1 1/3. If as a result of doing arithmetic on rats you
end up with a good-for-nothing-dirty-RAT (zero), print it as the digit “0”.
Your program should be able to do at least the following:
a=RAT(1,2)
b=RAT(1,3)
print(b) # should print 1/3, and the same for the results of the other operations.
c=a+b # also -, *, /
Your program should also be able to do “mixed mode arithmetic” between ints and RATS. So,
for example, if a is a RAT, a+3, a-3,3+a and 3-a should work correctly. For this assignment you
just need to do this for adding and subtracting.
Extra Credit
The result of all the operations should result in fractions that are in “lowest terms.” That is 4/6
2/3.
Download