#include <stdio.h>#include <stdlib.h>#include <string.h>char * BigNumberSubtration(const char *a, const char *b);int main(void){ int isSwap = 0; char *a = "9988798794654987657946579849874654132156798794654984613246"; char *b = "5826143213410346162471303216654677941012324646321446546879"; char *tmp_a = (char *)malloc( sizeof(char) * strlen(a) + 1); char *tmp_b = (char *)malloc( sizeof(char) * strlen(b) + 1); if ( tmp_a==NULL || tmp_b==NULL ) { //printf("分配記憶體tmp_a, tmp_b失敗在檔案:%s中第%d行\n", __FILE__, __LINE__); exit(1); } else { strcpy(tmp_a, a); strcpy(tmp_b, b); //printf("分配tmp_a, tmp_b記憶體成功在檔案:%s中第%d行\n", __FILE__, __LINE__); } char * tmp = NULL; /* 確保a>b,用大數減小數,然後根據原來的數的大小來確定加號或減號 如果a<b則交換兩個數 */ if ( strlen(a) < strlen(b) || strcmp(a, b)<0) { tmp = (char *)malloc( sizeof(char) * (strlen(a)+1) ); if ( tmp== NULL ) { //printf("分配記憶體tmp失敗在檔案:%s中第%d行\n", __FILE__, __LINE__); exit(1); } else { // printf("分配記憶體tmp成功在檔案:%s中第%d行\n", __FILE__, __LINE__); } strcpy(tmp, a); // printf("tmp is %s\n", tmp); a = (char *)malloc( sizeof(char) * (strlen(b)+1) ); memset(a, 0, sizeof(char) * strlen(a)+1); strcpy(a, b); // printf("a is %s\n", a); b = (char *)malloc( sizeof(char) * (strlen(tmp)+1) ); memset(b, 0, sizeof(char) * strlen(b)+1); strcpy(b, tmp); //printf("b is %s\n", b); isSwap = 1; } //printf("a is %s, b is %s\n", a, b); char * result = BigNumberSubtration(a, b); if ( result!=NULL) { if ( isSwap ) { printf(" %-s\n -%-s \n =-%s \n", tmp_a, tmp_b, result); } else { printf(" %-s\n -%-s \n =%s \n", tmp_a, tmp_b, result); } } else { printf("result is NULL!\n"); } printf("\n"); free(tmp); free(tmp_a); free(tmp_b); free(result); tmp = NULL; tmp_a = NULL; tmp_b = NULL; result = NULL; system("pause"); return 0;}char * BigNumberSubtration(const char *a, const char *b){ int i = 0; int j = 0; int lengthOfA = strlen(a); int lengthOfB = strlen(b); int a_index = lengthOfA - 1; int b_index = lengthOfB - 1; char memberOfA[2]; char memberOfB[2]; int maxLength = ( lengthOfA>lengthOfB ? lengthOfA: lengthOfB ) + 1; int * result = (int *)malloc( sizeof(int) * maxLength ); char * char_result = (char *)malloc( sizeof(char) * maxLength ); if ( result==NULL || char_result==NULL ) { printf("分配記憶體空間失敗!在檔案%s中第%d行!!\n", __FILE__, __LINE__); return NULL; } else { //printf("分配記憶體空間成功!在檔案%s中第%d行!!\n", __FILE__, __LINE__); } memset(result, 0, sizeof(int) * maxLength ); memset(char_result, 0, sizeof(char) * maxLength ); for (i=maxLength-1; i>=0; i--) { if ( a_index >= 0 ) { sprintf(memberOfA, "%c", a[a_index--]); } else { sprintf(memberOfA, "0"); } if ( b_index >= 0 ) { sprintf(memberOfB, "%c", b[b_index--]); } else { sprintf(memberOfB, "0"); } result[i] = atoi(memberOfA) - atoi(memberOfB); } /* for (i=0; i<maxLength; i++) { printf("%d ", result[i]); } printf("\n");*/ for(i=maxLength-1; i>1; i--) { if ( result[i] < 0 ) { result[i-1] = result[i-1] - 1; result[i] = result[i] + 10; } } /* for (i=0; i<maxLength; i++) { printf("%d ", result[i]); }*/ i = 0; while ( result[i]==0 ) { i++; } for (j=0; i<maxLength; i++, j++) { char_result[j] = result[i] + '0'; } char_result[j] = '\0'; printf("\n"); free(result); result = NULL; return char_result;}