跳到正文

科技·工程

一种高精度的类型

Release_Leaf约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

在做题时,我们又是会遇到高精度烦恼,代码又长又臭,根本不想写高精度。

这次我向大家分享一下一个叫做BigInt的整数高精度自定义数据类型。

创建框架

我们的自定义类型使用class制作的,先写一个框架。

C++
class BigInt{
private:     //私自功能
    vector<int> num;   //数的部分
    bool isnegative=0;      //是否为负
public:     //公开功能
    //初始化类型
    BigInt(){}
    ~BigInt(){}
}

变量赋值

这是一个数据类型,必须有=符赋值功能,我们可以设定用string和long long传入。

C++
//string类型的传入方式
BigInt(string s){
    int f=0;
    if(s[0]=='-'){
        f=1,isnegative=1;
    }
    for(;f<s.size();f++)num.push_back(s[f]-'0');
}
//例如"123"这样传入的类型为char*,所以定义char*类型生成数据
BigInt(char* s){
    int f=0;
    if(s[0]=='-'){
        f=1,isnegative=1;
    }
    for(;'0'<=s[f]&&s[f]<='9';f++)num.push_back(s[f]-'0');
}
//通过long long类型生成数据,改成int之类也没事
BigInt(long long n){
    if(n<0)isnegative=1,n=-n;
    while(n){
        num.push_back(n%10);
        n/=10;
    }
    reverse(num.begin(),num.end());
}

//使用=重载赋值
void operator=(char* s){
  *this=BigInt(s);
}
void operator=(long long n){
  *this=BigInt(n);
}
void operator=(string s){
  *this=BigInt(s);
}

还有一种复制类型的初始化

C++
BigInt(BigInt& old){
    *this=old;
}
  • *this的含义:表示本身,就是自己。

加减乘除

高精度就是有加减乘(除法太难了,两个高精度更本不会)的功能,在写几个重载符(operator)。

加法

C++
BigInt& operator+(BigInt other){
    if(isnegative){
      isnegative=0;
    return other-*this;
  }
    if(other.isnegative){
      other.isnegative=0;
    return *this-other;
  }
  if(size()<other.size())swap(*this,other);
  int k=size()-other.size();
  for(int i=size()-1;i>=0;i--){
    int c=num[i]+other.num[i-k];
    num[i]=c%10;
    if(c>=10){
      if(i-k){
        num[i-1]+=c/10;
      }else {
        num.insert(num.begin(),c/10);
        break;
      }
    }
  }
    return *this;
}
BigInt& operator+(long long other){
    if(other<0){
      other=-other;
    return *this-other;
  }
  //使用BigInt()强转为BigInt类型
  *this=*this+BigInt(other);
    return *this;
}
//使用任意类型+=,基本可以储存数字的我们都定义新赋值。
template<typename T>
void operator+=(T other){
    *this=*this+BigInt(other);
}

减法和乘法不展示了,都差不多。

比较符

众所周知,作为数学肯定可以比较数字,我对BigInt也很讲究(我只是认真写了<和==的重载)。

C++
template<typename T>
bool operator<(T nums){
  BigInt other=BigInt(nums);
  BigInt This=*this;
  if(This.isnegative&&!other.isnegative){
    return 1;
  }
  if(!This.isnegative&&other.isnegative){
    return 0;
  }
  if(This.isnegative&&other.isnegative){
    swap(This,other);
  }
  if(size()!=other.size())return size()<other.size();
  for(int i=size()-1;i>=0;i--){
    if(other.num[i]!=This.num[i])return other.num[i]>This.num[i];
  }
  return 0;
}
template<typename T>
bool operator>(T nums){
  return !(*this==nums||*this<nums);
}
template<typename T>
bool operator==(T nums){
  BigInt other=BigInt(nums);
  return isnegative==other.isnegative&&num==other.num;
}
template<typename T>
bool operator!=(T nums){
  return !(*this==nums);
}
template<typename T>
bool operator<=(T nums){
  return (*this==nums||*this<nums);
}
template<typename T>
bool operator>=(T nums){
  return (*this==nums||*this>nums);
}

输入与输出

我们知道cin和cout分别是用istream和istream定义的,所以我们可以重载输入输出,其中输入输出功能不是BigInt自身的功能,所以需要增加前缀friend以朋友方式使用内置函数。(如下)

C++
//重载输出
friend ostream& operator<<(ostream& os,BigInt &Bint){
    os<<Bint.get_num();
	return os;
}
//重载输入
friend istream& operator>>(istream& os,BigInt &Bint){
    string s;
	os>>s;
	BigInt n=s;
	Bint=n;
	return os;
}
  • ostream:在iostream标准库里的链接到标准输出
  • istream:在iostream标准库里的链接到标准输入

取绝对值

C++
friend BigInt abs(BigInt _X){
    _X.isnegative=0;
    return _X;
}

位运算还没写,二进制高精度太难了

完整代码

原文