OOP实现大整数的加减

这里介绍的只是大整数的加减法,加减法相对来说比较容易理解;
如果一个数的前面有很多0或者有空格的话,要删除。
加法:要考虑到进位的情况,循环的时候 ,要从最低位开始,还得考虑两个数的数位长短问题;
减法:要考虑到借位的情况,还得判断两个数的大小问题,后面的就和加法差不多了。

#include "stdafx.h"
#include<iostream> //c++的i/o输入和输出
using namespace std;
#include<assert.h>
#include<stdio.h>
#include<iterator>
#include<vector>  //包含C++   STL向量容器的代码
#include<stdlib.h>

//大数的加减操作

class BigInt {
public:
	BigInt() {}
	BigInt(string str):strBigit(str)
	{
		Delete_();
	}
	void Delete_()   //删除多余空格 和  删除大数高位的 ‘0’
	{
		string::iterator it = strBigit.begin();

		if (it == strBigit.end())//只有一位数
		{
			return;
		}
		if ('0' == *it && strBigit.end() != it + 1)
		{
			return;
		}
		//删除多余空格
		while (it != strBigit.end())
		{
			it = find(it, strBigit.end(), ' ');//找到空格 并删除
			it = strBigit.erase(it);
		}

		//删除大数高位的'0'
		it = strBigit.begin();
		if ('-' == *it)
		{
			++it;
		}
		while (it != strBigit.end())
		{
			if ('0' == *it)
			{
				if (strBigit.end() == it + 1)
				{
					break;
				}
				it = strBigit.erase(it);
			}
			else
			{
				break;
			}
		}

	}
private:
	string strBigit;  //使用字符串存储大整数
	friend ostream& operator<<(ostream &, const BigInt&);  //打印函数
	friend BigInt operator+(const BigInt &, const BigInt&);  //加法
	friend BigInt operator-(const BigInt &, const BigInt&);  //减法

};

ostream& operator<<(ostream &out, const BigInt&src)
{
	out << src.strBigit.c_str;
	return out;
}
BigInt operator+(const BigInt &lhs, const BigInt &rhs)
{
	//从后往前遍历lhs和rhs,进行相加操作,相加的过程中,考虑进位操作
	
	bool flag = false;
	int size1 = lhs.strBigit.length();  //分别计算两个数的长度
	int size2 = rhs.strBigit.length();
	int i = size1 - 1, j = size2 - 1;

	vector<int> result;
	int sum = 0;

	for (; i >= 0 && j >= 0; --i, --j)  //两个数对齐的每一位都相加并看是否进位
	{
		int num1 = lhs.strBigit[i] - '0';
		int num2 = rhs.strBigit[j] - '0';
		if (flag)       //如果sum 大于等于10  则进行进行操作
		{
			sum = num1 + num2 + 1;
			flag = false;
		}
		else
		{
			sum = num1 + num2;
		}

		if (sum >= 10)
		{
			flag = true;
		}
		result.push_back(sum % 10);  //把结果保存在容器中
	}

	//那段数字长,考虑进位继续添加
	for (; i >= 0; --i)
	{
		int num = lhs.strBigit[i] - '0';
		if (flag)
		{
			sum = num + 1;
			flag = false;
		}
		else
		{
			sum = num;
		}
		if (sum >= 10)
		{
			flag = true;
		}
		result.push_back(sum % 10);
	}
	for (; j >= 0; --j)
	{
		int num = lhs.strBigit[j] - '0';
		if (flag)
		{
			sum = num + 1;
			flag = false;
		}
		else
		{
			sum = num;
		}
		if (sum >= 10)
		{
			flag = true;
		}
		result.push_back(sum % 10);
	}
	if (flag)
	{
		result.push_back(1);  //第一位加1
	}

	string str = "";
	for (int i = result.size() - 1; i >= 0; --i)
	{
		str.push_back(result[i] + '0');
	}
	return str;
}
BigInt operator-(const BigInt &lhs, const BigInt &rhs)
{
	//减法比加法不同的一点 就是要考虑到借位的情况
	bool flag = false;
	bool isMinor = false;
	int size1 = lhs.strBigit.length();
	int size2 = rhs.strBigit.length();
	int i = 0, j = 0;
	string str = "";

	vector<int> result;
	BigInt big, small;
	int minor = 0;

	if (size1 > size2)  //lhs和rhs长度相比  三种情况
	{
		big = lhs;
		small = rhs;
	}
	else if(size1 < size2)
	{
		big = rhs;
		small = lhs;
		isMinor = true;
	}
	else  //长度相等的话
	{
		for (; i < size1 && j < size2; ++i, ++j)
		{
			if (lhs.strBigit[i] > rhs.strBigit[j])
			{
				big = lhs;
				small = rhs;
				break;
			}
			else if (lhs.strBigit[i] < rhs.strBigit[j])
			{
				big = rhs;
				small = lhs;
				isMinor = true;
				break;
			}
		}
		if (i == size1 && j == size2)
		{
			str = "0";
			return str;
		}
	}

	i = big.strBigit.length() - 1;
	j = small.strBigit.length() - 1;
	for (; i >= 0 && j >= 0; --i, --j)
	{
		int num1 = big.strBigit[i] - '0';
		int num2 = small.strBigit[j] - '0';
		if (flag)
		{
			minor = num1 - 1 - num2;
			flag = false;
		}
		else
		{
			minor = num1 - num2;
		}
		if (minor < 0)  //发生借位的情况
		{
			flag = true;
			minor += 10;
		}
		result.push_back(minor);
	}

	for (; i >= 0; --i)
	{

	}
	if (isMinor)
	{
		result.push_back('-' - '0');
	}

	
	for (int i = result.size() - 1; i >= 0; --i)
	{
		str.push_back(result[i] + '0');
	}
	return str;
}

int main()
{
	// atoi '9' - '0' = 9
	BigInt int1 = 28937697857832167849697653231243;
	BigInt int2 = -9785645649886874535428765;
	
	//cout << int1 + int2 << endl;
	cout << int1 - int2 << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值