天天看点

c++面向对象:自己实现大数类Bigint(各种符号+重载加减乘除)c++实现高精度大数类的加减乘除

c++实现高精度大数类的加减乘除

加减乘都比较常见,

这里还实现了一个大数类的除法,是我自己想的,以下说明下思路:

c++面向对象:自己实现大数类Bigint(各种符号+重载加减乘除)c++实现高精度大数类的加减乘除

我习惯把this指针写上,这个相当于python中的self

对于加法,正加正,负加负,负加正,正加负都得

对于其他也是一样

注意这里的除法是整除

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

class Bigint{
private:
	vector<int> num;
	int sign = 1;
public:
	void set_value(string &t){
		int tt = 0;
		if (t[0] == '-'){
			tt++;
			this->sign = -1;
		}
		for (int i = t.size() - 1; i >= tt; i--) this->num.push_back(t[i] - '0');
	};

	void set_num(vector<int> &t){
		this->num = t;
	};

	void show(){
		if (this->sign == -1)cout << '-';
		for (int i = this->get_num().size()-1; i >= 0; i--)cout << this->num[i];
	};

	int get_sign(){
		return this->sign;
	}

	void set_sign(int s){
		this->sign = s;
	}

	vector<int> get_num(){
		return this->num;
	}

	Bigint to_bigint(int t){
		Bigint c;
		vector<int> tmp;
		if (!t){
			tmp.push_back(0);
			c.set_num(tmp);
			return c;
		}
		int k = 1;
		if (t < 0)
			k = -1;
		t *= k;
		while (t)tmp.push_back(t % 10), t /= 10;
		c.set_num(tmp);
		c.set_sign(k);
		return c;
	}
	
	Bigint operator+(Bigint &y){
		if (this->get_sign() == -1 && y.get_sign() == 1){
			this->set_sign(1);
			return y-*this;
		}
		if (this->get_sign() == 1 && y.get_sign() == -1){ 
			y.set_sign(1);
			return *this-y; 
		}

		if (this->get_num().size() < y.get_num().size())return y+*this;
		Bigint c;
		if (this->get_sign() == -1 && y.get_sign() == -1)c.set_sign(-1);
		vector<int> tmp;
		int t = 0;
		for (int i = 0; i < this->get_num().size(); i++){
			t += this->get_num()[i];
			if (i < y.get_num().size())t += y.get_num()[i];
			tmp.push_back(t % 10);
			t /= 10;
		}
		if (t)tmp.push_back(1);
		c.set_num(tmp);
		return c;
	}

	bool bigger_than(Bigint &t){
		if (this->get_num().size() != t.get_num().size())return this->get_num().size() > t.get_num().size();
		for (int i = this->get_num().size()-1; i >= 0; i--)
			if (this->get_num()[i] != t.get_num()[i])return this->get_num()[i] > t.get_num()[i];
		return false;
	}

	bool equale(Bigint &t){
		if (this->get_num().size() != t.get_num().size())return false;
		for (int i = this->get_num().size() - 1; i >= 0; i--)
			if (this->get_num()[i] != t.get_num()[i])return false;
		return true;
	}

	Bigint operator-(Bigint &y){
		Bigint zero = zero.to_bigint(0);
		if (this->get_sign() == y.get_sign() && this->equale(y))return zero;
		if (this->get_sign() == -1 && y.get_sign() == -1){
			y.set_sign(1);
			return y + *this;
		}
		if (this->get_sign() == 1 && y.get_sign() == -1){
			y.set_sign(1);
			return *this + y;
		}
		if (sign == -1 && y.get_sign() == 1){
			y.set_sign(-1);
			return *this + y;
		}
		Bigint c;
		if (!(this->bigger_than(y))){
			c = y - *this;
			c.set_sign(-1);
			return c;
		}
		vector<int> tmp;
		for (int i = 0, t = 0; i < this->get_num().size(); i++){
			t = this->get_num()[i] - t;
			if (i < y.get_num().size())t -= y.get_num()[i];
			tmp.push_back((t + 10) % 10);
			if (t < 0)t = 1;
			else t = 0;
		}
		while (tmp.size() > 1 && tmp.back() == 0)tmp.pop_back();
		c.set_num(tmp);
		return c;
	};

