1037.Powerful Calculator

题目描述:
Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
输入:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
输出:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000

80000000000000000000000000000000


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool Judge(const string &a, const string &b) //判断字符串a是否大于等于字符串b
{
	int len_a = a.size();
	int len_b = b.size();
	if (len_a == len_b)
		return a >= b;
	else
		return len_a > len_b;
}

string Plus(const string &a, const string &b)
{
	int len = max(a.size(), b.size());
	vector<int> ivec(len + 1, 0); //最大位数要么是len,要么是len + 1;
	string s(len + 1, '0'); //最后的计算结果
	for (int i = a.size() - 1, j = b.size() - 1; len > 0; --i, --j, --len)
	{
		ivec[len] = (i >= 0 ? a[i] - '0' : 0) + (j >= 0 ? b[j] - '0' : 0);
	}

	for (int i = ivec.size() - 1; i >= 0; --i)
	{
		if (ivec[i] >= 10)
		{
			ivec[i - 1] += 1;
		}
		s[i] = ivec[i] % 10 + '0';
	}

	if (s[0] == '0')
		s.erase(s.begin());
	return s;
}

string Minus(const string &a, const string &b)
{
	string s;
	if (!Judge(a, b))
	{
		s = Minus(b, a);
		s.insert(s.begin(), '-');
	}
	else
	{
		int len = max(a.size(), b.size());
		vector<int> ivec(len, 0); //最大位数要么是len,要么是len - 1;
		s.assign(len, '0'); //最后的计算结果
		for (int i = a.size() - 1, j = b.size() - 1; len > 0; --i, --j, --len)
		{
			ivec[len - 1] = (i >= 0 ? a[i] - '0' : 0) - (j >= 0 ? b[j] - '0' : 0);
		}

		for (int i = ivec.size() - 1; i >= 0; --i)
		{
			if (ivec[i] < 0)
			{
				ivec[i - 1] -= 1;
			}
			s[i] = (ivec[i] + 10) % 10 + '0';
		}
	}

	string::size_type pos = s.find_first_not_of('0'); //解决差全0的问题
	if (pos == string::npos)
		s.assign(1, '0');
	return s;
}

string Multi(const string &a, const string &b)
{
	int len = a.size() + b.size();
	vector<int> ivec(len, 0); //最大位数要么是len,要么是len - 1;
	string s(len, '0'); //最后的计算结果
	for (int i = 0; i != a.size(); ++i)
	{
		for (int j = 0; j != b.size(); ++j)
		{
			ivec[i + j + 1] += (a[i] - '0') * (b[j] - '0');    //注意这里是+=
		}
	}

	for (int i = ivec.size() - 1; i >= 0; --i)
	{
		if (ivec[i] > 10)
		{
			ivec[i - 1] += ivec[i] / 10; //注意一下
		}
		s[i] = ivec[i] % 10 + '0';
	}

	if (s[0] == '0')
		s.erase(s.begin());
	return s;
}

int main()
{
	string a, b;
	while (cin >> a >> b)
	{
		cout << Plus(a, b) << endl;
		cout << Minus(a, b) << endl;
		cout << Multi(a, b) << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值