C语言程序设计基础--高精度计算

高精度计算

涉及知识点:数组、流程控制、函数等
要求:用整型数组表示10进制大整数(超过2^32的整数),数组的每个元素存储大整数的一位数字,实现大整数的加减法
这世上最痛苦的事情莫过于读别人的代码,所以我尽量写了写注释

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include <iostream>
using namespace std;
string add(string a, string b);//加法
string subtract(string a, string b);//减法
int main()
{
	string m, n;
	for (int i = 0; i < 10; i++)
	{
		cin >> m >> n;//输入原始数据
		cout << "和为: " << add(m, n);//输出和
		cout << endl;
		cout << "差为: " << subtract(m, n) << endl;//输出差

	}
	return 0;
}
string add(string a, string b)
{
	string result;
	int i;
	bool x = true, y = true;
	if (a[0] == '-') x = false;
	if (b[0] == '-') y = false;
	int length_A = a.size();
	int length_B = b.size();
	if (x && y)//正数加正数
	{
		if (length_A < length_B)
		{
			for (i = 0; i < length_B - length_A; i++)
			{
				a = '0' + a;
			}
		}
		else
		{
			for (i = 0; i < length_A - length_B; i++)
			{
				b = '0' + b;
			}
		}
		int length = a.size();
		int carry = 0;//进位
		int temp;//保存相同位相加和
		for (i = length - 1; i >= 0; i--)
		{
			temp = a[i] - '0' + b[i] - '0' + carry;
			carry = temp / 10;
			temp %= 10;
			result = char(temp + '0') + result;
		}
		if (carry != 0)
		{
			result = char(carry + '0') + result;
		}
		return result;
	}
	else if (x == false && y == true)//负数加正数
	{
		a.erase(0, 1);//去掉负号,两数相减
		return subtract(b, a);//递归
	}
	else if (x == true && y == false)//正数加负数
	{
		b.erase(0, 1);//去掉符号,两数相减
		return subtract(a, b);//递归
	}
	else//负数加负数
	{
		a.erase(0, 1);
		b.erase(0, 1);
		result = add(a, b);
		result = '-' + result;
		return result;
	}
}
string subtract(string a, string b)//减法
{
	string result;
	int i = 0;
	bool x = true, y = true, z = true,equal=false;
	if (a[0] == '-') x = false;//a是负数,x变为false
	if (b[0] == '-') y = false;//b是负数,y变为false
	int length_A = a.size();
	int length_B = b.size();
	if (x && y)//正数减正数
	{
		if (length_A < length_B)
		{
			for (i = 0; i < length_B - length_A; i++)
			{
				a = '0' + a;
			}
			z = false;//z默认代表a比b长,如果a比b短,z为flase
		}
		else if (length_A > length_B)
		{
			for (i = 0; i < length_A - length_B; i++)
			{
				b = '0' + b;
			}
		}
		else
			equal = true;//如果a,b等长,equal为true
		int length = a.size();
		int temp;
		int abdicate = 0;
		if (z == true&&equal==false)//a比b长
		{
			for (i = length - 1; i >= 0; i--)
			{
				temp = a[i] - b[i];
				if (temp >= 0)
				{
					result = char(temp + '0') + result;
				}
				else
				{
					a[i - 1]--;
					temp += 10;
					result = char(temp + '0') + result;
				}
			}
			
		}
		else if(z==false&&equal==false)//a比b短
		{

			for (i = length - 1; i >= 0; i--)
			{
				temp = b[i] - a[i];
				if (temp >= 0)
				{
					result = char(temp + '0') + result;
				}
				else
				{
					b[i - 1]--;
					temp += 10;
					result = char(temp + '0') + result;
				}
			}
			
			result = '-' + result;
		}
		if (equal)//a,b等长
		{
			if (a > b)//类似于strcmp()函数
			{
				for (i = length - 1; i >= 0; i--)
				{
					temp = a[i] - b[i];
					if (temp >= 0)
					{
						result = char(temp + '0') + result;
					}
					else 
					{
						a[i - 1]--;
						temp += 10;
						result = char(temp + '0') + result;
					}
				}
			}
			else if (a < b)
			{
				for (i = length - 1; i >= 0; i--)
				{
					temp = b[i] - a[i];
					if (temp >= 0)
					{
						result = char(temp + '0') + result;
					}
					else
					{
						b[i - 1]--;
						temp += 10;
						result = char(temp + '0') + result;
					}
				}
				result = '-' + result;
			}
			else
				result = "0";
		}
		return result;
	}
	else if (x == false && y == true)//负数减正数
	{
		a.erase(0, 1);
		result='-'+ add(a, b);
		return result;
	}
	else if (x == true && y == false)//正数减负数
	{
		b.erase(0, 1);
		return add(a, b);
	}
	else//负数减负数
	{
		a.erase(0, 1);
		b.erase(0, 1);
		return subtract(b, a);
	}
}

ps:如有错误敬请指正,欢迎评论区交流或者发私信
邮箱1654407501@qq.com,QQ号同邮箱

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怡人蝶梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值