	Bigint operator*(Bigint &y){
		Bigint c;
		vector<int> tmp(this->num.size()+y.get_num().size());
		if ((this->num.size() == 1 && num[0] == 0) || y.get_num().size() == 1 && y.get_num()[0] == 0){
			c.set_sign(1);
			tmp[0]=0;
			while (tmp.size() > 1)tmp.pop_back();
			c.set_num(tmp);
			return c;
		}
		if (this->get_sign() == -1 && y.get_sign() == -1){
			c.set_sign(1);
		}
		if (this->get_sign() == 1 && y.get_sign() == -1){
			c.set_sign(-1);
		}
		if (sign == -1 && y.get_sign() == 1){
			c.set_sign(-1);
		}
		int t = 0;
		for (int i = 0; i < this->num.size(); i++)
			for (int j = 0; j < y.get_num().size(); j++)
				tmp[i + j] += this->num[i] * y.get_num()[j];
		for (int i = 0; i < tmp.size();i++)
			if (tmp[i]>9){
				tmp[i + 1] += tmp[i] / 10;
				tmp[i] %= 10;
			}
		while (tmp.size()>1 && tmp.back() == 0)tmp.pop_back();
		c.set_num(tmp);
		return c;
	};

	//除法一般是大数除小数的吧?两个数都是大整形,被除数一般会比除数大吧?不然只有余数了
	//先确定一个大数,然后二分
	//具体看图
	Bigint operator/(Bigint &y){
		Bigint c, d;
		vector<int> tmp, a, tmpp;
		Bigint power = power.to_bigint(1);
		Bigint ten = ten.to_bigint(10);
		Bigint two = two.to_bigint(2);
		Bigint one = one.to_bigint(1);
		Bigint zero = zero.to_bigint(0);
		if (this->get_sign() == -1 && y.get_sign() == -1){
			c.set_sign(1);
		}
		if (this->get_sign() == 1 && y.get_sign() == -1){
			c.set_sign(-1);
		}
		if (sign == -1 && y.get_sign() == 1){
			c.set_sign(-1);
		}
		if ((this->equale(y)))return one;
		if ((this->num.size() == 1 && num[0] == 0) || (!(this->bigger_than(y))))return zero;

		while (!((power*y).bigger_than(*this))){
			if ((power*y).equale(*this))return power;
			if((power * ten *y).bigger_than(*this))break;
			power = power*ten;
		}
		d = power * y;

		Bigint e, f, ans, res;
		ans = power;
		
		for (int i = 2; i <= 9; i++){
			e = e.to_bigint(i);
			f = d*e;
			if (f.bigger_than(*this)){
				e = e.to_bigint(i - 1);
				ans = ans*e;
				d = d*e;
				break;
			}
			else if (f.equale(*this))
				return ans*e;
		}
		res = d;
		while ((*this-d).bigger_than(y)){
			power = power / two;
			if (power.equale(zero))power = power.to_bigint(1);
			res = d + (y*power);
			if (res.bigger_than(*this))continue;
			else if (res.equale(*this))return ans + power;
			else{
				ans = ans + power;
				
				d = res;
			}
			
		}
		if ((*this - d).equale(y)){
			ans = ans + one;
		}
		
		return ans;
	}
	//了逼个的妈,终于调出来了,妈他的逼他
};

int main(){
	Bigint a, b;
	string A, B;
	cin >> A >> B;
	a.set_value(A);
	b.set_value(B);
	Bigint c;
	c = a/b;
	c.show();
	getchar(); getchar();
	return 0;
}
           

继续阅读