跳到正文

个人记录

高精度封装

LINYUHENG2约 3 分钟
原文

这是 2026年3月4日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年3月4日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
#include<bits/stdc++.h>
using namespace std;
const int N=10005;
struct Bigint{
	int num[N],len;
	void input(){
		string s;
		cin>>s;
		len=s.size();
		for(int i=0,j=len;i<len;i++,j--){
			num[j]=s[i]-'0';
		}
	}
	void print(){
		if(len==0) cout<<0;
		for(int i=len;i>=1;i--){
			cout<<num[i];
		}
	}
	int &operator[](const int i){
		return num[i];
	}
};

int com(Bigint a,Bigint b){
	if(a.len!=b.len) return a.len>b.len?1:-1;
	int len=a.len;
	for(int i=len;i>=1;i--){
		if(a[i]!=b[i]) return a[i]>b[i]?1:-1;
	}
	return 0;
}
bool operator<(Bigint a,Bigint b){return com(a,b)==-1;}
bool operator>(Bigint a,Bigint b){return com(a,b)==1;}
bool operator==(Bigint a,Bigint b){return com(a,b)==0;}
bool operator<=(Bigint a,Bigint b){return a<b||a==b;}
bool operator>=(Bigint a,Bigint b){return a>b||a==b;}

Bigint operator+(Bigint a,Bigint b){
	Bigint c;
	c.len=max(a.len,b.len);
	for(int i=1;i<=c.len;i++){
		c[i]+=a[i]+b[i];
		c[i+1]+=c[i]/10;
		c[i]%=10;
	}
	if(c[c.len+1]) c.len++;
	return c;
}
Bigint operator-(Bigint a,Bigint b){
	Bigint c;
	c.len=max(a.len,b.len);
	for(int i=1;i<=c.len;i++){
		c[i]+=a[i]-b[i];
		if(c[i]<0){
			c[i]+=10;
			c[i+1]--;
		}
	}
	while(c.len>0&&!c[c.len]) c.len--;
	return c;
}

int main(){
	Bigint a,b;
	a.input();
	b.input();
	if(a>b) cout<<">";
	else if(a<b) cout<<"<";
	else if(a==b) cout<<"=";
	return 0;
}