首页 > 代码库 > 无线OSS-高精度整数加法

无线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] = ‘\0‘;

    for(count--; count>0 && buf[count]==‘0‘ ; count--);
    buf[count+1] = ‘\0‘;

    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] = ‘\0‘;


    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;
}

  

无线OSS-高精度整数加法