#!/usr/bin/env python # # Fraction class Version 0.1 # Author: Yotam Medini yotam.medini@gmail.com -- Created: 2006/June/01 import sys class Frac: def __init__(self, n=0, d=1): self.n = n # Numerator self.d = d # Denominator def __str__(self): return "%d/%d" % (self.n, self.d) def add(a, b): add_numerator = a.n * b.d + b.n * a.d add_denominator = a.d * b.d total = Frac(add_numerator, add_denominator) return total def test_add(a, b): sys.stdout.write("%s + %s = %s\n" % (a, b, add(a, b))) # Main three_fifths = Frac(3, 5) two_sevenths = Frac(2, 7) one = Frac(1) zero = Frac() test_add(three_fifths, two_sevenths) test_add(three_fifths, one) test_add(two_sevenths, zero) test_add(zero, one) sys.exit(0)