1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class LongInt 5 { 6 public: 7 LongInt() //默认构造函数 8 { 9 size=0; 10 } 11 LongInt(string &t) 12 { 13 a=t; 14 size=t.size(); 15 } 16 LongInt(const LongInt &t) 17 { 18 a=t.a; 19 size=t.size; 20 } 21 void Get(string &t) //输入 22 { 23 a=t; 24 size=t.size(); 25 } 26 string operator+(const LongInt &t) 27 {//把长度长的放到字符串temp里,再把另一个加到temp里,最后遍历temp,该进位的进位,该降位的降位 28 if(size>t.size) 29 { 30 string temp(size,‘\0‘); 31 for(int i=0;i<size-t.size;i++) 32 temp[i]=a[i]; 33 for(int i=size-t.size;i<size;i++) 34 temp[i]=a[i]+t.a[i-size+t.size]-‘0‘; 35 for(int i=size-1;i>0;i--) 36 if(temp[i]>‘9‘) 37 { 38 temp[i-1]++; 39 temp[i]-=10; 40 } 41 if(temp[0]>‘9‘) 42 { 43 string _temp(temp+"a"); 44 for(int i=size;i>0;i--) 45 _temp[i]=_temp[i-1]; 46 _temp[1]-=10; 47 _temp[0]=‘1‘; 48 return _temp; 49 } 50 return temp; 51 } 52 else if(size<t.size) 53 { 54 string temp(t.size,‘\0‘); 55 for(int i=0;i<t.size-size;i++) 56 temp[i]=t.a[i]; 57 for(int i=t.size-size;i<t.size;i++) 58 temp[i]=t.a[i]+a[i-t.size+size]-‘0‘; 59 for(int i=t.size-1;i>0;i--) 60 if(temp[i]>‘9‘) 61 { 62 temp[i-1]++; 63 temp[i]-=10; 64 } 65 if(temp[0]>‘9‘) 66 { 67 string _temp(temp+"a"); 68 for(int i=size;i>0;i--) 69 _temp[i]=_temp[i-1]; 70 _temp[1]-=10; 71 _temp[0]=‘1‘; 72 return _temp; 73 } 74 return temp; 75 } 76 else if(size==t.size) 77 { 78 string temp(size,‘\0‘); 79 for(int i=0;i<size;i++) 80 temp[i]=a[i]+t.a[i]-‘0‘; 81 for(int i=size-1;i>0;i--) 82 if(temp[i]>‘9‘) 83 { 84 temp[i-1]++; 85 temp[i]-=10; 86 } 87 if(temp[0]>‘9‘) 88 { 89 string _temp(temp+"a"); 90 for(int i=size;i>0;i--) 91 _temp[i]=_temp[i-1]; 92 _temp[1]-=10; 93 _temp[0]=‘1‘; 94 return _temp; 95 } 96 return temp; 97 } 98 } 99 private: 100 string a; 101 int size; 102 }; 103 int main() 104 { 105 LongInt a,b; 106 string _a,_b; 107 int n; 108 cin>>n; 109 for(int i=1;i<=n;i++) 110 { 111 cin>>_a>>_b; 112 a.Get(_a); 113 b.Get(_b); 114 cout<<"Case "<<i<<‘:‘<<endl<<_a<<‘ ‘<<‘+‘<<‘ ‘<<_b<<‘ ‘<<‘=‘<<‘ ‘<<a+b<<endl; 115 if(i!=n) 116 cout<<endl; 117 } 118 }
原文:http://www.cnblogs.com/Leonsco/p/5031370.html