|
// CPosBigInt02.cpp // 01/28/2004 // Author: AOU /* Implement very very very long positive integers a="12000000000001111111111113" b="62222222222222222222222222222222222223" c = a+b */ ////////////////////////////////////////////////////////////////////// // include files ////////////////////////////////////////////////////////////////////// #include <iostream.h> #include <string.h> ////////////////////////////////////////////////////////////////////// // constants ////////////////////////////////////////////////////////////////////// const int MAX_SIZE = 50; const int TEST_COUNT = 5; ////////////////////////////////////////////////////////////////////// // class CPosBigInt ////////////////////////////////////////////////////////////////////// class CPosBigInt { private: char _digits[MAX_SIZE]; void initialize(void); public: CPosBigInt(void); void display(void); CPosBigInt(char d[]); // 2004.01.28 CPosBigInt(char ch); // CPosBigInt x('r'); void displayRev(void); void displayMinimized(void); }; ////////////////////////////////////////////////////////////////////// // driver prototypes ////////////////////////////////////////////////////////////////////// void driver_CPosBigInt_Constructor_Str(void); ////////////////////////////////////////////////////////////////////// // void main(void) ////////////////////////////////////////////////////////////////////// void main(void) { driver_CPosBigInt_Constructor_Str(); //CPosBigInt x; //x.display(); //x = "123"; //x = 123; //CPosBigInt y; //y.display(); //x = y; //y.display(); //x = x + y; //CPosBigInt z("123"); //z.display(); } ////////////////////////////////////////////////////////////////////// // void CPosBigInt::initialize(void) ////////////////////////////////////////////////////////////////////// /* Initializes a big integer with zeros */ void CPosBigInt::initialize(void) { for (int i=0; i<MAX_SIZE; i++) _digits[i] = '0'; } ////////////////////////////////////////////////////////////////////// // void driver_CPosBigInt_Constructor_Str(void) ////////////////////////////////////////////////////////////////////// void driver_CPosBigInt_Constructor_Str(void) { cout << "---------------------------------\n"; cout << "driver_CPosBigInt_Constructor_Str\n"; cout << "---------------------------------\n"; CPosBigInt a("1234"); cout << "a="; a.display(); CPosBigInt b("12"); cout << "b="; b.display(); } ////////////////////////////////////////////////////////////////////// // CPosBigInt::CPosBigInt(char d[]) ////////////////////////////////////////////////////////////////////// /* A constructor that takes a character array and creates and initializes a big integer CPosBigInt a("12345"); will create object a with value 12345 "12345" 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000012345 Algorithm: fill digits with zeros for len = length of a[] digits[MAX_SIZE-len] = d[0] digits[MAX_SIZE-len+1] = d[1] digits[MAX_SIZE-len+2] = d[2] digits[MAX_SIZE-len+3] = d[3] digits[MAX_SIZE-len+4] = d[4] for i=0 to len-1 digits[MAX_SIZE-len+i] = d[i] */ CPosBigInt::CPosBigInt(char d[]) { initialize(); int len = strlen(d); for (int i=0; i<=len-1; i++) _digits[MAX_SIZE-len+i] = d[i]; } ////////////////////////////////////////////////////////////////////// // CPosBigInt::CPosBigInt(void) ////////////////////////////////////////////////////////////////////// CPosBigInt::CPosBigInt(void) { initialize(); }; ////////////////////////////////////////////////////////////////////// // void CPosBigInt::display(void) ////////////////////////////////////////////////////////////////////// void CPosBigInt::display(void) { for (int i=0; i<MAX_SIZE; i++) cout << _digits[i]; cout << endl; }; ////////////////////////////////////////////////////////////////////// // SAMPLE OUTPUT: ////////////////////////////////////////////////////////////////////// /* --------------------------------- driver_CPosBigInt_Constructor_Str --------------------------------- a=00000000000000000000000000000000000000000000001234 b=00000000000000000000000000000000000000000000000012 Press any key to continue */ |