TOJ-2234. A+B In the Future

//不知道为什么,我的代码怎么这么多
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

char a[25], b[25], s[25];
int aa[25], bb[25], num[25];
int la, lb, flag = -1;

int getnum(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	else
		return c - 'A' + 10;
}

char getchar(int c)
{
	if (c >= 0 && c <= 9)
		return c + '0';
	else
		return c - 10 + 'A';
}
void make()
{
	memset(aa, 0, sizeof(aa));
	memset(bb, 0, sizeof(bb));
	memset(num, 0, sizeof(num));
	la = strlen(a); lb = strlen(b);
	for (int i = la - 1, j = 0; i > 0; i--)
		aa[j++] = getnum(a[i]);
	for (int i = lb - 1, j = 0; i > 0; i--)
		bb[j++] = getnum(b[i]);
}

void compare()
{
	if (la < lb)  flag = 0;
	else if (la > lb) flag = 1;
	else
	{
		for (int i = la - 2; i >= 0; i--)
			if (aa[i] < bb[i])
			{
				flag = 0; break;
			}
			else if (aa[i] > bb[i])
			{
				flag = 1; break;
			}
	}
}

void print()
{
	bool start = false;
	for (int i = 23; i >= 0; i--)
	{
		if (num[i] != 0)
			start = true;
		if (start)
			printf("%c", getchar(num[i]));
	}
	printf("\n");
}

void solve()
{
	flag = -1;
	if (a[0] == '-' && b[0] == '+')
	{
		printf("%s %s\n", a, b);
		compare();
		if (flag == -1)
			printf("+0\n");
		else if (flag == 0)
		{
			printf("+");
			for (int i = 0; i < lb - 1; i++)
			{
				if (bb[i] >= aa[i])
					num[i] = bb[i] - aa[i];
				else if (bb[i] < aa[i])
				{
					bb[i] += 16; bb[i + 1]--; 
					for (int j = i + 1; j + 1 < lb - 1; j++)
					{
						if (bb[j] < 0)
							bb[j] += 16, bb[j + 1]--;
						else break;
					}
					num[i] = bb[i] - aa[i];
				}
			}
			print();
		}
		else
		{
			printf("-");
			for (int i = 0; i < la - 1; i++)
			{
				if (aa[i] >= bb[i])
					num[i] = aa[i] - bb[i];
				else if (aa[i] < bb[i])
				{
					aa[i] += 16; aa[i + 1]--; 
					for (int j = i + 1; j < la - 1; j++)
						if (aa[j] < 0)
							aa[j] += 16, aa[j + 1]--;
						else break;
					num[i] = aa[i] - bb[i];
				}
			}
			print();
		}
		
	}
	else if (a[0] == '+' && b[0] == '-')
	{
		printf("%s %s\n", b, a);
		compare();
		if (flag == -1)
			printf("+0\n");
		else if (flag == 0)
		{
			printf("-");
			for (int i = 0; i < lb - 1; i++)
			{
				if (bb[i] >= aa[i])
					num[i] = bb[i] - aa[i];
				else if (bb[i] < aa[i])
				{
					bb[i] += 16; bb[i + 1]--; 
					for (int j = i + 1; j < lb - 1; j++)
						if (bb[j] < 0)
							bb[j] += 16, bb[j + 1]--;
						else break;
					num[i] = bb[i] - aa[i];
				}
			}
			print();
		}
		else
		{
			printf("+");
			for (int i = 0; i < la - 1; i++)
			{
				if (aa[i] >= bb[i])
					num[i] = aa[i] - bb[i];
				else if (aa[i] < bb[i])
				{
					aa[i] += 16; aa[i + 1]--; 
					for (int j = i + 1; j < la - 1; j++)
						if (aa[j] < 0)
							aa[j] += 16, aa[j + 1]--;
						else break;
					num[i] = aa[i] - bb[i];
				}
			}
			print();
		}
	}
	else
	{
		compare();
		if (a[0] == '+')
		{
			if (flag == 0 || flag == -1)
				printf("%s %s\n", a, b);
			else
				printf("%s %s\n", b, a);
			printf("+");
		}
		else
		{
			if (flag == 0 || flag == -1)
				printf("%s %s\n", b, a);
			else
				printf("%s %s\n", a, b);
			printf("-");
		}
		for (int i = 0; i < 23; i++)
		{
			num[i] += aa[i] + bb[i];
			if (num[i] >= 16)
				num[i] -= 16, num[i + 1]++;
		}
		print();
	}
}

int main()
{
	while (scanf("%s%s", a, b) != EOF)
	{
		if (a[1] == '0' && b[1] == '0')
			break;
		make();
		solve();
		getchar();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值