⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lgint.h

📁 一个超大数的实现类,提供了加减乘除,功能比较完善,应该没有什么Bug
💻 H
字号:
#include<iostream>
#include<string>
using namespace std;

class lgint{
public:
	lgint(){}
	lgint(string n)
	{
		numb.clear();
		int i=n.length();
		for(int j=i-1;j>=0;--j)
		{
			numb.push_back(n[j]);
		}
	}

	lgint	operator +(lgint n);
	lgint	operator -(lgint n);
	lgint	operator *(lgint n);
	lgint	operator /(lgint n);
	lgint	operator =(string n);

	string toString()
	{
		return swaps(numb);
	}

	string get_sd()
	{
		return numb;
	}

private:
	string numb;

	string swaps(string n)
	{
		string st;
		int i=n.length();
		for(int j=i-1;j>=0;--j)
		{
			st.push_back(n[j]);
		}
		return st;
	}

	int toNumb(char c)
	{
		switch(c){
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
case '0':return 0;
		}
	}

	char toChar(int i)
	{
		switch(i){
case 1:return '1';
case 2:return '2';
case 3:return '3';
case 4:return '4';
case 5:return '5';
case 6:return '6';
case 7:return '7';
case 8:return '8';
case 9:return '9';
case 0:return '0';
		}
	}
};

lgint lgint::operator+(lgint n)
{
	string nt=n.get_sd();
	int ln=nt.length();
	int lnu=numb.length();
	int length=(lnu>ln)?ln:lnu;

	string fnl;

	int sp;
	char k1,k2;
	int t=0;

	for(sp=0;sp<=length-1;++sp)
	{
		k1=toNumb(numb.c_str()[sp]);
		k2=toNumb(nt.c_str()[sp]);

		if(k1+k2+t>=10)
		{	fnl.push_back(toChar((k1+k2+t)%10));

		t=(k1+k2+t)/10;}
		else
		{
			fnl.push_back(toChar(k1+k2+t));
			t=0;
		}
	}

	if(lnu>ln)
	{

		for(int i=length;i<=lnu-1;++i)
		{
			fnl.push_back(

				toChar(

				(toNumb(   numb.c_str()[i])+t  )%10

				)

				);

			t=(toNumb(   numb.c_str()[i])+t  )/10;
		}

	}
	else if(ln>lnu)
	{

		for(int i=length;i<=ln-1;++i)
		{

			fnl.push_back(

				toChar(

				(toNumb(   nt.c_str()[i])+t  )%10

				)

				);

			t=(toNumb(   nt.c_str()[i])+t  )/10;
		}

	}




	if(t!=0)fnl.push_back('1');

	lgint ed(swaps(fnl));
	return ed;


}

///////////////////////////////////////////
lgint lgint::operator=(const string n)
{


	numb.clear();
	int i=n.length();
	for(int j=i-1;j>=0;--j)
	{
		numb.push_back(n[j]);
	}

	return *this;

}

//////////////////////////////////////////////////////////

lgint lgint::operator -(lgint n)
{

	string nt=n.get_sd();
	int ln=nt.length();
	int lnu=numb.length();

	string max,min;

	max=numb;
	min=nt;

	if(lnu>ln)
	{
		max=numb;
		min=nt;

	}
	else if(lnu<ln)
	{
		max=nt;
		min=numb;

	}
	else
	{
		for(int i=0;i<=ln;++i)
		{
			if(          toNumb(numb.c_str()[i])         >         toNumb(nt.c_str()[i])       )
			{
				max=numb;
				min=nt;
				break;

			}
			else if(          toNumb(numb.c_str()[i])         <        toNumb(nt.c_str()[i])       )
			{
				min=numb;
				max=nt;
				break;

			}
		}

	}

	ln=min.length();
	lnu=max.length();

	int length=(lnu>ln)?ln:lnu;

	string fnl;
	//string::iterator sp;
	int sp;

	char k1,k2;
	int t=0;

	for(sp=0;sp<=length-1;++sp)
	{
		k1=toNumb(max.c_str()[sp]);
		k2=toNumb(min.c_str()[sp]);

		if(k1-k2-t<0)
		{
			k1+=10;
			fnl.push_back(toChar(k1-k2-t));
			t=1;

		}
		else
		{
			fnl.push_back(toChar(k1-k2-t));
			t=0;

		}



	}

	if(lnu>ln)
	{

		for(int i=length;i<=lnu-1;++i)
		{

			if(   toNumb(   max.c_str()[i])-t>=0  )
			{

				fnl.push_back(			toChar(	(toNumb(   max.c_str()[i])-t)  )		);
				t=0;



			}

			else
			{
				fnl.push_back(			toChar(	(toNumb(   max.c_str()[i])-t+10  )	)		);
				t=1;


			}


		}

	}
	else if(ln>lnu)
	{

		for(int i=length;i<=ln-1;++i)
		{

			if(   toNumb(   min.c_str()[i])-t>=0  )
			{
				for(int j=i;j<=lnu-1;++j)
					fnl.push_back(			toChar(	(toNumb(   min.c_str()[i])-t  )	)		);


			}

			else
			{
				fnl.push_back(			toChar(	(toNumb(   min.c_str()[i])-t+10  )	)		);
				t=1;


			}
		}

	}


	//if(max!=numb)fnl.push_back('-');

	lgint ed(swaps(fnl));
	return ed;

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -