Listing 2: Addition and Subtraction routine
//add 2 BigNums BigNum BigNum::operator+(const BigNum &b){ if (positive){ if (b.positive){ if (exp < b.exp) return (b + *this); else{ BigNum a; a.exp = exp + 1; delete[] a.x; a.x = new int[a.exp+1]; a.positive = 1; a.x[a.exp]=0; long carry=0; long temp; for (int i=0;i<=b.exp;i++){ temp = long(x[i])+long(b.x[i])+carry; a.x[i] = temp % BASE; carry = temp/BASE; } for(i=b.exp+1;i<=exp;i++){ temp = x[i] + carry; a.x[i] = temp % BASE; carry = temp/BASE; } if (!a.x[a.exp]) a.exp--; return a; } //a pos & b neg }else return (*this - (-b)); }else{ if (b.positive) return (b - (-*this)); //a neg else //a neg & b neg return (- (-*this) + (-b)); } } //subtract a BigNum from another BigNum BigNum BigNum::operator-(const BigNum &b){ if (positive){ if (b.positive){ if (*this<b) return(-(b-*this)); BigNum a; a.exp = exp; delete[] a.x; a.x = NULL; a.x = new int[a.exp+1]; a.positive = 1; a.x[a.exp]=0; for (int i=0;i<=exp;i++) a.x[i]=x[i]; for(i=0;i<=b.exp;i++){ long temp = a.x[i] - b.x[i]; if (temp < 0){ a.x[i+1]-=1; temp+=BASE; } a.x[i] = temp; } i = b.exp+1; while(a.x[i]<0){ a.x[i++]+=BASE;a.x[i]-=1; } while(!a.x[a.exp] && a.exp) a.exp--; return a; }else //a pos & b neg return (*this + (-b)); }else{ //a negative if (b.positive)return (-(b + (-*this))); else return(b-*this); } } //End of File