请设计一个算法完成两个超长正整数的加法。用字符串来解决,按位加然后考虑进位就OK。
下面是代码实现:
#ifndef _ADDLONGINTERGER_#define _ADDLONGINTERGER_/*================================ Macros ===================================*/#define FALSE 0#define TURE 1/*============================= Include Function ===============================*/int AddLongInteger(char *addend, char *augend,char *result);#endif//_ADDLONGINTERGER_#include"AddLongInteger.h"#include/***********************************************************//************函数名:AddCharInerger*************************//************入口参数:加数字符和被加数字符*****************//************返回值:相加结果*******************************//************函数功能:求两个数相加结果*********************//***********************************************************/static int AddCharInerger(char addend,char augend){ int ret = (addend -'0') + (augend - '0'); return ret;}/***********************************************************//************函数名:AddLongInteger*************************//************入口参数:加数字符串和被加数字符串*************//************返回值:相加结果*******************************//************函数功能:求两个超长数相加*********************//***********************************************************/int AddLongInteger(char *addend,char *augend,char *result){ int index = 0; int tmp = 0; int sizeAddend = strlen(addend); int sizeAugend = strlen(augend); int tempNumber = sizeAddend; //判断是否正确输入 if((NULL == addend)&&(NULL == augend)&&(NULL == result)) { return FALSE; } if(tempNumber <= sizeAugend) { tempNumber = sizeAugend; } while(tempNumber) { int temp = AddCharInerger(addend[sizeAddend-1],augend[sizeAugend-1])+tmp; //判断是否要进位 if(temp >= 10) { result[index] = temp%10 + '0'; tmp = temp/10; } else { tmp = 0; result[index] = temp + '0'; } index++; sizeAddend--; //判断是否越界 if(sizeAddend == 0) { sizeAddend += 1; addend[sizeAddend-1] = '0'; } sizeAugend--; //判断是否越界 if(sizeAugend == 0) { sizeAugend += 1; augend[sizeAugend-1] = '0'; } tempNumber--; } //最后是否有进位 if(tmp > 0) { result[index] = (tmp + '0'); result[index+1] = '\0'; } else { result[index] = '\0'; } return TURE;}#include"AddLongInteger.h"#include #include #include /***********************************************************//************函数名:inputArray*****************************//************入口参数:加数字符串和被加数字符串和结果*********//************返回值:判断是否成功***************************//************函数功能:输入*********************************//**********************************************************/int inputArrayM(char **addend,char **augend,char **result){ char ch = 0; int countAddend = 0; int countAugend = 0; printf("请输入加数:"); *addend = (char *)malloc(sizeof(char)); if(NULL == *addend) { return FALSE; } while((ch = getchar()) != '\n') { *(*addend+countAddend) = ch; countAddend++; *addend = (char *)realloc(*addend,(countAddend+1)*sizeof(char)); if(NULL == *addend) { return FALSE; } } *((*addend)+countAddend) = '\0'; printf("请输入被加数:"); *augend = (char *)malloc(sizeof(char)); if(NULL == *addend) { return FALSE; } while((ch = getchar()) != '\n') { *(*augend+countAugend) = ch; countAugend++; *augend = (char *)realloc(*augend,(countAugend+1)*sizeof(char)); if(NULL == *addend) { return FALSE; } } *((*augend)+countAugend) = '\0'; if(countAugend > countAddend) { *result = (char *)malloc((countAugend+3)*sizeof(char)); } else { *result = (char *)malloc((countAddend+3)*sizeof(char)); } return TURE;}/*程序的入口main*/int main(){ int index = 0; char *addend = NULL; char *augend = NULL; char *result = NULL; inputArrayM(&addend,&augend,&result); if(FALSE == AddLongInteger(addend,augend,result)) { printf("计算出错\n"); return 0; } index = strlen(result)-1; for(;index>=0;index--) { printf("%c",result[index]); } printf("\n"); system("pause"); free(addend); free(augend); free(result); return 0;}