|
| 1 | +''' |
| 2 | +For some calculations it is necessary to have very large integers, i.e., integers with an arbitrarily large number of digits. We can represent such integers using lists. Design and implement a class for representing arbitrarily large integers. Your implementation should include operations to add, subtract, and multiply such integers, and to compute the power of such an integer, where k is a small positive integer. Hint: Base your design on the Polynomial class given in Program . |
| 3 | +''' |
| 4 | + |
| 5 | +from opus7.polynomialAsOrderedList import PolynomialAsOrderedList |
| 6 | +from opus7.visitor import Visitor |
| 7 | +from opus7.linkedList import LinkedList |
| 8 | + |
| 9 | +class LargeInt(PolynomialAsOrderedList): |
| 10 | + |
| 11 | + def __add__(self, poly): |
| 12 | + result = LargeInt() |
| 13 | + p1 = iter(self._list) |
| 14 | + p2 = iter(poly._list) |
| 15 | + term1 = self.nextTerm(p1) |
| 16 | + term2 = self.nextTerm(p2) |
| 17 | + carry = 0 |
| 18 | + while term1 is not None and term2 is not None: |
| 19 | + temp = term1 + term2 + carry |
| 20 | + carry = temp/10 |
| 21 | + temp %= 10 |
| 22 | + print "adding %d to %d, get %d and %d as carry" % (term1, term2, temp, carry) |
| 23 | + result.addTerm(temp) |
| 24 | + term1 = self.nextTerm(p1) |
| 25 | + term2 = self.nextTerm(p2) |
| 26 | + while term1 is not None: |
| 27 | + print "entering only term1" |
| 28 | + term1 += carry |
| 29 | + carry = term1/10 |
| 30 | + term1 %= 10 |
| 31 | + print "adding %d and %d as carry" % (term1, carry) |
| 32 | + result.addTerm(term1) |
| 33 | + term1 = self.nextTerm(p1) |
| 34 | + while term2 is not None: |
| 35 | + print "entering only term2" |
| 36 | + term2 += carry |
| 37 | + carry = term2/10 |
| 38 | + term2 %= 10 |
| 39 | + print "adding %d and %d as carry" % (term2, carry) |
| 40 | + result.addTerm(term2) |
| 41 | + term2 = self.nextTerm(p2) |
| 42 | + if carry != 0: |
| 43 | + print "adding extra carry", carry |
| 44 | + result.addTerm(carry) |
| 45 | + return result |
| 46 | + |
| 47 | + def nextTerm(self, iter): |
| 48 | + """ |
| 49 | + (PolynomialAsSortedList, Iterator) -> Polynomial.Term |
| 50 | + Returns the next term or None if there are no more terms. |
| 51 | + """ |
| 52 | + try: |
| 53 | + return iter.next() |
| 54 | + except StopIteration: |
| 55 | + return None |
| 56 | + |
| 57 | + def generateInt(self, *ints): |
| 58 | + for i in reversed(ints): |
| 59 | + self.addTerm(i) |
| 60 | + |
| 61 | + def __str__(self): |
| 62 | + r = self.ReverseVisitor() |
| 63 | + self.accept(r) |
| 64 | + return str(r.list) |
| 65 | + |
| 66 | + class ReverseVisitor(Visitor): |
| 67 | + def __init__(self): |
| 68 | + self._list = LinkedList() |
| 69 | + |
| 70 | + def visit(self, obj): |
| 71 | + self._list.prepend(obj) |
| 72 | + |
| 73 | + def getList(self): |
| 74 | + return self._list |
| 75 | + |
| 76 | + list = property( |
| 77 | + fget = lambda self: self.getList()) |
| 78 | + |
| 79 | +def pro6(): |
| 80 | + p1 = LargeInt() |
| 81 | + p1.generateInt(1, 9, 9) |
| 82 | + p2 = LargeInt() |
| 83 | + p2.generateInt(9,9) |
| 84 | + result = p1 + p2 |
| 85 | + print p1, " + ", p2, " = ", result |
| 86 | + |
| 87 | +pro6() |
0 commit comments