#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
#define maxlen 10000
class HP
{
public:
friend ostream& operator<<(ostream &cout,const HP& x);
public:
HP() {(*this)=0;}
HP(int inte) {(*this)=inte;}
HP(const char* str) {(*this)=str;}
HP operator=(int inte);
HP operator=(const char* str);
HP operator+(const HP&b);
HP operator-(const HP&b);
HP operator*(const HP&b);
HP operator/(const HP&b);
HP operator%(const HP&b);
int compare(const HP&b);
int len;
int s[maxlen];
};
ostream& operator<<(ostream& cout,const HP&x){
for(int i=x.len;i>=1;i--)
cout<<x.s[i];
return cout;
}
HP HP::operator=(int inte){
if(inte==0){
len=1;
s[len]=0;
return (*this);
}
else{
for(len=0;inte>0;){
s[++len]=inte%10;
inte/=10;
}
return *this;
}
}
HP HP::operator=(const char* str){
int len=strlen(str);
for(int i=1;i<len;i++){
s[i]=str[len-i]-'0';
}
return (*this);
}
HP HP::operator*(const HP&x){
HP temp;
temp.len=len+x.len;
int i,j;
for(i=1;i<=temp.len;i++)
temp.s[i]=0;
for(i=1;i<=x.len;i++)
for(j=1;j<=len;j++){
temp.s[i+j-1]+=x.s[i]*s[j];
}
for(i=1;i<x.len;i++){
temp.s[i+1]+=temp.s[i]/10;
temp.s[i]%=10;
}
while(i>1&&!temp.s[i]){
i--;
}
temp.len=i;
return temp;
}//19.3.10
HP HP::operator+(const HP&x){
HP temp(0);
int i=1;
for(i=1;i<=x.len;i++)
temp.s[i] += x.s[i];
for(i=1;i<=len;i++)
temp.s[i] += s[i];
for(i=1;i<=len-1||i<=x.len-1;i++){
temp.s[i+1]+=temp.s[i]%10;
temp.s[i]/=10;
len=i+1;
}
if(temp.s[i]>=10){
len++;
temp.s[i+1]=1;
temp.s[i]/=10;
}
return temp;
}
//19.3.15
HP HP::operator-(const HP&x){
int i,j=0;
HP temp(0);
for(i=1;i<=len;i++){
temp.s[i]=s[i]-j;
if(i<=x.len)
temp.s[i]-=x.s[i];
if(temp.s[i]<0){
temp.s[i]+=10;
j=1
}
else
j=0;
}
temp.len=len;
while(temp.len>1&&!temp.s[temp.len]){
temp.len--;
}
return temp;
}
HP HP::operator/(const HP&x){
int i,j;
HP temp(0),c(0);
for(i=len;i>0;i--){
if(!(temp.len==1&&temp.s[1]==0)){
for(j=temp.len;j>0;j--){
temp.s[j+1]=temp.s[j];
}
temp.len++;
}
temp.s[1]=s[i];
c.s[i]=0;
while((j=temp.compare(x))>=0){
c.s[i]++;
temp=temp-x;
if(j==0)
break;
}
}
c.len=len;
while((c.len>1)&&!(c.s[c.len])){
c.len--;
}
return c;
}
HP HP::operator%(const HP&x){
HP c(0),b(0);
c=(*this)/x;
b=(*this)-c*x;
return b;
}
int HP::compare(const HP&x){
if(len>x.len)
return 1;
else if(len<x.len)
return -1;
else{
for(int i=len;i>=1;i++){
if(s[i]==x.s[i])
continue;
else
return s[i]>x.s[i]? 1:-1;
}
return 0;
}
}//仅支持正数,负数需加入符号位
c++ My_task
最新推荐文章于 2024-04-18 18:51:48 发布