#include<iostream>
#include<cstring>
#include<cstdlib>
#define maxn 1000
using namespace std;
int a[maxn], b[maxn];
bool myStrcmp(const char str1[], const char str2[])
{
if(strlen(str1) > strlen(str2))
return true;
if(strlen(str1) == strlen(str2))
return strcmp(str1, str2) > 0;
return false;
}
void sub(char str1[], char str2[])
{
//逆序存入
for(int i = strlen(str1) - 1, j = 0; i >= 0; i--)
a[j++] = str1[i] - '0';
for(int i = strlen(str2) - 1, j = 0; i >= 0; i--)
b[j++] = str2[i] - '0';
//计算
int bit = 0;
for(int i = 0; i < maxn; i++)
{
a[i] -= bit;
if(a[i] < b[i])
{
a[i] += 10;
bit = 1;
}
else
bit = 0;
b[i] = a[i] - b[i];
}
//输出
int i;
for(i = maxn - 1; i >= 0 && b[i] == 0; i--);
if(i >= 0)
for( ; i >= 0; i--)
cout << b[i];
else
cout << 0;
cout << endl;
}
int main()
{
char str1[maxn], str2[maxn];
//清空数组和字符数组
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
cin >> str1 >> str2;
//判断字符串大小(即判断数字大小)
if(myStrcmp(str1, str2))
{
sub(str1, str2);
}
else
{
cout << "-";
sub(str2, str1);
}
return 0;
}
反对
最新推荐文章于 2024-06-10 22:34:50 发布