写到激情处,发现没有定义小数点,很无语。
#include<cstdio>
#include<cstring>
#include<cstdlib>
char* swapdir(char* str){
if(str[0]=='-')return str+1;
if(str[0]=='+'){str[0]='-';return str;}
char* cstr=(char*)malloc((strlen(str)+2)*sizeof(char));
cstr[0]='-';cstr[1]='\0';
strcat(cstr,str);
return cstr;
}
char* format(char* text){
bool sign=(text[0]=='-');
int si=0;
while(text[sign?1:0+si]=='0')text++;
if(sign)text[0]='-';
return text;
}
int cmps(char* left,char* right,int progress=0){
while(left[0]==0)left++;
while(right[0]==0)right++;
int len=strlen(left)-strlen(right);
if(len>0)return 1;
if(len<0)return -1;
if((signed)progress==strlen(left))return 0;
len=left[progress]-right[progress];
if(len>0)return 1;
if(len<0)return -1;
return cmps(left,right,progress+1);
}
char* strplus(char* cLeft,char* cRight,int i=0,int jump=0,char* result=NULL){
int signL=cLeft[0]=='-'?1:0;
int signR=cRight[0]=='-'?1:0;
int Llen=strlen(cLeft);
int Rlen=strlen(cRight);
if(Llen-signL<Rlen-signR){return strplus(cRight,cLeft);}
if(result==NULL)result=(char*)malloc((strlen(cLeft)+1)*sizeof(char));
if(signL+signR==1){
if(signL)cLeft=swapdir(cLeft);
if(signR)cRight=swapdir(cRight);
if((cmps(cLeft,cRight)<0&&signR)||(cmps(cLeft,cRight)>0&&signL))
return swapdir(strplus(signL?cLeft:swapdir(cLeft),signR?cRight:swapdir(cRight)));
if(signL)cLeft=swapdir(cLeft);
if(signR)cRight=swapdir(cRight);
}
if(signL&&signR){
cLeft=swapdir(cLeft);
cRight=swapdir(cRight);
return swapdir(strplus(cLeft,cRight));
}
if(i==Llen-signL){result[1]=jump>0?jump+'0':-jump+'0';result[0]=jump>=0?'0':'-';result[Llen+2-signL]='\0';return result;}
int sum=(signL?-1:1)*(cLeft[Llen-1-i]-'0')+(Rlen-signR-i>0?(signR?-1:1)*(cRight[Rlen-1-i]-'0'):0)+jump;
jump=0;
while(sum<0){jump--;sum+=10;}
while(sum>9){jump++;sum-=10;}
result[Llen-signL-i+1]=sum+'0';
return strplus(cLeft,cRight,i+1,jump,result);
}
char* MultSimple(char* cal,char a,int deta){
int i,sum,jump=0;
unsigned int iAmount=strlen(cal);
int mul=a-'0';
char* result;
result=(char*)malloc((iAmount+deta+2)*sizeof(char));
for(i=0;i!=iAmount;i++){
sum=(cal[iAmount-i-1]-'0')*mul+jump;
jump=0;
while(sum>9){
sum-=10;
jump++;
}
result[iAmount-i]=sum+'0';
}
result[0]=jump+'0';
for(i=iAmount+1;i!=iAmount+deta+1;i++)result[i]='0';
result[iAmount+deta+1]='\0';
return format(result);
}
char* Mult(char* left,char* right){
int Llen=strlen(left);
int Rlen=strlen(right);
int signL=left[0]=='-'?1:0;
int signR=right[0]=='-'?1:0;
int i=0;
if(Llen-signL<Rlen-signR)return Mult(right,left);
if(signL+signR==1)return swapdir(Mult(signL?swapdir(left):left,signR?swapdir(right):right));
if(signL+signR==2)return Mult(swapdir(left),swapdir(right));
char* result=(char*)malloc((strlen(left)+strlen(right)+2)*sizeof(char));
result=MultSimple(left,right[Rlen-1],0);
for(i=1;i!=Rlen;i++){
result=format(strplus(MultSimple(left,right[Rlen-i-1],i),result));
}
result[Llen+Rlen+2]='\0';
return result;
}
char* Divi(char* left,char* right,int faction){//无法实现,其他运算没有定义小数点 2013-11-16
return NULL;
}
struct strNumber{
char* Number;
friend const char* operator+(const strNumber&left,const strNumber&right){return format(strplus(left.Number,right.Number));}
friend const char* operator-(const strNumber&left,const strNumber&right){return format(strplus(left.Number,swapdir(right.Number)));}
friend const char* operator*(const strNumber&left,const strNumber&right){return format(Mult(left.Number,right.Number));}
friend const bool operator>(const strNumber&left,const strNumber&right){return cmps(left.Number,right.Number)>0;}
friend const bool operator<(const strNumber&left,const strNumber&right){return cmps(left.Number,right.Number)<0;}
friend const bool operator==(const strNumber&left,const strNumber&right){return cmps(left.Number,right.Number)==0;}
friend const bool operator>=(const strNumber&left,const strNumber&right){return cmps(left.Number,right.Number)>=0;}
strNumber(void){}
strNumber(char* uN){Number=uN;}
};
int main(){
strNumber str1,str2;
str1="1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001";
str2="9111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
printf("%s\n",str1*str2);
}