一个用起来很简单的高精度1.0
2023-10-29 16:59:43
发布于:浙江
写在前面:
1、需要iostream、string、cstring头文件
2、如需要与低精运算,请将其转化为高精后在运算如:
计算高精的1加上低精的1应写成如下形式:
lll a = 1;
a + lll(1);
3、目前只支持+、-、<、>、==、=运算
class lll{
private:
	int *a;
	int len = 0;
	bool f=0;
	lll add(const lll& a,const lll& b)const{
		lll c;
		int len = max(a.len,b.len);
		c.a[0] = 0;
		for(int i = 0;i <= len;i++){
			c.a[i] = a.a[i] + b.a[i] + c.a[i];
			c.a[i+1] += c.a[i] / 10;
			c.a[i] %= 10;
		}
		
		if(c.a[len+1])len++;
		c.len = len;
		c.f = a.f;
		return c;
	}
	lll __reduce(const lll& a,const lll& b)const{
		lll c;
		int len = max(a.len,b.len);
		for(int i = 0;i <= len;i++){
			c[i] = a[i] - b[i];
			if(c[i] < 0){
				c[i+1]--;
				c[i]+=10;
			}
		}
		return c;
	}
	lll reduce(const lll& a,const lll& b)const{
		lll c;
		if(a.gt(b,a)){
			c = a.__reduce(b,a);
			c.f = 1;
			return c;
		}
		c = a.__reduce(a,b);
		return c;
	}
	bool gt(const lll& a,const lll& b)const{ //大于
		if(a.len > b.len)return true;
		if(b.len > a.len || a.eq(a,b))return false;
		for(int i = a.len;i>=0;i--) {
			if(a.a[i] < b.a[i])return false;
		}
		return true;
	}
	bool lt(const lll& a,const lll& b)const{ //小于
		if(a.len < b.len)return true;
		if(b.len < a.len || a.eq(a,b))return false;
		for(int i = a.len;i>=0;i--) {
			if(a.a[i] > b.a[i])return false;
		}
		return true;
	}
	bool eq(const lll& a,const lll& b)const{
		int len = max(a.len,b.len);
		for(int i=0;i<=len;i++){
			if(a[i] != b[i]){
				return false;
			}
		}
		return true;
	}
public:
	lll(){
		this->a = new int [10000];
		memset(this->a,0,sizeof(int[1000]));
	}
	lll(int a){
		this->a = new int [10000];
		memset(this->a,0,sizeof(int[1000]));
		int len = 0;
		if(a < 0){
			a = -a;
			this->f = true;
		}
		for(int y = a;y > 0;y /= 10){
			this->a[len++] = y % 10;
		}
		if(len == 0){
			len++;
		}
		this->len = len - 1;
	}
	lll(const lll& other) {
		len = other.len;
		f = other.f;
		a = new int[len + 1];
		for (int i = 0; i <= len; i++) {
			a[i] = other.a[i];
		}
	}
	
	lll(lll&& other) noexcept {
		len = other.len;
		f = other.f;
		a = other.a;
		other.a = nullptr;
	}
	
	lll& operator=(const lll& other) {
		if (this == &other) {
			return *this;
		}
		delete[] a;
		len = other.len;
		f = other.f;
		a = new int[len + 1];
		for (int i = 0; i <= len; i++) {
			a[i] = other.a[i];
		}
		return *this;
	}
	
	lll& operator=(lll&& other) noexcept {
		if (this == &other) {
			return *this;
		}
		delete[] a;
		len = other.len;
		f = other.f;
		a = other.a;
		other.a = nullptr;
		return *this;
	}
	~lll() {
		if(this->a != NULL && this->a != nullptr){
			delete[] this->a;
			this->a = NULL;
		}
	}
	int& operator[](const int n){
		return this->a[n];
	}
	const int& operator[](const int n) const{
		return this->a[n];
	}
	friend istream& operator >>(istream &input,lll &a){
		string arr;
		input >> arr;
		int len = arr.length() - 1;
		if(arr[0] == '-'){
			len--;
			a.f = true;
		}
		a.len = len;
		for(int i = 0;i <= len;i++){
			a.a[len - i] = arr[i] - '0';
		}
		return input;
	} 
	friend ostream& operator <<(ostream &output,const lll& a){
		if(a.f)output << '-';
		for(int i = a.len;i >= 0;i--){
			output << a.a[i];
		}
		return output;
	}
	friend lll operator +(const lll& a,const lll& b){
		if(a == lll(0)){
			return b;
		}
		if(b == lll(0)){
			return a;
		}
		if((a > lll(0) && b < lll(0)) || (b > lll(0) && a < lll(0))){
			return a.reduce(a,b);
		}
		return a.add(a,b);
	}
	friend lll operator -(const lll& a,const lll& b){
		if(a > lll(0) && b < lll(0)){
			return a.add(a,b);
		}
		return a.reduce(a,b);
	}
	friend bool operator >(const lll& a,const lll& b){
		if(a.f == 0&&b.f == 1){
			return true;
		}else if(a.f == 1&&b.f == 0){
			return false;
		}
		if(a.f == 1&&b.f == 1){
			return !a.gt(a,b);
		}
		return a.gt(a,b);
	}
	friend bool operator <(const lll& a,const lll& b){
		if(a.f == 0&&b.f == 1){
			return false;
		}else if(a.f == 1&&b.f == 0){
			return true;
		}
		if(a.f == 1&&b.f == 1){
			return !a.lt(a,b);
		}
		return a.lt(a,b);
	}
	friend bool operator==(const lll& a,const lll& b){
		if(a.f != b.f){
			return false;
		}
		int len = max(a.len,b.len);
		for(int i=0;i<=len;i++){
			if(a[i] != b[i]){
				return false;
			}
		}
		return true;
	}
	friend bool operator!=(const lll& a,const lll& b){
		if(a.f == b.f){
			return false;
		}
		int len = max(a.len,b.len);
		for(int i=0;i<=len;i++){
			if(a[i] == b[i]){
				return false;
			}
		}
		return true;
	}
};
这里空空如也






有帮助,赞一个