Mit den
BigInts soll operiert werden können wie mit gewöhnlichen
Integers, z.B:
int main() { BigInt a = "234578997624315"; BigInt b = "23987489367823"; BigInt c = a + b; cout << c << endl; return 0; }
// BigInt.hxx header file for class BigInt; class BigInt { private: int number[100]; // reserve string with 100 chars int ndig; // count digits public: BigInt(); // default constructor BigInt(const char * s); // standard constructor // BigInt(const BigInt & x ); // copy constructor, spaeter // ~ BigInt(); // destructor, spaeter void print() const; BigInt operator + (const BigInt & x ) const; BigInt operator - (const BigInt & x ) const; friend std::ostream &operator << ( std::ostream &s, const BigInt &x); }; std::ostream &operator << ( std::ostream &s, const BigInt &x); // BigInt.cxx implementation file for class BigInt #include <iostream> #include <cstring> #include "BigInt.hxx" using namespace std; #define MAX(a,b) ( (a) > (b) ? ( a ) : ( b ) ) BigInt::BigInt() { ndig = 0; } BigInt::BigInt(const char * str) { int len = strlen(str); ndig = 0; while ( len >= 0 ) { int c = str[len- -]; if ( c >= '0' && c <= '9' ) { number[ndig++] = c - '0'; } } } BigInt BigInt::operator + (const BigInt & x ) const { BigInt t; t.ndig = MAX( this->ndig, x.ndig) + 1; int sum = 0, carry = 0; for ( int i = 0; i < t.ndig; i++ ) { carry = sum/10; sum = carry; if ( i < this->ndig ) sum += this->number[i]; if ( i < x.ndig ) sum += x.number[i]; t.number[i] = sum % 10; } if ( carry == 0 ) t.ndig --; return t; } void BigInt::print() const { for ( int i = ndig; i > 0; i-- ) { cout << number[i-1]; } cout << endl; }