撸了一个半小时代码,没玩守望(垃圾游戏毁我青春五个金牌都带不起队友)最后编译出312个错误。吓我,然而改了几个地方就直接过了直接过了直接过了,目前还是不明白ostream,istream的用处,上网才搞定的。只写了加与乘还有比较。其他后面加吧。减法跟除都很恶心。。。。
代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 5000 6 using namespace std; 7 struct SPH{ 8 int len,s[maxn]; 9 SPH(){ 10 memset(s,0,sizeof(s)); 11 len=1; 12 } 13 SPH operator = (const char* num) 14 { 15 len=strlen(num); 16 for(int i=0;i<len;++i) s[i]=num[len-i-1]-48; 17 return *this; 18 } 19 SPH operator=(const int num){ 20 char a[maxn]; 21 sprintf(a,"%d",num); 22 *this=a; 23 return *this; 24 } 25 SPH (const int num){ *this=num; 26 } 27 SPH (const char * num){ *this=num; 28 } 29 SPH operator+(const SPH & a) 30 { 31 SPH c; 32 c.len=max(len,a.len)+1; 33 int x=0; 34 for(int i=0;i<c.len;++i) 35 { 36 c.s[i]=s[i]+a.s[i]+x; 37 x=c.s[i]/10; 38 c.s[i]=c.s[i]%10; 39 } 40 if(c.s[c.len-1]==0) --c.len; 41 return c; 42 } 43 SPH operator+=(const SPH & a){ 44 *this=*this+a; 45 return *this; 46 } 47 SPH operator*(const SPH & x){ 48 SPH c; 49 c.len=len+x.len; 50 for(int i=0;i<len;++i){ 51 for(int j=0;j<x.len;++j){ 52 c.s[i+j]+=s[i]*x.s[j]; 53 c.s[i+j+1]+=c.s[i+j]/10; 54 c.s[i+j]%=10; 55 } 56 57 } 58 if(c.len-1) --c.len; 59 return c; } 60 SPH operator *=(const SPH & a){ 61 *this=*this * a; 62 return *this; 63 } 64 bool operator < (const SPH & x) const{ 65 if(len !=x.len) return len<x.len; 66 for(int i=len-1;i>=0;i--){ 67 if(s[i]!=x.s[i]) return s[i]<x.s[i]; 68 } 69 return false;} 70 bool operator >(const SPH & x) const {return x<*this;} 71 bool operator <=(const SPH & x) const {return !(x<*this);} 72 bool operator >=(const SPH & x) const {return !(x>*this);} 73 bool operator ==(const SPH & x) const {return !(x<*this||x>*this);} 74 bool operator !=(const SPH & x) const {return x<*this||x>*this;} 75 }; 76 ostream& operator << (ostream &out,const SPH& x){ 77 for(int i=x.len-1;i>=0;i--) 78 cout<<x.s[i]; 79 return out; 80 } 81 istream& operator >> (istream &in,SPH& x) 82 { 83 char num[maxn]; 84 in>>num; 85 x=num; 86 return in; 87 } 88 89 int main() 90 { 91 SPH n,m,sum; 92 cin>>n>>m; 93 sum=n+m; 94 cout<<sum<<" "; 95 sum=n*m; 96 cout<<sum; 97 return 0; 98 }