a + b, a -b问题,帮忙找错嗯我
#define _CRT_SECURE_NO_WARNINGS
#include<Windows.h>
#include<stdio.h>
#include<string.h>
void sub(char stra[102], char strb[102])
{
int a[110], b[110],op;
int length_a, length_b;
op = strcmp(stra, strb);
if (op == 0)
{
printf("0");
}
else
{
length_a = strlen(stra) - 1;
length_b = strlen(strb) - 1;
for (int i = 0; i < 110; i++)
{
a[i] = 0;
}
for (int i = 0; i < 110; i++)
{
b[i] = 0;
}
for (int i = length_a, j = 1; i >= 0; i--, j++)
{
a[110 - j] = stra[i] - '0';
}
for (int j = length_b, i = 1; j >= 0; j--, i++)
{
b[110 - i] = strb[j] - '0';
}
if (length_a > length_b)
{
for (int i = 109; i >= 0; i--)
{
if (a[i] >= b[i])
{
a[i] = a[i] - b[i];
}
else
{
a[i] = (10 - b[i]) + a[i];
a[i - 1] = a[i - 1] - 1;
}
}
int num = 0;
while (a[num] == 0)
{
num++;
}
for (int i = num; i < 110; i++)
{
printf("%d", a[i]);
}
}
else if (length_a < length_b)
{
for (int i = 109; i >= 1; i--)
{
if (b[i] >= a[i])
{
b[i] = b[i] - a[i];
}
else
{
b[i] = (10 - a[i]) + b[i];
b[i - 1] = b[i - 1] - 1;
}
}
printf("-");
int num = 0;
while (b[num] == 0)
{
num++;
}
for (int i = num; i < 110; i++)
{
printf("%d", b[i]);
}
}
else
{
if (op == 1)
{
for (int i = 109; i >= 1; i--)
{
if (a[i] >= b[i])
{
a[i] = a[i] - b[i];
}
else
{
a[i] = a[i] - b[i] + 10;
a[i - 1] = a[i - 1] - 1;
}
}
int num = 0;
while (a[num] == 0 && num<=109)
{
num++;
}
if (num == 110)
{
printf("0");
}
else
{
for (int i = num; i < 110; i++)
{
printf("%d", a[i]);
}
}
}
else
{
for (int i = 109; i >= 0; i--)
{
if (b[i] >= a[i])
{
b[i] = b[i] - a[i];
}
else
{
b[i] = b[i] - a[i] + 10;
b[i - 1] = b[i - 1] - 1;
}
}
int num = 0;
while (b[num] == 0 && num<109)
{
num++;
}
printf("-");
for (int i = num; i < 110; i++)
{
printf("%d", b[i]);
}
}
}
}
}
void add(char a[1000],char b[1000])
{
int int_a[1001], int_b[1001], legnth_a, legnth_b;
int_a[0] = 0;
int_b[0] = 0;
legnth_a = strlen(a);
legnth_b = strlen(b);
int m = 0;
for (int s = 1, j = 0; j < legnth_a; j++, s++)
{
int_a[s] = a[j] - '0';
if (int_a[s] == 0)
{
m = m + 1;
}
else
continue;
}
int n = 0;
for (int s = 1, j = 0; j < legnth_b; j++, s++)
{
int_b[s] = b[j] - '0';
if (int_b[s] == 0)
{
n = n + 1;
}
else
continue;
}
if (m == legnth_a && n == legnth_b)
{
printf("0");
}
else
{
if (legnth_a > legnth_b)
{
for (int j = legnth_b, s = legnth_a; j >= 1; j--, s--)
{
int_a[s] = int_a[s] + int_b[j];
if (int_a[s] >= 10)
{
int_a[s] = int_a[s] - 10;
int_a[s - 1] += 1;
}
else
{
continue;
}
}
int x;
for (int j = 0; j <= legnth_a; j++)
{
if (int_a[j] == 0)
{
continue;
}
else
{
x = j;
break;
}
}
for (x; x <= legnth_a; x++)
{
printf("%d",int_a[x]);
}
}
else
{
for (int j = legnth_a, s = legnth_b; j >= 1; j--, s--)
{
int_b[s] = int_b[s] + int_a[j];
if (int_b[s] >= 10)
{
int_b[s] = int_b[s] - 10;
int_b[s - 1] += 1;
}
else
{
continue;
}
}
int y;
for (int j = 0; j <= legnth_b; j++)
{
if (int_b[j] == 0)
{
continue;
}
else
{
y = j;
break;
}
}
for (y; y <= legnth_b; y++)
{
printf("%d",int_b[y]);
}
}
}
}
int main()
{
char str1[101], str2[101], ty;
int jk;
scanf("%d",&jk);
getchar();
for (int i = 0; i < jk; i++)
{
scanf("%s %c %s",str1, &ty,str2);
if (ty == '-')
{
sub(str1,str2);
printf("\n");
}
else
{
add(str1,str2);
printf("\n");
}
}
system("pause");
}