|
// CPosBigInt02_P02.cpp // 01/28/2004 // Author: AOU /* 2004 Spring csis 250 P02 Date Assigned: 01/30/2004, Due: 02/06/2004 Implement a constructor that will create a random big integer supply the missing code based on the algorithm Implement displayMinimized function to display a big integer without leading zeros supply the missing code based on the algorithm Expected output: ---------------------------------- driver_CPosBigInt_Constructor_Rand ---------------------------------- a=00000000000000000000000000000000000000000740948824 a=00000000000000000000000000000000000000000000517115 a=00000000761423221685761892795431233411387427793198 a=00000000000000000000000005028602486509006138934460 a=00000000000000000006184963788291359840763615420973 a=00000000000000000000000000000000000000000000000026 a=00000000000000000000000000000016575412001460717777 a=00000000035998182660380125094783512016406189841439 a=80877838371073496510996834849925533374380880681989 a=00000000000000000000000002282890781586124258626539 ---------------------------------- driver_CPosBigInt_displayMinimized ---------------------------------- a=00000000000000000000000000000000000000000000000046 a=46 a=00821197629520039181953252586772294196982554912508 a=821197629520039181953252586772294196982554912508 a=00000000000000000000000000000093967997693576658254 a=93967997693576658254 a=00000000016163355328253618621462915036492934405963 a=16163355328253618621462915036492934405963 a=00000000000000000000000000000000000002887581257444 a=2887581257444 a=00000000042930778730382520372975343211325351222640 a=42930778730382520372975343211325351222640 a=00000000000034005310675004549564821683148492070607 a=34005310675004549564821683148492070607 a=00000000000000000000000000000000000000005673849265 a=5673849265 a=00000000000000000007457983022367155402606111730048 a=7457983022367155402606111730048 a=00000000000000000000000000000000000000129038857708 a=129038857708 Press any key to continue */ /* Implement very very very long positive integers a="12000000000001111111111113" b="62222222222222222222222222222222222223" c = a+b */ ////////////////////////////////////////////////////////////////////// // include files ////////////////////////////////////////////////////////////////////// #include <iostream.h> #include <string.h> #include <stdlib.h> ////////////////////////////////////////////////////////////////////// // constants ////////////////////////////////////////////////////////////////////// const int MAX_DIGITS = 50; const int TEST_COUNT = 10; ////////////////////////////////////////////////////////////////////// // class CPosBigInt ////////////////////////////////////////////////////////////////////// class CPosBigInt { private: char _digits[MAX_DIGITS]; void initialize(void); public: CPosBigInt(void); void display(void); CPosBigInt(char d[]); // 2004.01.28 CPosBigInt(char ch); // p02 void displayMinimized(void); // p02 void displayRev(void); // }; ////////////////////////////////////////////////////////////////////// // driver prototypes ////////////////////////////////////////////////////////////////////// void driver_CPosBigInt_Constructor_Str(void); void driver_CPosBigInt_Constructor_Rand(void); void driver_CPosBigInt_displayMinimized(void); ////////////////////////////////////////////////////////////////////// // void main(void) ////////////////////////////////////////////////////////////////////// void main(void) { //driver_CPosBigInt_Constructor_Str(); driver_CPosBigInt_Constructor_Rand(); driver_CPosBigInt_displayMinimized(); } ////////////////////////////////////////////////////////////////////// // void driver_CPosBigInt_displayMinimized(void) ////////////////////////////////////////////////////////////////////// void driver_CPosBigInt_displayMinimized(void) { cout << "----------------------------------\n"; cout << "driver_CPosBigInt_displayMinimized\n"; cout << "----------------------------------\n"; for (int i=0; i<=TEST_COUNT-1; i++) { CPosBigInt a('r'); cout << "a="; a.display(); cout << "a="; a.displayMinimized(); } } ////////////////////////////////////////////////////////////////////// // void CPosBigInt::displayMinimized(void) ////////////////////////////////////////////////////////////////////// /* Purpose: Display a big integer in minimized form (without leading zeros) Examples: 000000123 will be displayed as 123 Algorithm: do the following for i=0 to MAX_DIGITS-2 if digits[i] != '0' then startPrint = i exit loop end if end do do the following for i=startPrint to MAX_DIGITS-1 display digits[i] end do */ void CPosBigInt::displayMinimized(void) { // supply the code } ////////////////////////////////////////////////////////////////////// // void driver_CPosBigInt_Constructor_Rand(void) ////////////////////////////////////////////////////////////////////// void driver_CPosBigInt_Constructor_Rand(void) { cout << "----------------------------------\n"; cout << "driver_CPosBigInt_Constructor_Rand\n"; cout << "----------------------------------\n"; for (int i=0; i<=TEST_COUNT-1; i++) { CPosBigInt a('r'); cout << "a="; a.display(); } } ////////////////////////////////////////////////////////////////////// // CPosBigInt::CPosBigInt(char ch) ////////////////////////////////////////////////////////////////////// /* Purpose: A constructor to create a random big integer Examples: CPosBigInt x('r'); could give x=667621 Algorithm: set leadingZeros to a random number between 0 and MAX_DIGITS; do the following for i=0 to MAX_DIGITS-1 if i<leadingZeros then c = '0' else c = random character betwwe '0' to '9' digits[i] = c end do */ CPosBigInt::CPosBigInt(char ch) { initialize(); // supply the code } ////////////////////////////////////////////////////////////////////// // void CPosBigInt::initialize(void) ////////////////////////////////////////////////////////////////////// /* Initializes a big integer with zeros */ void CPosBigInt::initialize(void) { for (int i=0; i<MAX_DIGITS; 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_DIGITS-len] = d[0] digits[MAX_DIGITS-len+1] = d[1] digits[MAX_DIGITS-len+2] = d[2] digits[MAX_DIGITS-len+3] = d[3] digits[MAX_DIGITS-len+4] = d[4] for i=0 to len-1 digits[MAX_DIGITS-len+i] = d[i] */ CPosBigInt::CPosBigInt(char d[]) { initialize(); int len = strlen(d); for (int i=0; i<=len-1; i++) _digits[MAX_DIGITS-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_DIGITS; i++) cout << _digits[i]; cout << endl; }; ////////////////////////////////////////////////////////////////////// // SAMPLE OUTPUT: ////////////////////////////////////////////////////////////////////// /* */ |