zoukankan      html  css  js  c++  java
  • 无线OSS-高精度整数加法

    #include<iostream>
    #include<string>
    using namespace std;
    
    int compareStr(string str1, string str2)
    {
        int num1 = str1.length();
        int num2 = str2.length();
        if(num1>num2) return 1;
        if(num1<num2) return -1;
        if(num1==num2)
        {
            for(int i=0; i<num1; i++ )
            {
                if(str1[i]>str2[i])
                {
                    return 1;
                }
                else if(str1[i]<str2[i])
                {
                    return -1;
                }
                else
                {
    
                }
            }
            return 0;
        }
    }
    
    string rever(string str)
    {
        int num = str.length();
        string tmp = str;
        for(int i=0; i<num; i++)
        {
            tmp[i] = str[num-1-i];
        }
        return tmp;
    }
    
    string mns(string str1, string str2)
    {
        int num1 = str1.length();
        int num2 = str2.length();
    
        string tmp1 = rever(str1);
        string tmp2 = rever(str2);
    
        char buf[100];
        int count = 0;
    
        int carry = 0;
        for(int i=0; i<num1; i++)
        {
            if(i<num2)
            {
                if(tmp1[i]-'0'-carry >= tmp2[i]-'0')
                {
                    char ch = tmp1[i]-'0'-carry - (tmp2[i]-'0') + '0';
                    buf[count++] = ch;
                    carry = 0;
                }
                else
                {
                    char ch = tmp1[i]-'0'-carry - (tmp2[i]-'0') + 10 + '0';
                    buf[count++] = ch;
                    carry = 1;
                }
            }
            else
            {
                if(tmp1[i]-'0'-carry >= 0)
                {
                    char ch = tmp1[i]-'0'-carry;
                    buf[count++] = ch;
                    carry = 0;
                }
                else
                {
                    char ch = '9';
                    buf[count++] = ch;
                    carry = 1;
                }
            }
        }
    
        buf[count] = '';
    
        for(count--; count>0 && buf[count]=='0' ; count--);
        buf[count+1] = '';
    
        return rever(buf);
    }
    
    string add(string str1, string str2)
    {
        int num1 = str1.length();
        int num2 = str2.length();
    
        int maxnum = num1 > num2 ? num1 : num2;
        int minnum = num1 > num2 ? num2 : num1;
    
        string tmp1 = rever(str1);
        string tmp2 = rever(str2);
    
        char buf[100];
        int count = 0;
    
        int carry = 0;
        for(int i=0; i<maxnum; i++)
        {
            if(i<minnum)
            {
                if((tmp1[i]+tmp2[i]-'0'-'0' + carry)>=10)
                {
                    char ch = (tmp1[i]+tmp2[i]-'0'-'0'-10) + '0' + carry;
    
                    //res = res + string(&ch);
                    buf[count++] = ch;
                    carry = 1;
                }
                else
                {
                    char ch = (tmp1[i]+tmp2[i]-'0'-'0') + '0' + carry;
                    //res = res + string(&ch);
                    buf[count++] = ch;
                    carry = 0;
                }
            }
            else
            {
                if(num1 > num2)
                {
                    if( (carry + tmp1[i] - '0') >= 10 )
                    {
                        char ch = (tmp1[i]-'0'-10) + carry + '0';
                    //res = res + string(&ch);
                    buf[count++] = ch;
                        carry = 1;
                    }
                    else
                    {
                        char ch = (tmp1[i]-'0') + carry + '0';
                    //res = res + string(&ch);
                    buf[count++] = ch;
                        carry = 0;
                    }
                }
                else
                {
                    if( (carry + tmp2[i] - '0') >= 10 )
                    {
                        char ch = (tmp2[i]-'0'-10) + carry + '0';
                    //res = res + string(&ch);
                    buf[count++] = ch;
                        carry = 1;
                    }
                    else
                    {
                        char ch =  (tmp2[i]-'0') + carry + '0';
                    //res = res + string(&ch);
                    buf[count++] = ch;
                        carry = 0;
                    }
                }
            }
        }
    
        if(carry==1)
        {
            char ch = '1';
            //res = res + string(&ch);
            buf[count++] = ch;
        }
    
        buf[count] = '';
    
    
        return rever(buf);;
    }
    
    void solve(string str1, string str2)
    {
        if(str1[0]=='-' && str2[0]=='-')
        {
            string tmp1;
            string tmp2;
            tmp1 = str1.substr(1);
            tmp2 = str2.substr(1);
            cout<<'-'<<add(tmp1,tmp2);
        }
        else if(str1[0]=='-' && str2[0]!='-')
        {
            string tmp1;
            string tmp2;
            tmp1 = str1.substr(1);
            tmp2 = str2;
            if(compareStr(tmp1,tmp2)>0)
            {
                cout<<'-'<<mns(tmp1,tmp2);
            }
            else if(compareStr(tmp1,tmp2)==0)
            {
                cout<<'0';
            }
            else
            {
                cout<<mns(tmp2,tmp1);
            }
        }
        else if(str1[0]!='-' && str2[0]=='-')
        {
            string tmp1;
            string tmp2;
            tmp1 = str1;
            tmp2 = str2.substr(1);
            if(compareStr(tmp1,tmp2)>0)
            {
                cout<<mns(tmp1,tmp2);
            }
            else if(compareStr(tmp1,tmp2)==0)
            {
                cout<<'0';
            }
            else
            {
                cout<<'-'<<mns(tmp2,tmp1);
            }
        }
        else
        {
            string tmp1 = str1;
            string tmp2 = str2;
            cout<<add(tmp1,tmp2);
        }
    }
    
    int main()
    {
        string str1, str2;
        cin>>str1>>str2;
    
        solve(str1, str2);
    
        return 0;
    }
    

      

  • 相关阅读:
    第8.13节 Python类中内置方法__repr__详解
    Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
    Python中使用eval执行下面函数的结果怎么是字符串'10020'?
    第8.12节 Python类中使用__dict__定义实例变量和方法
    ThinkPHP---thinkphp拓展之空操作
    ThinkPHP---TP功能类之邮件
    ThinkPHP---案例--实现知识管理功能
    ThinkPHP---TP功能类之公文管理功能2----------继续完善
    ThinkPHP---TP拓展之获取IP信息
    ThinkPHP---layer插件
  • 原文地址:https://www.cnblogs.com/hardsoftware/p/6234901.html
Copyright © 2011-2022 走看看