[求助]自己写的高精度不知为何在做减法的时候总是会莫名其妙崩溃
  • 板块灌水区
  • 楼主IQ勇士
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/8/2 11:12
  • 上次更新2023/10/27 17:24:19
查看原帖
[求助]自己写的高精度不知为何在做减法的时候总是会莫名其妙崩溃
158652
IQ勇士楼主2022/8/2 11:12

RT,代码如下

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXN = 10000;
struct BigInt{
	int f;
	int data[100001];
	int top;//有几位 
	void set(int x)
	{
		if(x >= 0)
			f = 1;
		else
			f = -1;
		top = 0;
		if(x == 0)
			top = 1;
		while(x)
		{
			data[++top] = x % 10;
			x /= 10;	
		}		
	}
	void csh()
	{
		f = 1;
		memset(data, 0, sizeof(data));
		top = 1;
	}
	void readin()
	{
		csh();
		char c = getchar();
		top = 0;
		while(c < '0' || c > '9')
		{
			if(c == '-')
				f = -1;
			c = getchar();
		}
		while(c >= '0' && c <= '9')
		{
			data[++top] = c - '0';
			c = getchar();
		}
		for(int i = 1; i <= top / 2; i++)
		{
			int t = data[i];
			data[i] = data[top - i + 1];
			data[top - i + 1] = t;
		}
	}
	void flattern(int x)
	{
		for(int i = 1; i <= x; i++)
		{
			data[i + 1] += data[i] / 10;
			data[i] %= 10; 
		}
		top = x;
		while(!data[top])
			top--;
	}
	void print(int x, char c)
	{
		if(f == -1)
			cout << '-';
		for(int i = top; i >= 1; i--)
			cout << data[i];
		if(x)
			cout << c;
	}
	void cut(int x)
	{
		top = x;
		while(!data[top])
			top--;
	}
	void jiajia()
	{
		data[1]++;
		flattern(top + 1);
	}
	BigInt opp()
	{
		BigInt re = *this;
		re.f = 0 - re.f;
		return re;
	}
	BigInt absolute()
	{
		BigInt re = *this;
		re.f = 1;
		return re;
	}
};

int cmp(BigInt a, BigInt b)
{
	if(a.f == 1 && b.f == -1)
		return 1;
	if(a.f == -1 && b.f == 1)
		return -1;
	if(a.top == b.top)
	{
		for(int i = a.top; i >= 1; i--)
		{
			if(a.data[i] == b.data[i])
				continue;
			if(a.data[i] > b.data[i])
				return a.f == 1? 1: -1;
			else
				return a.f == 1? -1: 1;
		}
		return 0;
	}
	if(a.top > b.top)
		return a.f == 1? 1: -1;
	if(a.top < b.top)
		return a.f == 1? -1: 1;
}

BigInt jian(BigInt a, BigInt b)
{
	cout << "!" << endl; 
	BigInt re, aa = a, bb = b;
	re.csh();
	if(!cmp(aa, bb))
	{
		re.set(0);
		return re;
	}
	if(cmp(aa, bb) == -1)
	{
		re = a;
		a = b;
		b = re;
		re.csh();
		re.f = -1;		
	}
	if(aa.f == 1 && bb.f == -1)
	{
		for(int i = 1; i <= max(aa.top, bb.top); i++)
			re.data[i] = aa.data[i] + bb.data[i];
		re.flattern(max(aa.top, bb.top) + 2);
	}
	else
	{
		for(int i = 1; i <= max(aa.top, bb.top); i++)
		{
			if(bb.data[i] > aa.data[i])
			{
				aa.data[i + 1]--;
				aa.data[i] += 10;
			}
		}
		re.flattern(max(aa.top, bb.top) + 1);
	}
	return re;
}

BigInt jia(BigInt a, BigInt b)
{
	BigInt c;
	c.csh();
	for(int i = 1; i <= max(a.top, b.top); i++)
		c.data[i] = a.data[i] + b.data[i];
	c.flattern(max(a.top, b.top) + 2);
	return c;
}

BigInt cheng(BigInt a, BigInt b)
{
	BigInt c;
	c.csh();
	for(int i = 1; i <= a.top; i++)
	{
		for(int j = 1; j <= b.top; j++)
		{
			c.data[i + j - 1] += a.data[i] * b.data[j];
		}
	}
	c.flattern(a.top + b.top + 2);
	return c;
}
int main()
{
	BigInt a, b;
	a.set(0);
	while(1)
	{
		a.readin();
		b.readin();
		cout << cmp(a, b) << endl;
		BigInt c = jian(a, b);
		c.print(1, '\n');
	}
	return 0;
}
2022/8/2 11:12
加载中...