int size = 35; //will get bigger, small now just for testing class bigint { SafeArray<int> *arr; public: bigint() //initializes to zero { arr = new SafeArray<int>; for(int i =0;i < size; i++) arr->set(i,0); } void print() //prints numbers without zeroes in front { bool start_num=false; for(int i = 0;i <arr->get_size() ;i++) { if(arr->get(i)!=0 && start_num==false ) {start_num=true; cout << arr->get(i);} else if(start_num==true) cout<<arr->get(i); } cout<<endl; } void assign(const bigint &A) // { for(int i=0;i<arr->get_size();i++) { //Ways to initialize stuff arr->set(i,A.arr->get(i)); } } void assign(int num) { for(int i = arr->get_size()- 1; i >= 0; i--) { arr->set(i,num%10); num /=10; } } void assign(string num) // { long len = num.length(); int j=arr->get_size()-1; for(long i=len-1;i>=0;i--) { arr->set(j,num[i]-48); j--; } } void add(const bigint &A) //add big ints { int carry=0; for(int i=size-1;i>=0;i--) { int result = arr->get(i)+A.arr->get(i)+carry; arr->set(i,result%10); carry=result/10; } } void subtract(const bigint &A) //subtract big ints { int borrow = 0; for(int i=size-1; i >= 0; --i) { int result=((arr->get(i) - A.arr->get(i) - borrow)); if(result < 0) { arr->set(i, result + 10); borrow = 1; } else { arr->set(i, result); borrow = 0; } } } //int compare(const bigint &A) //compare big ints // { // // for(int i<size;i>0;i--) // { // if(A.arr->get(i) > arr->get(i)) // { // return 1; // } // else if(A.arr->get(i) < arr->get(i)) // { // return -1; // } // else // { // return 0; // } // } // // } }; int main() { bigint a, b, c; a.assign("12345678"); //for testing b.assign("12345678"); //a.compare(b); a.print(); c.assign(24691357); // 696969 is small enough to be an int. a.add(b); // a += b; a.subtract(c); // a -= b; a.print(); return 0; }</pre>
int
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)