int BigInteger::compareNumber(BigInteger const &A, BigInteger const &B) { int A_digit = A.amountDigit(); int B_digit = B.amountDigit(); Node *a_Ptr = A.pHead; Node *b_Ptr = B.pHead; int a, b; if(A_digit > B_digit) return 1; else if (A_digit < B_digit) return -1; else { while( a_Ptr != NULL ) { a = convertCharToInt(a_Ptr->getValue()); b = convertCharToInt(b_Ptr->getValue()); if( a > b) return 1; else if( a < b) return -1; a_Ptr = a_Ptr->getpNext(); b_Ptr = b_Ptr->getpNext(); } } return 0; }
void addCharNum(char *num1, const char *num2) { const size_t len1 = strlen(num1); const size_t len2 = strlen(num2); const size_t max_len = (len1 > len2 ? len1 : len2); if(len1 <= 0 || len2 <= 0 || (max_len + 1) >= kBufferLimitSize) return; char temp[kBufferLimitSize] = {}; temp[max_len] = temp[max_len + 1] = '\0'; int32_t n1i = len1; int32_t n2i = len2; int32_t carry = 0; size_t idx = 0; for(idx = 0; idx < max_len; ++idx) { int32_t n1_val = --n1i >= 0 ? convertCharToInt(num1[n1i]) : 0; int32_t n2_val = --n2i >= 0 ? convertCharToInt(num2[n2i]) : 0; int32_t sum = n1_val + n2_val + carry; temp[idx] = (sum % 10) + '0'; carry = sum / 10; } if(carry > 0) { temp[idx] = carry + '0'; } size_t sum_len = strlen(temp); for(size_t i = 0; i < sum_len; ++i) { num1[i] = temp[sum_len - i - 1]; } num1[sum_len] = '\0'; }
int main() { printf("%d", 'A'); //Declare the stuff we need int bucket[MAX_NUM_CHARS], i, currentChar; char c; //Initializes the array to zero for(i = 0; i < 26; i++) bucket[i] = 0; //Read a bunch of characters. while((c=getchar()) != EOF) { //Convert the char into the value we want i = convertCharToInt(c); //Little bit of input validation if(i>=0) bucket[i]++; } //The rest is output printf("\n\nLetter Count\n------ -----\n"); for(i = 0; i < 26; i++) printf("%3c%10d\n", i+'a', bucket[i]); return 0; }
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; }
/********************** * Reference: http://www.ascii-code.com/ * Reference: http://www.cquestions.com/2011/07/c-program-for-modular-division-of-large.html ***********************/ int checkIfValid(int size, char textArray[]) { int i; int character; i = 0; while(i < size - 1) //loop for size of textArray { character = convertCharToInt(textArray[i]); if (character < 65 && character != 32) //check if character is less than A and not ' ' on ascii table { return 0; } else if (character > 90 && character != 32) //check if character is greater than Z and not ' ' { return 0; } i++; } return 1; }
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 }