srand(time(NULL)),time(NULL) is the seed. This seed application in this function will return a random number; the same seed will produce the same random number.
The bottom layer adopts the sequence table: if the memory is insufficient, the following functions are used to expand the memory;
bool Inc() { capacity += INC_SIZE; Type *new_base = new Type[capacity+1]; if(NULL == new_base) { cout<<"Out Of Memory."<<endl; //exit(1); return false; } memset(new_base, 0, sizeof(Type)*(capacity+1)); memcpy(new_base, base, sizeof(Type)*(capacity-INC_SIZE+1)); delete []base; base = new_base; return true; }
capacity is the private data of a sequential table.
The way of adding: mark method is used to judge whether the total number is greater than 10 by sign. If it is greater than 10, set sign to 1, and add it next time when calculating each number. Code of adding implementation:
u_char BigInt::AddItem(u_char a, u_char b, u_char &sign) { u_char sum = a + b + sign; if(sum >= 10) { sum -= 10; sign = 1; } else sign = 0; return sum; } void BigInt::Add(BigInt &bt, const BigInt &bt1, const BigInt &bt2) { bt.clear(); u_char sum, sign = 0; size_t i=1, j=1; while(i<=bt1.size() && j<=bt2.size()) { sum = AddItem(bt1[i],bt2[j],sign); bt.push_back(sum); ++i; ++j; } while(i <= bt1.size()) { sum = AddItem(bt1[i], 0, sign); bt.push_back(sum); ++i; } while(j <= bt2.size()) { sum = AddItem(0, bt2[j], sign); bt.push_back(sum); ++j; } if(sign > 0) bt.push_back(sign); }
Subtraction also uses sign
u_char BigInt::SubItem(u_char a, u_char b, u_char &sign) { u_char sub; if(a >= b+sign) { sub = a - b - sign; sign = 0; } else { sub = a + 10 - b - sign; sign = 1; } return sub; } void BigInt::Sub(BigInt &bt, const BigInt &bt1, const BigInt &bt2) { bt.clear(); if(bt1 < bt2) return; if(bt1 == bt2) bt = 0; u_char sub, sign = 0; size_t i=1, j=1; while(i<=bt1.size() && j<=bt2.size()) { sub = SubItem(bt1[i], bt2[j], sign); bt.push_back(sub); ++i; ++j; } while(i<=bt1.size()) { sub = SubItem(bt1[i], 0, sign); bt.push_back(sub); ++i; } bt.clear_head_zero(); }