• 讨厌的大整数加法


    #include <iostream> 
    #include <cstring> 
    #include <cstdlib> 
    #include <cstdio> 
    using namespace std;
    const int MAX = 110;     //终于过了的....
                                      //     要记得判断字符串长度!~
    class CHugeInt {
        char p[250];
        
        public:
            CHugeInt(char* s){
                strcpy(p,s);
        
            }
            CHugeInt(int s){
                sprintf(p,"%d",s);
            }
            friend ostream& operator<<(ostream & cout, CHugeInt a){
                     cout<<a.p;
                
                return cout;
               }
        
            CHugeInt  operator+(CHugeInt & b){
              CHugeInt a1(p);
              a1 += b.p;
              return a1;    
            }
            
        friend CHugeInt operator+(int N, CHugeInt& a){
             CHugeInt a1(a.p);
              a1 += N;
              return a1;
            
        }
        
        CHugeInt  operator+(int N){
             CHugeInt a1(p);
              a1 += N;
              return a1;
        }
            
            CHugeInt & operator++(){
              (*this) += 1;
              return*this;
                
            }
            
        friend CHugeInt  operator++(CHugeInt& b,int){
              CHugeInt b1(b.p);
              b += 1;
              return b1;
        }
        
        CHugeInt & operator +=(int N){
            
            char PB[250];
            sprintf(PB,"%d",N);
            char pa[250];
            char pb[250];
            
            if(strlen(p)>=strlen(PB)){
                    strcpy(pa,p);
                    strcpy(pb,PB);
            }
            else{
                   strcpy(pb,p);
                    strcpy(pa,PB);
            }
            int  alen=strlen(pa); 
            int  blen=strlen(pb);
    
              int A,B,j=0;
              char result[250];
              int i,m,n;
              for( i=0,m=alen-1,n=blen-1;n>=0;i++,m--,n--){
                  A=pa[m]-'0';
                  B=pb[n]-'0';
                  if(A+B+j>9){
                      result[i]=A+B-10+j+'0';
                      j=1;
                  }
                else{
                    result[i]=A+B+j+'0';
                    j=0;
               }
    
              }
              int r;
              for(r=i;m>=0;r++,m--){
                  A=pa[m]-'0';
                  if(A+j>9){
                      result[r]=A+j-10+'0';
                      j=1;
                  }
                  else{
                      result[r]=A+j+'0';
                      j=0;
                  }
              }
              if(j==1){
                  result[r]='1';
                  r++;
              }
              result[r]='';
              n=strlen(result);
              for(int i=0,j=n-1;i<j;i++,j--){  
            char c=result[i];  
            result[i]=result[j];  
            result[j]=c;  
        }  
              strcpy(p,result);
              return*this;
        
    }
       CHugeInt & operator +=(char* N){
        
            char pb[250];
            char pa[250];
            if(strlen(p)>=strlen(N)){
                    strcpy(pa,p);
                    strcpy(pb,N);
            }
            else{
                   strcpy(pb,p);
                    strcpy(pa,N);
            }
        
            int  alen=strlen(pa); 
            int  blen=strlen(pb);
    
              int A,B,j=0;
              char result[250];
              int i,m,n;
              for( i=0,m=alen-1,n=blen-1;n>=0;i++,m--,n--){
                  A=pa[m]-'0';
                  B=pb[n]-'0';
                  if(A+B+j>9){
                      result[i]=A+B-10+j+'0';
                      j=1;
                  }
                else{
                    result[i]=A+B+j+'0';
                    j=0;
               }
    
              }
              int r;
              for(r=i;m>=0;r++,m--){
                  A=pa[m]-'0';
                  if(A+j>9){
                      result[r]=A+j-10+'0';
                      j=1;
                  }
                  else{
                      result[r]=A+j+'0';
                      j=0;
                  }
              }
              if(j==1){
                  result[r]='1';
                  r++;
              }
              result[r]='';
              n=strlen(result);
              for(int i=0,j=n-1;i<j;i++,j--){  
            char c=result[i];  
            result[i]=result[j];  
            result[j]=c;  
        }  
              strcpy(p,result);
              return*this;
        
    }
        
    };
    int  main() 
    { 
        char s[210];
        int n;
    
        while (cin >> s >> n) {
            CHugeInt a(s);   //构造函数 1 
            CHugeInt b(n);   //  构造函数 2 
    
        cout << a + b << endl;  //重载<< 重载+号 1 
        cout << n + a << endl;  //重载+号 2 
        cout << a + n << endl;  //重载+号 3 需要返回对象 
        b += n;   
        cout  << ++ b << endl;   //重载++b 
            cout << b++ << endl;   //重载b++ 
          cout << b << endl;
        }
        return 0;
    }
    #include <iostream> 
    #include <cstring> 
    #include <cstdlib> 
    #include <cstdio>     //错误原因...偷懒用int 相加...
    using namespace std;
    const int MAX = 110; 
    class CHugeInt {
        char p[250];
        int  p1;
        
        public:
            CHugeInt(){
                p[0]='';
                p1=-1;
            }
            CHugeInt(char* s){
                strcpy(p,s);
                p1=-1;
            }
            CHugeInt(int s){
                p[0]='';
                p1=s;
            }
            friend ostream& operator<<(ostream & cout, CHugeInt a){
                if(a.p1!=-1){
                    cout<<a.p1;
                }
                else{
                     cout<<a.p;
                }
                return cout;
               }
        
            CHugeInt  operator+(CHugeInt & b){
              CHugeInt a1(p);
              a1 += b.p1;
              return a1;    
            }
            
        friend CHugeInt operator+(int N, CHugeInt& a){
             CHugeInt a1(a.p);
              a1 += N;
              return a1;
            
        }
        
        CHugeInt  operator+(int N){
             CHugeInt a1(p);
              a1 += N;
              return a1;
        }
            
            CHugeInt & operator++(){
              (*this) += 1;
              return*this;
                
            }
            
        friend CHugeInt  operator++(CHugeInt& b,int){
              CHugeInt b1(b.p1);
              b.p1 += 1;
              return b1;
        }
        
        CHugeInt & operator +=(int N){
            if(p1!=-1){
                p1 += N;
                return *this;
            }
            else{
            
            char pb[10];
            sprintf(pb,"%d",N);
            char pa[250];
            strcpy(pa,p);
            int  alen=strlen(pa); 
            int  blen=strlen(pb);
    
              int A,B,j=0;
              char result[250];
              int i,m,n;
              for( i=0,m=alen-1,n=blen-1;n>=0;i++,m--,n--){
                  A=pa[m]-'0';
                  B=pb[n]-'0';
                  if(A+B+j>9){
                      result[i]=A+B-10+j+'0';
                      j=1;
                  }
                else{
                    result[i]=A+B+j+'0';
                    j=0;
               }
    
              }
              int r;
              for(r=i;m>=0;r++,m--){
                  A=pa[m]-'0';
                  if(A+j>9){
                      result[r]=A+j-10+'0';
                      j=1;
                  }
                  else{
                      result[r]=A+j+'0';
                      j=0;
                  }
              }
              if(j==1){
                  result[r]='1';
                  r++;
              }
              result[r]='';
              n=strlen(result);
              for(int i=0,j=n-1;i<j;i++,j--){  
            char c=result[i];  
            result[i]=result[j];  
            result[j]=c;  
        }  
              strcpy(p,result);
              return*this;
        }
    }
            
        
    };
    int  main() 
    { 
        char s[210];
        int n;
    
        while (cin >> s >> n) {
            CHugeInt a(s);   //构造函数 1 
            CHugeInt b(n);   //  构造函数 2 
    
        cout << a + b << endl;  //重载<< 重载+号 1 
        cout << n + a << endl;  //重载+号 2 
        cout << a + n << endl;  //重载+号 3 需要返回对象 
        b += n;   
        cout  << ++ b << endl;   //重载++b 
            cout << b++ << endl;   //重载b++ 
          cout << b << endl;
        }
        return 0;
    }
    #include <iostream> 
    #include <cstring> 
    #include <cstdlib> 
    #include <cstdio> 
    using namespace std;
    const int MAX = 110; 
    class CHugeInt {      //将int 相加 全部改成了字符串相加 可是还是不过 TAT   错误原因:字符串长度未预先判断 想当然以为int输入的长度小于字符串
        char p[250];
        
        public:
            CHugeInt(){
                p[0]='';
            
            }
            CHugeInt(char* s){
                strcpy(p,s);
        
            }
            CHugeInt(int s){
                sprintf(p,"%d",s);
            }
            friend ostream& operator<<(ostream & cout, CHugeInt a){
                     cout<<a.p;
                
                return cout;
               }
        
            CHugeInt  operator+(CHugeInt & b){
              CHugeInt a1(p);
              a1 += b.p;
              return a1;    
            }
            
        friend CHugeInt operator+(int N, CHugeInt& a){
             CHugeInt a1(a.p);
              a1 += N;
              return a1;
            
        }
        
        CHugeInt  operator+(int N){
             CHugeInt a1(p);
              a1 += N;
              return a1;
        }
            
            CHugeInt & operator++(){
              (*this) += 1;
              return*this;
                
            }
            
        friend CHugeInt  operator++(CHugeInt& b,int){
              CHugeInt b1(b.p);
              b += 1;
              return b1;
        }
        
        CHugeInt & operator +=(int N){
        
    
            
            char pb[10];
            sprintf(pb,"%d",N);
            char pa[250];
            strcpy(pa,p);
            int  alen=strlen(pa); 
            int  blen=strlen(pb);
    
              int A,B,j=0;
              char result[250];
              int i,m,n;
              for( i=0,m=alen-1,n=blen-1;n>=0;i++,m--,n--){
                  A=pa[m]-'0';
                  B=pb[n]-'0';
                  if(A+B+j>9){
                      result[i]=A+B-10+j+'0';
                      j=1;
                  }
                else{
                    result[i]=A+B+j+'0';
                    j=0;
               }
    
              }
              int r;
              for(r=i;m>=0;r++,m--){
                  A=pa[m]-'0';
                  if(A+j>9){
                      result[r]=A+j-10+'0';
                      j=1;
                  }
                  else{
                      result[r]=A+j+'0';
                      j=0;
                  }
              }
              if(j==1){
                  result[r]='1';
                  r++;
              }
              result[r]='';
              n=strlen(result);
              for(int i=0,j=n-1;i<j;i++,j--){  
            char c=result[i];  
            result[i]=result[j];  
            result[j]=c;  
        }  
              strcpy(p,result);
              return*this;
        
    }
       CHugeInt & operator +=(char* N){
        
    
            
            char pb[250];
            char pa[250];
            if(strlen(p)>=strlen(N)){
                    strcpy(pa,p);
                    strcpy(pb,N);
            }
            else{
                   strcpy(pb,p);
                    strcpy(pa,N);
            }
        
            int  alen=strlen(pa); 
            int  blen=strlen(pb);
    
              int A,B,j=0;
              char result[250];
              int i,m,n;
              for( i=0,m=alen-1,n=blen-1;n>=0;i++,m--,n--){
                  A=pa[m]-'0';
                  B=pb[n]-'0';
                  if(A+B+j>9){
                      result[i]=A+B-10+j+'0';
                      j=1;
                  }
                else{
                    result[i]=A+B+j+'0';
                    j=0;
               }
    
              }
              int r;
              for(r=i;m>=0;r++,m--){
                  A=pa[m]-'0';
                  if(A+j>9){
                      result[r]=A+j-10+'0';
                      j=1;
                  }
                  else{
                      result[r]=A+j+'0';
                      j=0;
                  }
              }
              if(j==1){
                  result[r]='1';
                  r++;
              }
              result[r]='';
              n=strlen(result);
              for(int i=0,j=n-1;i<j;i++,j--){  
            char c=result[i];  
            result[i]=result[j];  
            result[j]=c;  
        }  
              strcpy(p,result);
              return*this;
        
    }
        
    };
    int  main() 
    { 
        char s[210];
        int n;
    
        while (cin >> s >> n) {
            CHugeInt a(s);   //构造函数 1 
            CHugeInt b(n);   //  构造函数 2 
    
        cout << a + b << endl;  //重载<< 重载+号 1 
        cout << n + a << endl;  //重载+号 2 
        cout << a + n << endl;  //重载+号 3 需要返回对象 
        b += n;   
        cout  << ++ b << endl;   //重载++b 
            cout << b++ << endl;   //重载b++ 
          cout << b << endl;
        }
        return 0;
    }
  • 相关阅读:
    【软件工程】Alpha冲刺 (6/6)
    【软件工程】Alpha冲刺 (5/6)
    为什么三层感知器能够解决任意区域组合的分类问题(不同隐层数的感知器的分类能力)
    PCA学习笔记(含推导过程)
    软件工程实践总结
    Beta冲刺(2/5)(麻瓜制造者)
    Beta冲刺(1/5)(麻瓜制造者)
    个人作业——华为软件开发云案例分析
    Alpha- 事后诸葛亮(麻瓜制造者)
    Alpha课堂展示(麻瓜制造者)
  • 原文地址:https://www.cnblogs.com/Latticeeee/p/8601257.html
Copyright © 2020-2023  润新知