00001 // sint.C 00002 00003 // no negatives allowed 00004 00005 // representation: 00006 // int capacity -- how big is dynamic array "digits" 00007 // int numDigits -- how much of the array has valid data (see invariants) 00008 // int *digits -- dynamic array to store digits (see invariants) 00009 00010 // representation invariants: 00011 // numDigits <= capacity 00012 // Data only valid in digits[0..numDigits-1] 00013 // Any valid digit[i] contains only a single digit value 00014 // i.e. a number in the range 0..9 00015 // Least significant digit (lsd) is in position 0. 00016 // Most sig. digit (msd) is in position numDigits-1. 00017 // 0 1 2 3 <-- array index 00018 // Eg: for the number 8697: digits is | 7 | 9 | 6 | 8 | 00019 // and numDigits is 4 00020 // and capacity >= 4 00021 00022 #include <assert.h> 00023 00024 #include "sint.h" 00025 00026 00027 // note: this is how we initialize a static const member 00028 00029 const int sint::INTDIGITS = 10; // number of decimal digits in largest 00030 // 32 bit int 00031 00032 00033 sint::sint() : capacity(INTDIGITS), 00034 numDigits(1) 00035 { 00036 digits = new int[capacity]; 00037 digits[0] = 0; 00038 } 00039 00040 00041 sint::sint(const sint & src): capacity(src.capacity), 00042 numDigits(src.numDigits) 00043 { 00044 00045 digits = new int[capacity]; 00046 00047 for (int i = 0; i < numDigits; i++) { 00048 digits[i] = src.digits[i]; 00049 } 00050 } 00051 00052 00053 sint::~sint() 00054 { 00055 delete [] digits; 00056 } 00057 00058 00059 sint operator +(const sint &a, const sint &b) 00060 { 00061 sint sum = a; // implemented in terms of += 00062 sum += b; // can't call until += is implemented 00063 return sum; 00064 } 00065 00066 00067 // private 00068 // pre: size >= 0 and this is a valid sint (see invariant) 00069 // post: (1) if (capacity < size) before, then (capacity'== size) 00070 // if (capacity >= size) before, then sint unchanged 00071 // (2) this is a valid sint with the same "value" as before the call 00072 // (i.e., all values in 0..numDigits-1 part of the digits array 00073 // are unchanged and numDigits is unchanged) 00074 void sint::grow(int size) 00075 { 00076 assert (size >=0); 00077 00078 if (capacity < size) { 00079 int *tmp = new int[size]; 00080 for (int i = 0; i < numDigits; i++) { 00081 tmp[i] = digits[i]; 00082 } 00083 delete [] digits; 00084 digits = tmp; 00085 capacity = size; 00086 00087 } 00088 } 00089 00090 00091 ostream & operator <<(ostream &os, const sint &b) 00092 { 00093 // need to print msd to lsd (i.e., print digits[0] last) 00094 00095 for (int i = b.numDigits-1; i >= 0; i--) { 00096 os << b.digits[i]; 00097 } 00098 00099 return os; 00100 } 00101 00102 00103 // NOTES on *=: 00104 // 1. you must *= reimplement for final version 00105 // 2. to call this version *=, need to have implemented other member 00106 // functions: +=, !=, int->big constr., copy constr, = 00107 00108 void sint::operator *=(const sint & big) 00109 // precondition: sint = a 00110 // postcondition: sint = a*big 00111 { 00112 // alg: use repeated addition 00113 00114 sint k(0); // used as counter 00115 sint limit(big); // in case of aliasing (a*=a;), use copy 00116 sint copy(*this); // will keep adding in orig value 00117 00118 *this = 0; // set self to 0 for accumulation 00119 while (k != limit) 00120 { 00121 *this += copy; 00122 k += 1; 00123 } 00124 }
1.2.6 written by Dimitri van Heesch,
© 1997-2001