018:别叫,这个大整数已经很简化了!

  1 #include <iostream> 
  2 #include <cstring> 
  3 #include <cstdlib> 
  4 #include <cstdio> 
  5 using namespace std;
  6 const int MAX = 110; 
  7 class CHugeInt {
  8 private:
  9     char hugenum[210];
 10 public:
 11     void reverse(char* s){
 12         int i = 0,j = strlen(s) - 1;
 13         while(i < j){
 14             swap(s[i],s[j]);
 15             ++ i;
 16             -- j;
 17         }
 18     }
 19     
 20     CHugeInt(const char* s){
 21         memset(hugenum,'\0',sizeof(hugenum));//记得重置!!! 
 22         strcpy(hugenum,s);
 23         reverse(hugenum);
 24     }
 25     
 26     CHugeInt(int n){
 27         memset(hugenum,'\0',sizeof(hugenum));
 28         sprintf(hugenum,"%d",n);
 29         reverse(hugenum);
 30     }
 31     
 32     CHugeInt operator+(const CHugeInt& a){
 33         CHugeInt tmp(0);
 34         int cnt = 0;
 35         for(int i = 0;i < 210;++ i){
 36             char c1 = hugenum[i];
 37             char c2 = a.hugenum[i];
 38             if(c1 == 0 && c2 == 0 && cnt == 0)
 39               break;
 40             if(c1 == 0)
 41               c1 = '0';
 42             if(c2 == 0)
 43               c2 = '0';
 44             int k = c1 - '0' + c2 - '0' + cnt;
 45             if(k >= 10){
 46                 k = k % 10;
 47                 tmp.hugenum[i] = k + '0';
 48                 cnt = 1;
 49             }
 50             else{
 51                 tmp.hugenum[i] = k + '0';
 52                 cnt = 0;
 53             }
 54         }
 55         return tmp;
 56     }
 57     
 58     friend CHugeInt operator+(int n,const CHugeInt& a){
 59         CHugeInt tmp = CHugeInt(n) + a;
 60         return tmp;
 61     }
 62     
 63     CHugeInt operator+(int n){
 64         CHugeInt tmp = *this + CHugeInt(n);
 65         return tmp;
 66     }
 67     
 68     CHugeInt operator+=(int n){
 69         //*this = *this + CHugeInt(n);
 70         *this = *this + n;
 71         return *this;
 72     }
 73     
 74     CHugeInt operator++(){//前置++
 75         /*int n = 1;
 76         *this = *this + CHugeInt(n);*/
 77         *this = *this + 1;
 78         return *this;
 79     }
 80     
 81     CHugeInt operator++(int){
 82         CHugeInt tmp = CHugeInt(*this);//默认复制函数,会把所有参数都复制过去
 83         /*int n = 1;
 84         *this = *this + CHugeInt(n);*/
 85         *this = *this + 1;
 86         return  tmp;
 87     }
 88     
 89     friend ostream& operator<<(ostream& o,const CHugeInt& a){
 90         int len = strlen(a.hugenum);
 91         for(int i = len - 1;i >= 0;-- i)
 92           o << a.hugenum[i];
 93         return o;
 94     }
 95     
 96 };
 97 int  main() 
 98 { 
 99     char s[210];
100     int n;
101 
102     while (cin >> s >> n) {
103         CHugeInt a(s);
104         CHugeInt b(n);
105 
106         cout << a + b << endl;
107         cout << n + a << endl;
108         cout << a + n << endl;
109         b += n;
110         cout  << ++ b << endl;
111         cout << b++ << endl;
112         cout << b << endl;
113     }
114     return 0;
115 }

posted @ 2022-09-13 22:43  balabalahhh  阅读(45)  评论(0编辑  收藏  举报