#include <stdio.h>
#include "string.h"
void addChar(char a[],char ch)//往字符串前加一个字符ch
{
for(int i = (unsigned)strlen(a)+1; i>0; i--)
{
a[i] = a[i-1];
}
a[0] = ch;
}
int main(int argc, const char * argv[]) {
char a[100],b[100],c[100];
int len,lena,lenb,t=0,max;
printf("请输入两个整数:");
scanf("%s%s",a,b);
lena = (unsigned)strlen(a);
lenb = (unsigned)strlen(b);
len = abs((lena-lenb));
max = lena > lenb ? lena : lenb;
for(int i = 0; i < len; i++)//将长度短的数前面补len个0
{
addChar((lena - lenb) > 0 ? b : a , '0');
}
for(int i = max-1; i >= 0; i--)//从后依次对应相加
{
int temp = a[i] + b[i] - 96 + t;
c[i] = 48 + temp % 10;
t = temp / 10;
}
c[max] = '\0';
if(t)//如果第一位相加大于等于10则在前面补1
{
addChar(c, '1');
}
printf("\t%s\n",a);
printf(" + %s\n",b);
printf(" = %s\n",c);
return 0;
}