[C++] 不新不好的高精类
该封装的实用例见 基于字符串的高精度容器 3.0
封装功能(目前)
Public成员
-
string return_string;
-
vector
return_vector; 计算返回的值都通过它们存储调用。
功能函数
-
void clear()
-
string in_change_string(vector
l) vector转string.
-
int in_compare(string x,string y)
按字典序比较string,大于返回1,小于返回-1,等于返回0.
-
void in_output(int length,string retype,bool spec_return)
返回结果到特定接收容器内
运算函数(仅对字符串型输入数据x,y)
输入说明:numeration是进制数,retype是返回格式,有"string"和"vector".
-
void do_additon(string x,string y,int numeration,string retype)
高精度加法 -
void do_subtraction(string x,string y,int numeration,string retype)
高精度减法 -
void do_multiplication(string x,string y,int numeration,string retype)
高精度乘法 -
string do_division_old(string x,string y,int numeration,string retype)
高精度除法。(用的减法模拟,慢的要死,别用。)返回的string是余数
-
void do_division(string x,string y,int numeration,string retype)
还是高精度除法,没改完,还是用的减法模拟,慢的要死,别用。
-
void do_power(string x,int time,int numeration,string retype)
高精度乘方 -
void do_cut_power(string x,int time,int cut,int numeration,string retype)
压位高精度乘方。cut是压的位数。 -
int do_low_QuickPower(long long base,long long tmes,long long mod)
低精快速幂 -
string do_QuickPower(string base,long long tmes)
高精快速幂 -
string do_cut_QuickPower(string base,long long tmes,int cut)
高精压位快速幂
代码
#define N 1000
int d[N*N+1]={};
class stringin_high_precision{
private:
int a[N],b[N],c[2*N+1];
public:
string return_string;
vector<int> return_vector;
void clear(){
return_vector.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');
}
}
if(retype=="vector"){
if(spec_return==true){
return_vector.push_back(-1);
}
bool flag=false;
for(int i=0;i<=length;++i){
if((flag==true)||(c[i]!=0)){
return_vector.push_back(c[i]);
flag=true;
}
}
if(flag==false){
return_vector.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');
}
}
if(retype=="vector"){
bool flag=false;
for(int i=2*lna+1;i>=1;--i){
if((flag==true)||(c[i]!=0)){
return_vector.push_back(c[i]);
flag=true;
}
}
if(flag==false){
return_vector.push_back(0);
}
}
}
string do_division_old(string x,string y,int numeration,string retype){
clear();
stringin_high_precision r;
r.do_subtraction(x,y,numeration,"string");
c[0]+=1;
while(in_compare(y,r.return_string)<=0){
r.do_subtraction(r.return_string,y,numeration,"string");
c[0]+=1;
if(c[0]>=1000000000){
for(int i=0;i<=x.length();++i){
c[i+1]+=c[i]/numeration;
c[i]%=numeration;
}
}
}
for(int i=0;i<=x.length();++i){
c[i+1]+=c[i]/numeration;
c[i]%=numeration;
}
for(int i=x.length()+1;i>=0;--i){
if(c[i]!=0){
if(retype=="string"){
for(int j=i;j>=0;--j){
return_string.push_back(c[j]+'0');
}
}
if(retype=="vector"){
for(int j=i;j>=0;--j){
return_vector.push_back(c[j]);
}
}
break;
}
if(i==0){
if(retype=="string"){
return_string.push_back('0');
}
if(retype=="vector"){
return_vector.push_back(0);
}
}
}
return r.return_string;
}
void do_power(string x,int time,int numeration,string retype){
if(time<=0){
return_string="1";
return_vector.push_back(0);
return;
}
clear();
return_string=x;
for(int i=1;i<=time-2;++i){
do_multiplication(x,return_string,numeration,"string");
}
do_multiplication(x,return_string,numeration,retype);
}
void do_division(string x,string y,int numeration,string retype){
clear();
stringin_high_precision r;
r.do_power("10",x.length()-y.length()-1,numeration,"string");
string l=r.return_string;
r.do_power("10",x.length()-y.length()-1,numeration,"vector");
for(int i=r.return_vector.size()-1;i>=0;--i){
c[i]=r.return_vector[r.return_vector.size()-1-i];
}
r.do_multiplication(l,y,numeration,"string");
r.do_subtraction(x,r.return_string,numeration,"string");
while(in_compare(y,r.return_string)<=0){
r.do_subtraction(r.return_string,y,numeration,"string");
c[0]+=1;
if(c[0]>=1000000000){
for(int i=0;i<=x.length();++i){
c[i+1]+=c[i]/numeration;
c[i]%=numeration;
}
}
}
if(x.length()-y.length()<=1){
c[0]+=1;
}
for(int i=0;i<=x.length();++i){
c[i+1]+=c[i]/numeration;
c[i]%=numeration;
}
for(int i=x.length()+1;i>=0;--i){
if(c[i]!=0){
if(retype=="string"){
for(int j=i;j>=0;--j){
return_string.push_back(c[j]+'0');
}
}
if(retype=="vector"){
for(int j=i;j>=0;--j){
return_vector.push_back(c[j]);
}
}
break;
}
if(i==0){
if(retype=="string"){
return_string.push_back('0');
}
if(retype=="vector"){
return_vector.push_back(0);
}
}
}
}
void do_cut_power(string x,int time,int cut,int numeration,string retype){
if(time<=0){
return_string="1";
return_vector.push_back(0);
return;
}
clear();
return_string=x;
for(int i=1;i<=time-2;++i){
if(return_string.length()>cut){
do_multiplication(x,return_string.substr(return_string.length()-cut,cut),numeration,"string");
}
else{
do_multiplication(x,return_string,numeration,"string");
}
}
do_multiplication(x,return_string,numeration,retype);
}
int do_low_QuickPower(long long base,long long tmes,long long mod){
int ans=1;
while(tmes>0){
if(tmes&1){
ans*=base;
if(mod!=0){
ans%=mod;
}
}
base*=base;
if(mod!=0){
base%=mod;
}
tmes>>=1;
}
return ans;
}
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;
}
};
stringin_high_precision p;
//Write by HaneDaniko
//C++ Code #1 Url as hpclass