美文网首页
大整数减法——C++实现

大整数减法——C++实现

作者: wolfaherd | 来源:发表于2022-10-10 17:05 被阅读0次
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    //将s1置为较大数
    bool exchangeS(string &s1,string &s2,int &size1,int &size2){
        bool isExchange = false;
        if(size1 < size2){
            isExchange = true;
        }else if(size1 == size2){
            for(int i = 0;i != size1;i++){
                if(s1[i] < s2[i]){
                    isExchange = true;
                    break;  
                }
            }
        }
    
        if(isExchange){
            swap(size1,size2);
            swap(s1,s2);
        }
        return isExchange;
    }
    
    //大整数加法
    string bigIntAdd(string &s1,string &s2){
        int size1 = s1.size();
        int size2 = s2.size();
        
        bool isExchange = exchangeS(s1,s2,size1,size2);
        
        bool isCarry = false;
        for(int i = 0;i != size1;i++){
            int addRes = s1[size1 - i - 1] - '0';
            if(i < size2){
                addRes += s2[size2 - i - 1] - '0';
            }else if(!isCarry){
                break;  
            }
            if(isCarry)
                addRes++;
            isCarry = addRes >= 10;
            addRes %= 10;
            s1[size1 - i - 1] = '0' + addRes;
        }
        
        if(isCarry){
            s1 = "1" + s1;      
        }
                   
        return s1;             
    }
    
    //大整数减法
    string bigIntSub(string s1,string s2){
        //s1为正,s2为负
        //s1为负,s2为正
        //采用大整数加法。
        bool isNegative1 = s1[0] == '-';
        bool isNegative2 = s2[0] == '-';
        if(isNegative1 && !isNegative2){
            s1.erase(s1.begin());
            bigIntAdd(s1,s2);
            return "-" + s1;
        }else if(isNegative2 && !isNegative1){
            s2.erase(s2.begin());
            bigIntAdd(s1,s2);
            return s1;
        }
        
        if(isNegative1 && isNegative2){
            s1.erase(s1.begin());
            s2.erase(s2.begin());
        }
        
        int size1 = s1.size();
        int size2 = s2.size();
        
        bool isExchange = exchangeS(s1,s2,size1,size2);
        
        bool isBorrow = false;
        for(int i = 0;i != size1;i++){
            char ch1 = s1[size1 - i - 1];
            
            if(i < size2){
                char ch2 = s2[size2 - i - 1];
                isBorrow = ch1 < ch2;
                int v = ch1 - ch2;
                if(isBorrow)
                    v += 10;
                s1[size1 - i - 1] = '0' + v;            
            }else if(!isBorrow)
                break;
            else{
                if(isBorrow && s1[size1 - i - 1] > '0'){
                    s1[size1 - i - 1] -= 1;
                    if((size1 - i - 1) == 0 && s1[size1 - i - 1] == '0'){
                        s1.erase(s1.begin());   
                    }
                    break;
                }else{
                    s1[size1 - i - 1] = '9';
                }
            }
        }
        if(isNegative1 && isNegative2)
            isExchange = !isExchange;
        if(isExchange)
            return "-" + s1;
        return s1;
    }
    
    int main()
    {
        cout << bigIntSub("1111","222222") << endl;
        cout << bigIntSub("-1111","222222") << endl;
        cout << bigIntSub("-1111","-222222") << endl;
        cout << bigIntSub("1111","-222222") << endl;
        
       cout << "Hello World";
       return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:大整数减法——C++实现

          本文链接:https://www.haomeiwen.com/subject/wxvoartx.html