A+B

weixin_30588675發表於2020-04-05

/*給定兩個整數A和B,其表示形式是:從個位開始,每三位數用逗號","隔開。

現在請計算A+B的結果,並以正常形式輸出。

 

  Input

 

         輸入包含多組資料資料,每組資料佔一行,由兩個整數A和B組成(-10^9 < A,B < 10^9)。

        

           Output

          

                   請計算A+B的結果,並以正常形式輸出,每組資料佔一行。

                  

                     Sample Input

                    

-234,567,890 123,456,789

1,234 2,345,678

                           

                              Sample Output

                             

                                     -111111101  2346912

*/

#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

int main()

{

         char a[30],b[30];

         int i,sum1,sum2,num;

         bool tag=false;

         while(cin>>a>>b)

         {

                   if(a[0]=='-')

                   {

                            tag=true;

                            i=1;

                   }

                   else

                            i=0;

                   sum1=0;

                   for(;i<strlen(a);i++)

                   {

                            if(a[i]!=',')

                            {

                                     num=a[i]-'0';

                                     sum1=sum1*pow(10.0,1.0)+num;

                            }

                   }

                   if(tag==true)

                   {

                            sum1=sum1*(-1);

                            tag=false;

                   }

                   if(b[0]=='-')

                   {

                            tag=true;

                            i=1;

                   }

                   else

                            i=0;

                   sum2=0;

                   for(;i<strlen(b);i++)

                   {

                            if(b[i]!=',')

                            {

                                     num=b[i]-'0';

                                     sum2=sum2*pow(10.0,1.0)+num;

                            }

                   }

                   if(tag==true)

                   {

                            sum2=sum2*(-1);

                            tag=false;

                   }

                   cout<<(sum1+sum2)<<endl;

         }

         return 0;

}

 

轉載於:https://www.cnblogs.com/-wang-xin/archive/2012/11/23/2785108.html

相關文章