Taisho decimal additive (hdu1753)

F - A + B Ming

Saying that after a long month, Xiao Ming has grown a lot, so he changed a name called "Ming."
At this time he is not only do that within 100 addition that "Bob", and now he'll even positive addition to any fractional length.

Now, you give two positive decimal A and B, your task is to calculate the value representative of the Ming of A + B.

Input this topic contains multiple sets of test data, the processing to the end of the file.
Each set of test data is included in the length of a line which is not greater than two decimal positive and B. A 400 Please Output A + B output values in a row inside, output a simplest form. See detailed requirements Sample Output.
Sample Input
1.1 2.9
1.1111111111 2.34443233434
1 1.1
21.3423 2.13423
23432.32 1232
222 232
19.2 12.1
19.2 12.9
2013.2013 3029.3029
99.9 99.9
0.99 99.9
99.9 0.1
99.99 009.11
00098.8 3.32
1.2 1.9
0.1 0.9
1.0 0.10
1.0001 0010.00100
Sample Output
  • 4
  • 3.45554344544
  • 2.1
  • 23.47653
  • 24664.32
  • 454
  • 31.3
  • 32.1
  • 5042.5042
  • 199.8
  • 100.89
  • 100
  • 109.1
  • 102.12
  • 3.1
  • 1
  • 1.1
  • 11.0011
     
     
    #include<iostream>
    #include <string.h>
    #include <algorithm>
    #include <string>
    using namespace std;
    bool flag = false; // 标记小数加法是否需要向整数部分进位
    string cnt , ans ,  sum ;//cnt存整数部分 , ans存小数部分,sum为整体
    int index , indey ;//分别标记前置零和后置零的下标 
    
    string minplu(string a , string b)//小数部分加法
    {
        string f ;
        char e ;
        int k = 0  ;
        for(int i = b.length() - 1 ; i >= 0 ; i--)//a为小数长度更长,用更长的小数部分进行操作
        {
            e = a[i] ;//防止进位计算覆盖
            a[i] = ((a[i] - '0') + (b[i] - '0') + k) % 10 + '0' ;
            k = ((e - '0') + (b[i] - '0') + k) / 10 ;
        }
        if(k) //判断是否需要向整数部分进位
            flag = true ;
        return a ;
    
    }
    string plu(string a , string b)//大数加法
    {
        string f ;
        int i = i = a.length() - 1, j , k = 0 , x , y ;
        if(flag)
        {
            a[i] ++ ; //将小数的进位加上
        }
        for( j = b.length() - 1 ; i >= 0 && j >= 0 ; i-- , j--)
        {
            x = a[i] - '0' ;
            y = b[j] - '0' ;
            f += (x + y + k ) % 10 + '0' ;
            k = (x + y + k ) / 10 ;
        }
        while(i >= 0)
        {
            x = a[i] - '0' ;
            f += (x + k) % 10 + '0' ;
            k = (x + k) / 10;
            i-- ;
        }
        while(j >= 0)
        {
            y = b[j] - '0';
            f += (y + k) % 10 + '0' ;
            k = (y + k) / 10 ;
            j -- ;
        }
        if(k)
            f += k + '0' ;
        reverse(f.begin() , f.end());
        return f ;
    }
    
    string cntclear(string a)//去整数部分前置零
    {
        index = a.find_first_not_of('0');
        if(index != a.npos)
            a.erase(0,index);
        else
            a = '0';
        return a ;
    }
    string ansclear(string a)//去小数部分后置零
    {
        reverse(a.begin() , a.end());
        indey = a.find_first_not_of('0');
        if(indey != a.npos)
            a.erase(0,indey);
        else
            a = "" ;
        reverse(a.begin() , a.end());
        return a ;
    }
    
    int main()
    {
        string a , b ;
        while(cin >> a >> b)
        {
            cnt = ans =  sum = "";
            flag = false ;
            int positiona = a.find('.') , positionb = b.find('.');
            if(positiona != a.npos && positionb != b.npos) // 如果a,b都为小数。。
            {
                string c(a , positiona+1) , d(b , positionb+1) ;//将小数和整数分离
                string e(a ,0 , positiona) , f(b ,0 , positionb) ;
                if(c.length() > d.length())//小数相加
                    ans += minplu(c , d);
                else
                    ans += minplu(d , c);
                cnt += plu(e , f);//整数部分相加
    
                cnt = cntclear(cnt);//整数部分去前置零
                ans = ansclear(ans);//小数部分取后置零
    
                if(ans == "")//判断小数部分是否为‘0’
                cout << cnt << endl ;
                else
                    cout << cnt + '.' + ans  << endl ;
    
            }
            else if(positiona != a.npos)//如果a为小数,b为整数
            {
                string c(a , positiona+1) ;//将小数分离
                string e(a ,0 , positiona) ;
                ans += c ;
                cnt +=plu(e , b);
                sum = cnt + '.' + ans ;
                cnt = cntclear(cnt);
                ans = ansclear(ans);
                if(ans == "")
                cout << cnt << endl ;
                else
                    cout << cnt + '.' + ans  << endl ;
            }
            else if(positionb != b.npos)//如果a为整数,b为小数
            {
                string  d(b , positionb+1) ;//将小数分离
                string  f(b ,0 , positionb) ;
                ans += d ;
                cnt += plu(f , a);
                cnt = cntclear(cnt);
                ans = ansclear(ans);
                if(ans == "")
                cout << cnt << endl ;
                else
                    cout << cnt + '.' + ans  << endl ;
    
            }
            else
            {
                cout << plu(a , b) << endl ;
    
            }
    
        }
    
        return 0;
    }

     


Guess you like

Origin www.cnblogs.com/nonames/p/11200103.html