#include<cstdio>
#include<cstring>
using namespace std;
void bigAdd(char a[],char b[],char res[])
{
int lena = strlen(a) - 1;
int lenb = strlen(b) - 1;
int index = 0;
int k = 0;//保留进位
while(lena >= 0 && lenb >= 0)
{
res[index] = a[lena] + b[lenb] + k - 48;//注意-48
k = 0;
if(res[index] > '9')
{
res[index] -= 10;
k = 1;
}
--lena;
--lenb;
++index;
}
if(lena < 0)
{
while(lenb >= 0)
{
res[index] = b[lenb] + k;
k = 0;
if(res[index] > '9')
{
res[index] -= 10;
k = 1;
}
--lenb;
++index;
}
}
else if(lenb < 0)
{
while(lena >= 0)
{
res[index] = a[lena] + k;
k = 0;
if(res[index] > '9')
{
res[index] -= 10;
k = 1;
}
--lena;
++index;
}
}
if(k == 1)//处理边界值
{
res[index] = '1';
}
}
void bigMinus(char a[],char b[],char res[])//提前保证a大于b
{
int lena = strlen(a) - 1;
int lenb = strlen(b) - 1;
int index = 0;
int k = 0;
while(lenb >= 0)
{
res[index] = a[lena] - b[lenb] - k +48;
k = 0;
if(res[index] < 48)
{
res[index] += 10;
k = 1;
}
++index;
--lena;
--lenb;
}
while(lena >= 0)
{
res[index] = a[lena] - k;
k = 0;
if(res[index] < 48)
{
res[index] += 10;
k = 1;
}
++index;
--lena;
}
//输出结果时操作可以去掉前面的0。
}
int main()
{
char a[100];
char b[100];
char res[105];
int i;
while(1)
{
memset(res,0,sizeof(res));
scanf("%s %s",a,b);
bigMinus(a,b,res);
i = strlen(res);
while(i--)
{
printf("%c",res[i]);
}
printf("\n");
}
return 0;
}
大整数加减法
最新推荐文章于 2021-12-02 00:52:29 发布