求调高精度
  • 板块灌水区
  • 楼主czm708033
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/25 17:39
  • 上次更新2023/11/25 19:43:23
查看原帖
求调高精度
94046
czm708033楼主2023/11/25 17:39

C++,恢复main函数内的注释c=a+b;就会出问题。求高手解答,谢谢。

代码如下:

#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstdarg>
#include<cstring>
#include<iostream>
using std::cerr;
using std::endl;
int InputEnd=0;
struct BigInteger{
	char A[100010];
	int num;
	BigInteger(){};
	BigInteger(int a){
		memset(this->A,0,sizeof(this->A));
		if ( a<0 ){
			this->A[0]='-';
			a=-a;
		}
		else
			this->A[0]='+';
		this->num=0;
		while ( a>0 ){
			this->A[++this->num]=a%10;
			a/=10;
		}
	}
	BigInteger Check(){// fix this
		while ( this->num>0 && this->A[this->num]==0 )
			this->num--;
		for ( int i=1 ; i<=this->num ; i++ ){
			this->A[i+1]+=this->A[i]/10;
			this->A[i]%=10;
			if ( this->A[i]<0 && (i!=this->num || this->A[i+1]) ){
				this->A[i+1]--;
				this->A[i]+=10;
			}
			if ( this->A[this->num+1]!=0 )
				this->num++;
		}
		while ( this->num>0 && this->A[this->num]==0 )
			this->num--;
		if ( this->A[this->num]<0 ){
			this->A[0]='-'+'+'-this->A[0];
			for ( int i=1 ; i<=this->num ; i++ )
				this->A[i]=-this->A[i];
			this->Check();
		}
		return *this;
	}
	BigInteger operator=(BigInteger a){// this=a
		this->num=a.num;
		memcpy(this->A,a.A,sizeof(this->A));
		return *this;
	}
	BigInteger operator=(int a){// this = (BigInteger)a
		*this=BigInteger(a);
		return *this;
	}
	int Input(){// scanf(this)  return 1 if success while 0 if fail
		if ( InputEnd )
			return 0;
		char r=0;
		*this=0;
		do{
			r=getchar();
			if ( !~r ){
				InputEnd=1;
				return 0;
			}
		}while ( r==' ' || r=='\n' );
		if ( !isdigit(r) ){
			if ( r=='-' )
				this->A[0]=r;
			else
				return 0;
		}
		else
			this->A[++this->num]=r-'0';
		while ( 1 ){
			r=getchar();
			if ( !~r ){
				InputEnd=1;
				break;
			}
			else if ( !isdigit(r) )
				break;
			else
				this->A[++this->num]=r-'0';
		}
		for ( int i=1 ; i<=this->num>>1 ; i++ ){
			r=this->A[i];
			this->A[i]=this->A[this->num+1-i];
			this->A[this->num+1-i]=r;
		}
		return 1;
	}
	int Output(){// printf(this)
		this->Check();
		if ( this->A[0]=='-' )
			putchar('-');
		if ( this->num==0 )
			putchar('0');
		for ( int i=this->num ; i>0 ; i-- )
			putchar(this->A[i]+'0');
		return 1;
	}
	bool operator==(BigInteger a){
		this->Check();
		a.Check();
		if ( this->num!=a.num )
			return false;
		for ( int i=0 ; i<=this->num ; i++ )
			if ( this->A[i]!=a.A[i] )
				return false;
		return true;
	}
	bool operator>(BigInteger a){
		this->Check();
		a.Check();
		if ( this->A[0]=='+' && a.A[0]=='-' )
			return true;
		else if ( this->A[0]=='-' && a.A[0]=='+' )
			return false;
		else if ( this->A[0]=='+' && a.A[0]=='+' ){
			if ( this->num>a.num )
				return true;
			else if ( this->num<a.num )
				return false;
			else{
				for ( int i=this->num ; i>0 ; i-- )
					if ( this->A[i]>a.A[i] )
						return true;
					else if ( this->A[i]<a.A[i] )
						return false;
				return false;
			}
		}
		else{
			if ( this->num>a.num )
				return false;
			else if ( this->num<a.num )
				return true;
			else{
				for ( int i=this->num ; i>0 ; i-- )
					if ( this->A[i]>a.A[i] )
						return false;
					else if ( this->A[i]<a.A[i] )
						return true;
				return false;
			}
		}
	}
	bool operator>=(BigInteger a){
		return ((*this)>a)||((*this)==a);
	}
	bool operator<(BigInteger a){
		return !((*this)>=a);
	}
	bool operator<=(BigInteger a){
		return !((*this)>a);
	}
	BigInteger operator+=(BigInteger a){// this+=a; return this;
		if ( a.A[0]!=this->A[0] )
			for ( int i=1 ; i<=a.num ; i++ )
				this->A[i]-=a.A[i];
		else
			for ( int i=1 ; i<=a.num ; i++ )
				this->A[i]+=a.A[i];
		if ( a.num>this->num )
			this->num=a.num;
		this->Check();
		return *this;
	}
	BigInteger operator+(BigInteger a){// this+=a; return this;
		BigInteger ret=*this;
		ret+=a;
		return ret;
	}
	BigInteger operator-=(BigInteger a){// this-=a; return this;
		a.A[0]='-'+'+'-a.A[0];
		return (*this)+=a;
	}
	BigInteger operator-(BigInteger a){// this+=a; return this;
		a.A[0]='-'+'+'-a.A[0];
		return (*this)+a;
	}
	BigInteger operator*(BigInteger a){
		BigInteger res;
		res=0;
		for ( int i=1 ; i<=this->num ; i++ )
			for ( int j=1 ; j<=a.num ; j++ ){
				res.A[i+j-1]+=this->A[i]*a.A[j];
				res.A[i+j]+=res.A[i+j-1]/10;
				res.A[i+j-1]%=10;
			}
		res.num=this->num+a.num-1;
		res.A[0]=(this->A[0]==a.A[0])?'+':'-';
		res.Check();
		return res;
	}
	BigInteger operator*=(BigInteger a){// this*=a; return this;
		(*this)=(*this)*a;
		return *this;
	}
	BigInteger operator/=(BigInteger a){// return this%a;this/=a;
		if ( a==0 )
			return BigInteger(0);
		BigInteger res;
		res=0;
		char thiszf=this->A[0],azf=a.A[0];
		this->A[0]='+';
		a.A[0]='+';
		if ( (*this)<a ){
			this->A[0]=thiszf;
			(*this)=res;
			return *this;
		}
		int deltanum=this->num-a.num;
		for ( int i=a.num ; i>0 ; i-- )
			a.A[i+deltanum]=a.A[i];
		for ( int i=1 ; i<=deltanum ; i++ )
			a.A[i]=0;
		a.num+=deltanum;
		if ( (*this)<a ){
			if ( deltanum>0 )
				for ( int i=deltanum ; i<a.num ; i++ )
					a.A[i]=a.A[i+1];
			deltanum--;
			a.A[a.num]=0;
			a.num--;
		}
		res.num=deltanum+1;
		while ( deltanum>=0 ){
			res.A[deltanum+1]++;
			(*this)-=a;
			while ( (*this)<a && deltanum>=0 ){
				if ( deltanum>0 )
					for ( int i=deltanum ; i<a.num ; i++ )
						a.A[i]=a.A[i+1];
				deltanum--;
				a.A[a.num]=0;
				a.num--;
			}
		}
		(*this)=res;
		this->Check();
		if ( thiszf!=azf && this->num>0 )
			this->A[0]='+'+'-'-this->A[0];
		return *this;
	}
	BigInteger operator/(BigInteger a){
		BigInteger ret;
		ret=(*this);
		ret/=a;
		return ret;
	}
	BigInteger operator%(BigInteger a){// return this%a;this/=a;
		return (*this)-((*this)/a)*a;
	}
	BigInteger operator%=(BigInteger a){
		(*this)=(*this)%a;
		return *this;
	}
};
struct BigInteger a,b,c;
int main(){
	a.Input();
	b.Input();
//	c=a+b;
	a=a/b;
	a.Output();
	putchar('\n');
	return 0;
}
2023/11/25 17:39
加载中...