封装的高精度,乘法P1303过了,测试没过,求助
  • 板块学术版
  • 楼主N2MENT
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/9/3 21:51
  • 上次更新2023/11/4 08:03:56
查看原帖
封装的高精度,乘法P1303过了,测试没过,求助
401583
N2MENT楼主2021/9/3 21:51

输入例如

4 4
999 999

的数据就会出错 但是A*BProblem过了,求助大佬们。

#include<bits/stdc++.h>
using namespace std;
class LLLInt {
	protected:
		string number;
		bool isPlus;
	public:
		bool IsPlus() {
			return isPlus;
		}
		void GetNum() { //获取数字(含符号)
			string in;
			cin>>in;
			if(in[0]=='-') {
				isPlus=0;
				in.erase(0,1);
				number=in;
			} else {
				isPlus=1;
				number=in;
			}
		}
		string ShowInt() { //输出数字(含符号)
			if(isPlus==1)
				return number;
			string Rt=number;
			Rt="-"+Rt;
			return Rt;
		}
		string ShowString() { //输出数字(不含符号)
			return number;
		}
		void ChangeData(string in) { //更改数据(输入无符号)
			number=in;
		}
		void ChangeData(bool in) { //更改正负(符号)
			isPlus=in;
		}
		void ChangeData(string in,bool o) { //更改数据(输入符号)
			number=in;
			isPlus=o;
		}
		void ChangeInt(string in) {
			if(in[0]=='-') {
				isPlus=0;
				in.erase(0,1);
				number=in;
			} else {
				isPlus=1;
				number=in;
			}
		}
		LLLInt() {
			number="0";
			isPlus=1;
		}
		LLLInt(string in,bool o) {
			number=in;
			isPlus=o;
		}
		LLLInt(string in) {
			if(in[0]=='-') {
				isPlus=0;
				in.erase(0,1);
				number=in;
				if(in=="")
					number="0";
			} else {
				isPlus=1;
				number=in;
				if(in=="")
					number="0";
			}
		}
		bool operator <(const LLLInt& d) { //运算符重载(小于号)
			if(isPlus==0&&d.isPlus==1) {
				return true;
			}
			if(isPlus==1&&d.isPlus==0) {
				return false;
			}
			string str1=number;
			string str2=d.number;
			int len1=str1.length();
			int len2=str2.length();
			if(len1<len2) {
				return isPlus;
			}
			if(len1>len2) {
				return !isPlus;
			}
			for(int i=0; i<=len1; i++) {
				if(str1[i]<str2[i]) {
					return isPlus;
				}
				if(str1[i]>str2[i]) {
					return !isPlus;
				}

			}
			return false;
		}
		bool operator >(const LLLInt& d) { //运算符重载(大于于号)
			if(isPlus==0&&d.isPlus==1) {
				return 0;
			}
			if(isPlus==1&&d.isPlus==0) {
				return 1;
			}
			string str1=number;
			string str2=d.number;
			int len1=str1.length();
			int len2=str2.length();
			if(len1<len2) {
				return !isPlus;
			}
			if(len1>len2) {
				return isPlus;
			}
			for(int i=0; i<=len1; i++) {
				if(str1[i]<str2[i]) {
					return !isPlus;
				}
				if(str1[i]>str2[i]) {
					return isPlus;
				}

			}
			return false;
		}
		bool operator =(const LLLInt& d) {
			isPlus=d.isPlus;
			number=d.number;
		}
		bool operator ==(const LLLInt& d) {
			int a=number.length(),b=d.number.length();
			int s=max(a,b);
			if(isPlus!=d.isPlus)
				return false;
			for(int i=0; i<s; i++) {
				if(number[i]!=d.number[i])
					return false;
			}
			return true;
		}
		bool operator <=(const LLLInt& d) {
			if(isPlus==0&&d.isPlus==1) {
				return true;
			}
			if(isPlus==1&&d.isPlus==0) {
				return false;
			}
			string str1=number;
			string str2=d.number;
			int len1=str1.length();
			int len2=str2.length();
			if(len1<len2) {
				return isPlus;
			}
			if(len1>len2) {
				return !isPlus;
			}
			for(int i=0; i<=len1; i++) {
				if(str1[i]<str2[i]) {
					return isPlus;
				}
				if(str1[i]>str2[i]) {
					return !isPlus;
				}

			}
			return true;

		}
		bool operator >=(const LLLInt& d) { //运算符重载(大于于号)
			if(isPlus==0&&d.isPlus==1) {
				return 0;
			}
			if(isPlus==1&&d.isPlus==0) {
				return 1;
			}
			string str1=number;
			string str2=d.number;
			int len1=str1.length();
			int len2=str2.length();
			if(len1<len2) {
				return !isPlus;
			}
			if(len1>len2) {
				return isPlus;
			}
			for(int i=0; i<=len1; i++) {
				if(str1[i]<str2[i]) {
					return !isPlus;
				}
				if(str1[i]>str2[i]) {
					return isPlus;
				}

			}
			return true;
		}
};
void Debug(string a) {
	cout<<"?"<<a<<"?"<<endl;
}
void Debug(LLLInt a) {
	cout<<"?"<<a.ShowInt()<<"?"<<endl;
}
class operation {
	public:

