00001 #ifndef _BIGINT_H 00002 #define _BIGINT_H 00003 00004 // author: Owen Astrachan 00005 // 8/23/95, modified 7/5/96 00006 // modified 1/5/97 00007 // 00008 //**************************************************************** 00009 // bigint1.h 00010 // 00011 // Modified January 8, 1998, Chris Nevison 00012 // 00013 // Modification removes operators -= *= (int) * (int, BigInt) 00014 // for use with lab for creating stubs. 00015 // Operators -, * (BigInt, int), and * (BigInt, BigInt) have 00016 // also been removed because they are not used in this lab. 00017 //**************************************************************** 00018 // 00019 // implements an arbitrary precision integer class 00020 // 00021 // constructors: 00022 // 00023 // BigInt() -- default constructor, value of integers is 0 00024 // BigInt(int n) -- initialize to value of n (C++ int) 00025 // BigInt(const apstring & s) -- initialize to value specified by s 00026 // it is an error if s is an invalid integer, e.g., 00027 // "1234abc567". In this case the bigint value is garbage 00028 // 00029 // 00030 // ***** arithmetic operators: 00031 // 00032 // all arithmetic operators +, -, * are overloaded both 00033 // in form +=, -=, *= and as binary operators 00034 // 00035 // multiplication also overloaded for *= int 00036 // e.g., BigInt a *= 3 (mostly to facilitate implementation) 00037 // 00038 // ***** logical operators: 00039 // 00040 // bool operator == (const BigInt & lhs, const BigInt & rhs) 00041 // bool operator != (const BigInt & lhs, const BitInt & rhs) 00042 // bool operator < (const BigInt & lhs, const BigInt & rhs) 00043 // bool operator <= (const BigInt & lhs, const BigInt & rhs) 00044 // bool operator > (const BigInt & lhs, const BigInt & rhs) 00045 // bool operator >= (const BigInt & lhs, const BigInt & rhs) 00046 // 00047 // ***** I/O operators: 00048 // 00049 // void Print() 00050 // prints value of BigInt (member function) 00051 // ostream & operator << (ostream & os, const BigInt & b) 00052 // stream operator to print value 00053 // 00054 // istream & operator >> (istream & in, const BigInt & b) 00055 // reads whitespace delimited BigInt from input stream in 00056 // 00057 00058 00059 #include <iostream.h> 00060 #include "apstring.h" // for strings 00061 #include "apvector.h" // for sequence of digits 00062 00063 class BigInt 00064 { 00065 public: 00066 00067 BigInt(); // default constructor, value = 0 00068 BigInt(int); // assign an integer value 00069 BigInt(const apstring &); // assign a string 00070 00071 // may need these in alternative implementation 00072 00073 // BigInt(const BigInt &); // copy constructor 00074 // ~BigInt(); // destructor 00075 // const BigInt & operator = (const BigInt &); // assignment operator 00076 00077 // operators: arithmetic, relational 00078 00079 const BigInt & operator += (const BigInt &); 00080 const BigInt & operator -= (const BigInt &); 00081 const BigInt & operator *= (const BigInt &); 00082 const BigInt & operator *= (int num); 00083 00084 apstring ToString() const; // convert to string 00085 int ToInt() const; // convert to int 00086 double ToDouble() const; // convert to double 00087 00088 // facilitate operators ==, <, << without friends 00089 00090 bool Equal(const BigInt & rhs) const; 00091 bool LessThan(const BigInt & rhs) const; 00092 void Print(ostream & os) const; 00093 00094 private: 00095 00096 // other helper functions 00097 00098 bool IsNegative() const; // return true iff number is negative 00099 bool IsPositive() const; // return true iff number is positive 00100 int NumDigits() const; // return # digits in number 00101 00102 int GetDigit(int k) const; 00103 void AddSigDigit(int value); 00104 void ChangeDigit(int k, int value); 00105 00106 void Normalize(); 00107 00108 // private state/instance variables 00109 00110 enum Sign{positive,negative}; 00111 Sign mySign; // is number positive or negative 00112 apvector<char> myDigits; // stores all digits of number 00113 int myNumDigits; // stores # of digits of number 00114 }; 00115 00116 // free functions 00117 00118 ostream & operator <<(ostream &, const BigInt &); 00119 istream & operator >>(istream &, BigInt &); 00120 00121 BigInt operator +(const BigInt & lhs, const BigInt & rhs); 00122 00123 BigInt operator *(int num, const BigInt & rhs); 00124 00125 bool operator == (const BigInt & lhs, const BigInt & rhs); 00126 bool operator < (const BigInt & lhs, const BigInt & rhs); 00127 bool operator != (const BigInt & lhs, const BigInt & rhs); 00128 bool operator > (const BigInt & lhs, const BigInt & rhs); 00129 bool operator >= (const BigInt & lhs, const BigInt & rhs); 00130 bool operator <= (const BigInt & lhs, const BigInt & rhs); 00131 00132 00133 #endif // _BIGINT_H not defined 00134