BigInteger BigInteger::sumDigit (BigInteger const &A, BigInteger const &B) { int t = 0, sum = 0; Node *a; Node *b; Node *c; BigInteger S; a = A.pTail; b = B.pTail; while( a != NULL && b != NULL) { sum = t + convertCharToInt(a->getValue()) + convertCharToInt(b->getValue()); // plus two node of A, B S.addHead(Node::getNode(convertIntToChar(sum % 10))); // get the unit number eg 18 -> 8 -> node t = sum / 10; // get the left number 18 -> 1; a = a->getpPrev(); b = b->getpPrev(); } if( a == NULL){ while( b != NULL) { // scan the redundancy of b sum = t + convertCharToInt(b->getValue()); S.addHead(Node::getNode(convertIntToChar(sum % 10))); t = sum/10; b = b->getpPrev(); } S.digit = B.digit; } else if( b == NULL) { while( a != NULL) { // scan the element of redundancy of a sum = t + convertCharToInt(a->getValue()); S.addHead(Node::getNode(convertIntToChar(sum % 10))); t = sum/10; a = a->getpPrev(); } S.digit = A.digit; } if( t== 1) { S.addHead(Node::getNode(convertIntToChar(t))); S.digit++; } S.setFlag(true); return S; }
BigInteger BigInteger::subtractDigit (BigInteger const &A, BigInteger const &B) { int t = 0, sum = 0; int x, y; Node *a; Node *b; Node *c; BigInteger S; a = A.pTail; b = B.pTail; while( a != NULL && b != NULL) { x = convertCharToInt(a->getValue()); y = convertCharToInt(b->getValue()); if(t + x - y >= 0) { sum = t + x - y ; // subtract two node of A, B t = 0; } else { sum = 10 + t + x - y ; t = -1; // borrow the previous unit } S.addHead(Node::getNode(convertIntToChar(sum))); // get the unit number eg 18 -> 8 -> node S.digit++; a = a->getpPrev(); b = b->getpPrev(); } if( a == NULL){ while( b != NULL) { // scan the redundancy of b y = convertCharToInt(b->getValue()); if(t - y >= 0) { sum = t - y ; // subtract two node of A, B t = 0; } else { sum = 10 + t - y ; t = -1; // borrow the previous unit } S.addHead(Node::getNode(convertIntToChar(sum))); // get the unit number eg 18 -> 8 -> node S.digit++; b = b->getpPrev(); } } else if( b == NULL) { while( a != NULL) { // scan the element of redundancy of a x = convertCharToInt(a->getValue()); if(t + x >= 0) { sum = t + x ; // subtract two node of A, B t = 0; } else { sum = 10 + t + x; t = -1; // borrow the previous unit } S.addHead(Node::getNode(convertIntToChar(sum))); // get the unit number eg 18 -> 8 -> node S.digit++; a = a->getpPrev(); } } S.setFlag(true); S.removePrefixZero(); return S; // error automatically destructor and lost all of information }