C++重载运算符的高精度模板

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
bool c=false;
class high_pricision{
	public:
		int a[100001],lenth;
	void read()
	{
		memset(a,0,sizeof(a));
		char s[100001];
		int k=0;
		cin>>s;
		lenth=strlen(s);
		for(int i=0;i<lenth;i++)
		  a[i]=s[lenth-i-1]-'0';
	}
	void write()
	{
		if(c==true&&(lenth>-1||a[0]!=0)) cout<<'-';
		for(int i=lenth+1;i>=0;i--)
		  cout<<a[i];
	}
	
	high_pricision operator +(const high_pricision &num)const//num代表第二个操作数 
	{
		high_pricision ans;
		ans.lenth=max(lenth,num.lenth);
		for(int i=0;i<ans.lenth;i++)
		{
			ans.a[i]+=num.a[i]+a[i];
			if(ans.a[i]>=10)
			{
				ans.a[i]-=10;
				ans.a[i+1]+=1; 
			}
		}
		int k=ans.lenth+1;
		while(ans.a[k--]==0&&k>=0);
		ans.lenth=k;
		return ans;
	}
	high_pricision operator -(const high_pricision &num)const
	{
		high_pricision ans;
		ans.lenth=max(lenth,num.lenth);
		for(int i=0;i<ans.lenth;i++)
		{
			if(c==true)  ans.a[i]=num.a[i]-a[i];
			else ans.a[i]=a[i]-num.a[i];
		}
		for(int i=0;i<ans.lenth;i++)
		{
			if(ans.a[i]<0&&ans.lenth!=1)
			{
				ans.a[i]+=10;
				ans.a[i+1]-=1;
			}
		}
		int k=ans.lenth;
		while(ans.a[k--]==0&&k>=0);
		ans.lenth=k;
		return ans;
	}
	high_pricision operator *(const high_pricision &num)const
	{
		high_pricision ans;
		for(int i=0;i<lenth;i++)
		{
			int mo=0;
			for(int j=0;j<num.lenth;j++)
			{
				ans.a[i+j]=mo+a[i]*num.a[j]+ans.a[i+j];
				mo=ans.a[i+j]/10;
				ans.a[i+j]%=10;
			}
			ans.a[i+num.lenth]=mo;
		}
		int k=lenth+num.lenth;
		while(ans.a[k--]==0&&k>=0);
		ans.lenth=k;
		return ans;
	}
	high_pricision operator /(const long long &num)const//高精除单精,取整 
	{
		high_pricision ans;
		if(c==true)
		{
			ans.a[0]=0;
			ans.lenth=-1;
			return ans;
		}
		int mo=0;
		for(int i=0;i<lenth;i++)
		{
			ans.a[i]=ans.a[i]+((mo*10+a[i])/num);
			mo=(mo*10+a[i])%num;
		}
		int k=lenth;
		while(ans.a[k--]==0&&k>=0);
		ans.lenth=k;
		return ans;
	}
}; 

void change(int *f,int *s,int fl,int sl);

int main()
{
	high_pricision fir,sec,ans;
	char mod;
	cout<<"Enter one number,the mode and the other number:\n"; 
	fir.read();
	cin>>mod;
	sec.read();
	if(mod=='-')
	{
	  change(fir.a,sec.a,fir.lenth,sec.lenth);
	  ans=fir-sec;
	}
	if(mod=='+') ans=fir+sec;
	if(mod=='*') ans=fir*sec;
	if(mod=='/') 
	{
	  change(fir.a,sec.a,fir.lenth,sec.lenth);
	  long long float_sec=0;
	  for(int i=sec.lenth-1;i>=0;i--)
	  	float_sec=(float_sec*10)+sec.a[i];
	  ans=fir/float_sec;	
	}
	ans.write();
	return 0;
}

void change(int *f,int *s,int fl,int sl)
{
	if(fl>sl) return;
	else if(fl<sl) c=true;
	else if(fl==sl)
	{
	   for(int i=fl-1;i>=0;i--)
	   {
	   	 if(f[i]==s[i]) continue;
	   	 else if(f[i]<s[i])
	   	 {
	   	 	c=true;
	   	 	break;
	   	 }
	   	 else if(f[i]>s[i]) break;
	   }
	}
}

其中高精度加减已经在洛谷和oj测试通过,代码部分参考了《c++一本通》中的模板

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值