标签:size span col 加法 scree include main inpu har
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MaxSize 1000 char *TwoBigNumPlus(char *InputAddend_1,char *InputAddend_2) { char *Result = malloc(MaxSize*sizeof(char)); int ResultEnd = 0; int AddendLen_1 = strlen(InputAddend_1); int AddendLen_2 = strlen(InputAddend_2); int Addend_1[MaxSize]; int Addend_2[MaxSize]; memset(Addend_1,0,sizeof(Addend_1)); memset(Addend_2,0,sizeof(Addend_2)); //reverse to store int InputAddendEnd,AddendEnd; for(InputAddendEnd = AddendLen_1-1,AddendEnd = 0;InputAddendEnd >= 0;InputAddendEnd --) { Addend_1[AddendEnd ++] = InputAddend_1[InputAddendEnd] - ‘0‘; } for(InputAddendEnd = AddendLen_2-1,AddendEnd = 0;InputAddendEnd >= 0;InputAddendEnd --) { Addend_2[AddendEnd ++] = InputAddend_2[InputAddendEnd] - ‘0‘; } //Plus && Carry for(AddendEnd = 0;AddendEnd < MaxSize;AddendEnd ++) { Addend_2[AddendEnd] += Addend_1[AddendEnd]; if(Addend_2[AddendEnd] >= 10) { Addend_2[AddendEnd+1] += Addend_2[AddendEnd] / 10; Addend_2[AddendEnd] %= 10; } } //store to return //0 screening for(AddendEnd = MaxSize-1;AddendEnd>=0 && Addend_2[AddendEnd]==0;AddendEnd --) ; if(AddendEnd >= 0) { for( ;AddendEnd >= 0;AddendEnd --) { Result[ResultEnd++] = Addend_2[AddendEnd] + ‘0‘; } } else { Result[ResultEnd++] = ‘0‘; } Result[ResultEnd] = ‘\0‘; return Result; } int main() { char InputAddend_1[MaxSize] = "99"; char InputAddend_2[MaxSize] = "99999999"; char *Result = TwoBigNumPlus(InputAddend_1,InputAddend_2); puts(Result); return 0; }
标签:size span col 加法 scree include main inpu har
原文地址:https://www.cnblogs.com/Asurudo/p/9427281.html