		LLLInt APlusB(LLLInt A,LLLInt B) {
			if(A.IsPlus()==1&&B.IsPlus()==0) {
				B.ChangeData(1);
				return AMinusB(A,B);
			}
			if(A.IsPlus()==0&&B.IsPlus()==1) {
				A.ChangeData(1);
				return AMinusB(B,A);
			}
			string a=A.ShowString();
			string b=B.ShowString();
			string c;
			int lenA=a.length();
			int lenB=b.length();
			if(lenA<lenB) {
				for(int i=1; i<=lenB-lenA; i++)
					a="0"+a;
			} else {
				for(int i=1; i<=lenA-lenB; i++)
					b="0"+b;
			}
			lenA=a.length();
			int cf=0;
			int digit;
			for(int i=lenA-1; i>=0; i--) {
				digit=a[i]-'0'+b[i]-'0'+cf;
				cf=digit/10;
				digit%=10;
				c=char(digit+'0')+c;
			}
			if(cf!=0)
				c=char(cf+'0')+c;
			LLLInt Rt(c);

			if(A.IsPlus()==0&&B.IsPlus()==0)
				Rt.ChangeData(0);
			return Rt;
		}
		LLLInt AMinusB(LLLInt A,LLLInt B) {
			if(A<B) {
				swap(A,B);
			}
			if(A.IsPlus()==1&&B.IsPlus()==0) {
				B.ChangeData(1);
				LLLInt Rt=APlusB(A,B);
				Rt.ChangeData(0);
				return Rt;
			}
			if(A.IsPlus()==0&&B.IsPlus()==0) {
				swap(A,B);
			}
			string a=A.ShowString();
			string b=B.ShowString();
			int lenA=a.length();
			int lenB=b.length();
			if(lenA<lenB) {
				for(int i=1; i<=lenB-lenA; i++)
					a="0"+a;
			} else {
				for(int i=1; i<=lenA-lenB; i++)
					b="0"+b;
			}
			lenA=a.length();
			lenB=b.length();
			string c;
			int digit=0;
			int cf=0;
			for(int i=lenA-1; i>=0; i--) {
				digit=(a[i]-'0')-(b[i]-'0')-cf;
				cf=0;
				while(digit<0) {
					digit+=10;
					cf++;
				}
				c=char(digit+'0')+c;
			}
			for(int len=c.length(); len>1; len--) {
				if(c[0]=='0')
					c.erase(0,1);
				else
					break;
			}
			LLLInt Rt(c);
			if(A.IsPlus()==0&&B.IsPlus()==0) {
				Rt.ChangeData(0);
			}
			return Rt;

		}
		LLLInt AMultiplyB(LLLInt A,LLLInt B) {
			string a=A.ShowString();
			string b=B.ShowString();
			int f=1;
			if(A.IsPlus()!=B.IsPlus()) {
				f=0;
			}
			string ans;
			string stepAns;
			string zeros;
			int lena=a.length();
			int lenb=b.length();
			for(int i=lenb-1; i>=0; i--) {
				stepAns="";
				int digit;
				int cf=0;
				if(b[i]!='0')
					for(int j=lena-1; j>=0; j--) {
						digit=(a[j]-'0')*(b[i]-'0')+cf;
						cf=digit/10;
						digit%=10;
						stepAns=char(digit+'0')+stepAns;
					}
				stepAns=stepAns+zeros;

				for(int len=stepAns.length(); len>1; len--) {
					if(stepAns[0]=='0')
						stepAns.erase(0,1);
					else
						break;
				}
				LLLInt X(stepAns),Y(ans);
				Y=APlusB(X,Y);
				ans=Y.ShowString();
				zeros=zeros+"0";
			}

			LLLInt Rt;
			Rt.ChangeData(ans,f);
			return Rt;

		}
		LLLInt ADivisionB(LLLInt A,LLLInt B) {
			int s;
			if(B.ShowString()=="0") {
				printf("E");
				return A;
			}
			if(A<B) {
				LLLInt Rt("0");
				return Rt;
			}
			bool flag=1;
			if(A.IsPlus()!=B.IsPlus()) {
				flag=0;
			}
			A.ChangeData(true);
			B.ChangeData(true);
			string stra=A.ShowString(),strb=B.ShowString();
			int lena=stra.length(),lenb=strb.length();
			s=lena-lenb;
			if(lena>lenb) {
				for(int i=1; i<=s; i++) {
					strb=strb+'0';
				}
			}
			int digit=0;
			string Rt;
			B.ChangeData(strb);
			while(1) {
				if(A>=B) {
					A=AMinusB(A,B);
					digit++;
					continue;
				} else {
					Rt=Rt+char(digit+'0');
					digit=0;
					if(s==0)
						break;
					s--;
					string sl=B.ShowString();
					sl.erase(sl.length()-1,1);
					B.ChangeData(sl);
				}
			}
			for(int len=Rt.length(); len>1; len--) {
				if(Rt[0]=='0')
					Rt.erase(0,1);
				else
					break;
			}
			LLLInt res(Rt,flag);
			return res;
		}
		LLLInt Power(LLLInt A,int B)
		{
			LLLInt Rt("1");
			for(int i=1;i<=B;i++)
			{
			Debug(Rt);
				Rt=AMultiplyB(Rt,A);
			}
			return Rt;
		}
};
signed main() {
//	freopen("APlusB.in","r",stdin);
//	freopen("APlusB.out","w",stdout);
	operation T;
	LLLInt a,b;
	a.GetNum();
	b.GetNum();
	LLLInt P=T.AMultiplyB(a,b);
	cout <<P.ShowInt();
}
2021/9/3 21:51
加载中...