码迷,mamicode.com
首页 > 其他好文 > 详细

无线OSS-高精度整数加法

时间:2016-12-29 22:52:53      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:return   cin   std   iostream   out   include   整数   namespace   amp   

#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-高精度整数加法

标签:return   cin   std   iostream   out   include   整数   namespace   amp   

原文地址:http://www.cnblogs.com/hardsoftware/p/6234901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!