高精度写炸求助
  • 板块学术版
  • 楼主_thiscall
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/10/2 09:25
  • 上次更新2023/11/4 05:11:17
查看原帖
高精度写炸求助
414610
_thiscall楼主2021/10/2 09:25

我用 class 写了的高精度

错误点位于 operator = 处的 memcpy

错误信息:0xC0000005

调试信息

running DP i=2 
start operator = 
OK operator = 
start operator + 
OK operator + 
start operator = 
(然后程序崩溃)

这是代码

class HP {
public:
	int m[maxv+20], len;
	HP() { reset(); }
	void reset() { memset(this, 0, sizeof(HP)); len=1; }
	void set1() { reset(); m[1]=1; }
	void print() {
		trim();
		for (reg int i=len; i>=1; i--) putchar(m[len]^48);
		putchar('\n');
	}
	void trim() { while (len>=2 && m[len]==0) len--; }
	HP& operator = (HP &_b) {
		cerr<<"start operator =\n"; fflush(stderr);
		memcpy(this, &_b, sizeof(HP));
		cerr<<"OK operator =\n"; fflush(stderr);
		return *this;
	}
};
HP& operator + (HP &a, HP &b) {
	HP res;
	cerr<<"start operator +\n"; fflush(stderr);
	const int ed=max(a.len, b.len)+1;
	rep (i, 1, ed) {
		res.m[i]=a.m[i]+b.m[i];
		res.m[i+1]+=res.m[i]/10;
		res.m[i]%=10;
	}
	res.len=ed;
	res.trim();
	cerr<<"OK operator +\n"; fflush(stderr);
	return res;
}
2021/10/2 09:25
加载中...