具体功能:
/*
Copyright: HaneDaniko
Date: 15/02/24 12:13
Version 3.0
*/
//I - 高精度容器的输入、输出和赋值
high_precision_vector a,b;
a.input(); //以字符串形式输入
a=1; //使用(long long) int 给 a 赋值
b="1"; //使用字符串给 b 赋值
a=b; //使用另一个高精度容器赋值
b.print(); //以字符串形式输出
//II - 运算符
/*
每个运算符都可以使高精度容器与 (long long) int , 字符串或者其他高精度容器进行运算. 并且其返回值类型为字符串.
*/
// 运算符 +
a=a+b;
b=b+10;
a=a+"10";
b+=a;
//运算符 -
a=a-1;
b-="1";
b-=a;
//运算符 *
b=a*2;
a=b*"2";
a*=b;
//运算符 ^ (乘方) //使用快速幂
a=a^2;
a=a^b;
//III 其他函数
a+=a.it(); //it() 把容器自己作为字符串返回.
b+=b.int_it(); //int_it() 把容器自己作为 (long long) int 返回.
//IV 更新历史
//2.0 更新内容
a=max(a,b); //max函数
b=min(a,2); //min函数
a=max(b,"3");
a=max(a,max(b,"3")); //最值函数嵌套
a=a*2+3; //连续运算(支持运算优先级)
b=max(a^2+b*3-4,"143");
//3.0 更新内容
/*
漏洞修复:
1.添加了初始化值为0
2.添加了数字赋值时清空的指令
*/
cin>>a; //输入输出流重载
cout<<a<<b;
/*
以下的内容不属于高精度容器范畴
*/
/*
高精度容器基于高精度运算类,以下是高精度运算类的其他内容(使用高精度容器时不必单独声明).
*/
stringin_high_precision x;
x.clear();
x.do_additon("1","1",10,"string"); //十进制加法(“10”那里代表进制)
x.do_subtraction("1","1",10,"string"); //十进制减法
x.do_subtraction("1","1",10,"string"); //十进制乘法
x.do_QuickPower("2",2); //十进制乘方
x.do_cut_QuickPower("2",2,100); //十进制带模乘方
vector<int> o;
x.in_change_string(o); //把 vector<int> 转化为字符串
x.in_compare("1","2"); //按字典序比较两个字符串大小
x.in_output(1,1,false); //输出结果
cout<<x.return_string; //在每次运算后以字符串形式存储结果
代码
#define N 100 //最大运算位数,默认是100位,可以改,但最好不超过800. 如果程序崩溃就是你开的不够大或者开太大了.
int d[N*N+1];
class stringin_high_precision{
private:
int a[N],b[N],c[2*N+1];
public:
string return_string;
void clear(){
return_string.clear();
for(int i=0;i<=N-1;++i){
a[i]=0;
b[i]=0;
}
for(int i=0;i<=2*N;++i){
c[i]=0;
}
}
string in_change_string(vector<int> l){
clear();
for(int i=0;i<=l.size()-1;++i){
return_string.push_back(l[i]+'0');
}
return return_string;
}
int in_compare(string x,string y){
if(x.length()>y.length()){
return 1;
}
else if(x.length()<y.length()){
return -1;
}
else{
for(int i=0;i<=x.length();++i){
if(x[i]>y[i]){
return 1;
}
else if(x[i]<y[i]){
return -1;
}
}
return 0;
}
}
void in_output(int length,string retype,bool spec_return){
if(retype=="string"){
if(spec_return==true){
return_string.push_back('-');
}
bool flag=false;
for(int i=0;i<=length;++i){
if((flag==true)||(c[i]!=0)){
return_string.push_back(c[i]+'0');
flag=true;
}
}
if(flag==false){
return_string.push_back('0');
}
}
}
void do_additon(string x,string y,int numeration,string retype){
clear();
if(x.length()<y.length()){
string l=x;
x=y;
y=l;
}
for(int i=0;i<=x.length()-1;++i){
a[i+1]=x[i]-'0';
}
for(int i=0;i<=y.length()-1;++i){
b[i+1]=y[i]-'0';
}
for(int i=y.length();i>=1;--i){
b[i+x.length()-y.length()]=b[i];
}
for(int i=1;i<=x.length()-y.length();++i){
b[i]=0;
}
for(int i=x.length();i>=0;--i){
c[i]+=a[i]+b[i];
if(c[i]!=0){
c[i-1]+=c[i]/numeration;
c[i]%=numeration;
}
}
in_output(x.length(),retype,false);
}
void do_subtraction(string x,string y,int numeration,string retype){
clear();
bool spec_return=false;
if(x.length()<y.length()){
string l=x;
x=y;
y=l;
spec_return=true;
}
else if((x.length()==y.length())&&(x<y)){
string l=x;
x=y;
y=l;
spec_return=true;
}
else if(x==y){
in_output(0,retype,0);
return;
}
for(int i=0;i<=x.length()-1;++i){
a[i+1]=x[i]-'0';
}
for(int i=0;i<=y.length()-1;++i){
b[i+1]=y[i]-'0';
}
for(int i=y.length();i>=1;--i){
b[i+x.length()-y.length()]=b[i];
}
for(int i=1;i<=x.length()-y.length();++i){
b[i]=0;
}
for(int i=x.length();i>=1;--i){
if(a[i]>=b[i]){
c[i]+=a[i]-b[i];
}
else{
c[i-1]-=1;
a[i]+=numeration;
c[i]+=a[i]-b[i];
}
while(c[i]<0){
c[i-1]-=1;
c[i]+=numeration;
}
}
in_output(x.length(),retype,spec_return);
}
void do_multiplication(string x,string y,int numeration,string retype){
clear();
int lna=x.length(),lnb=y.length();
if(lna<lnb){
string f=x;
x=y;
y=f;
lna=x.length();
lnb=y.length();
}
for(int i=0;i<=lna-1;++i){
a[i+1]=x[i]-'0';
}
for(int i=0;i<=lnb-1;++i){
b[i+1]=y[i]-'0';
}
for(int i=lnb;i>=1;--i){
b[i+lna-lnb]=b[i];
}
for(int i=1;i<=lna-lnb;++i){
b[i]=0;
}
for(int i=lna;i>=1;--i){
for(int j=lna;j>=1;--j){
d[lna*lna-(lna*(i-1)+j)+1]=a[j]*b[i];
}
}
for(int i=1;i<=lna+lnb-1;++i){
for(int m=1,k=lna-i;(m<=lnb)&&(k<lna);++m){
if(k>=0){
c[i]+=d[m*lna-k];
}
++k;
}
}
for(int i=1;i<=lna*2;++i){
c[i+1]+=c[i]/numeration;
c[i]%=numeration;
}
if(retype=="string"){
bool flag=false;
for(int i=2*lna+1;i>=1;--i){
if((flag==true)||(c[i]!=0)){
return_string.push_back(c[i]+'0');
flag=true;
}
}
if(flag==false){
return_string.push_back('0');
}
}
}
string do_QuickPower(string base,long long tmes){
stringin_high_precision v;
string ans="1";
while(tmes>0){
if(tmes&1){
v.do_multiplication(ans,base,10,"string");
ans=v.return_string;
}
v.do_multiplication(base,base,10,"string");
base=v.return_string;
tmes>>=1;
}
return ans;
}
string do_cut_QuickPower(string base,long long tmes,int cut){
stringin_high_precision v;
string ans="1";
while(tmes>0){
if(tmes&1){
if(ans.length()>cut){
v.do_multiplication(ans.substr(ans.length()-cut,cut),base,10,"string");
}
else{
v.do_multiplication(ans,base,10,"string");
}
ans=v.return_string;
}
if(base.length()>cut){
v.do_multiplication(base.substr(base.length()-cut,cut),base.substr(base.length()-cut,cut),10,"string");
}
else{
v.do_multiplication(base,base,10,"string");
}
base=v.return_string;
tmes>>=1;
}
return_string=ans;
return ans;
}
};
class high_precision_vector{
private:
string a="0";
public:
void operator =(const string x){
a=x;
}
void operator =(const long long m){
a.clear();
long long x=m;
if(!x){
a="0";
return;
}
while(x){
a.insert(a.begin(),x%10+'0');
x/=10;
}
}
void print(){
cout<<a;
}
void input(){
cin>>a;
}
high_precision_vector operator +(const high_precision_vector &A)const{
stringin_high_precision p;
p.clear();
p.do_additon(a,A.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator +(const long long &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_additon(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator +(const string &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_additon(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator -(const high_precision_vector &A)const{
stringin_high_precision p;
p.clear();
p.do_subtraction(a,A.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator -(const long long &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_subtraction(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator -(const string &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_subtraction(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator *(const high_precision_vector &A)const{
stringin_high_precision p;
p.clear();
p.do_multiplication(a,A.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator *(const long long &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_multiplication(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator *(const string &A)const{
high_precision_vector x;
x=A;
stringin_high_precision p;
p.clear();
p.do_multiplication(a,x.a,10,"string");
high_precision_vector ans;
ans=p.return_string;
return ans;
}
high_precision_vector operator ^(const int x)const{
stringin_high_precision p;
p.clear();
high_precision_vector ans;
ans=p.do_QuickPower(a,x);
return ans;
}
bool operator <(const high_precision_vector &A)const{
if(a.length()!=A.a.length()){
return a.length()<A.a.length();
}
for(int i=0;i<=a.length();++i){
if(a[i]==A.a[i]){
continue;
}
return a[i]<A.a[i];
}
return false;
}
bool operator ==(const high_precision_vector &A)const{
if(a.length()!=A.a.length()){
return false;
}
for(int i=0;i<=a.length();++i){
if(a[i]==A.a[i]){
continue;
}
return false;
}
return true;
}
bool operator >(const high_precision_vector &A)const{
return !((*this<A)||(*this==A));
}
bool operator >=(const high_precision_vector &A)const{
return !(*this<A);
}
bool operator <=(const high_precision_vector &A)const{
return (*this<A)||(*this==A);
}
void operator +=(const high_precision_vector &A){
*this=*this+A;
}
void operator +=(const long long &A){
*this=*this+A;
}
void operator +=(const string &A){
*this=*this+A;
}
void operator -=(const high_precision_vector &A){
*this=*this-A;
}
void operator -=(const long long &A){
*this=*this-A;
}
void operator -=(const string &A){
*this=*this-A;
}
void operator *=(const high_precision_vector &A){
*this=*this*A;
}
void operator *=(const long long &A){
*this=*this*A;
}
void operator *=(const string &A){
*this=*this*A;
}
string it(){
return a;
}
long long int_it(){
long long x=0;
for(int i=0;i<=a.length()-1;++i){
x=x*10+a[i]-'0';
}
return x;
}
friend ostream& operator<<(ostream& output,high_precision_vector& x){
output<<x.a<<endl;
return output;
}
friend istream& operator>>(istream& input,high_precision_vector& x){
input>>x.a;
return input;
}
};
high_precision_vector min(high_precision_vector A,high_precision_vector B){
if(A.it()>B.it()){
return B;
}
return A;
}
high_precision_vector min(high_precision_vector A,string B){
if(A.it()>B){
high_precision_vector ans;
ans=B;
return ans;
}
return A;
}
high_precision_vector min(high_precision_vector A,long long B){
if(A.int_it()>B){
high_precision_vector ans;
ans=B;
return ans;
}
return A;
}
high_precision_vector max(high_precision_vector A,high_precision_vector B){
if(A.it()<B.it()){
return B;
}
return A;
}
high_precision_vector max(high_precision_vector A,string B){
if(A.it()<B){
high_precision_vector ans;
ans=B;
return ans;
}
return A;
}
high_precision_vector max(high_precision_vector A,long long B){
if(A.int_it()<B){
high_precision_vector ans;
ans=B;
return ans;
}
return A;
}
typedef high_precision_vector hpv; //声明的时候打 hpv 就行.
使用示例:高精度加法
hpv a,b;
cin>>a>>b;
a+=b;
cout<<a;