/*
author: openxmpp@163.com
由于GCC,VS的内置数据类型的加法受限于int,long,unsigned long等的精度,则对于大数的加法应采用字符串的形式进行输入和计算。
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
*/
//排版好看的版本
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define ARRAY_SIZE 1024 void reverseInPlace( char str[] ) { int strLen = strlen(str); char *start = str; char *end = str + strLen - 1; while ( start < end ) { char tmp = *start; *start = *end; *end = tmp; start ++; end -- ; } } void sum( char left[ARRAY_SIZE],char right[ARRAY_SIZE],char result[ARRAY_SIZE]) { memset(result,'\0',sizeof(result)); reverseInPlace(left); reverseInPlace(right); int leftLen = strlen(left); int rightLen = strlen(right); bool biggerThan10 = false; int i = 0; for ( ; i < leftLen || i < rightLen ; i ++ ) { char leftTmp = (left[i])?(left[i]):('0'); //如果加数和被加数的位数不同,则位数少的那个加完后其余地方的填充位为0,而不是期望的'0'(对应的ASCII码为48) char rightTmp = (right[i])?(right[i]):('0'); int sum = 0; int tmpValue = (leftTmp - '0') + (rightTmp - '0'); sum = (true == biggerThan10)?(tmpValue+1):(tmpValue); biggerThan10 = (sum>=10)?(true):(false); result[i] = sum % 10 + '0'; //当前的进位值为取余计算 } if ( true == biggerThan10 ) { result[i] = '1'; } reverseInPlace(left); //翻转回去,方便打印 reverseInPlace(right); reverseInPlace(result); //由于是从个位开始计算,输出的时候习惯个位在最右面,需要翻转 return ; } int main(int argc,char* argv[]) { int grpnumber; cin >> grpnumber; char left[1024]; char right[1024]; char result[1024] = {0}; for ( int i = 1 ; i <= grpnumber ; i ++ ) { memset(left,1024); memset(right,1024); memset(result,1024); scanf("%s %s",left,right); sum(left,right,result); cout << "Case " << i <<":"<<endl; printf ("%s + %s = %s\n",result); if ( i != grpnumber ) { cout << endl; //坑爹的ACM判定系统,当处理最后一个输出时,不需要加'\n' } } return 0; }
下面是排版可以拷贝直接测试的版本
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define ARRAY_SIZE 1024
void reverseInPlace( char str[] )
{
int strLen = strlen(str);
char *start = str;
char *end = str + strLen - 1;
while ( start < end )
{
char tmp = *start;
*start = *end;
*end = tmp;
start ++;
end -- ;
}
}
void sum( char left[ARRAY_SIZE],char right[ARRAY_SIZE],char result[ARRAY_SIZE])
{
memset(result,'\0',sizeof(result));
reverseInPlace(left);
reverseInPlace(right);
int leftLen = strlen(left);
int rightLen = strlen(right);
bool biggerThan10 = false;
int i = 0;
for ( ; i < leftLen || i < rightLen ; i ++ )
{
char leftTmp = (left[i])?(left[i]):('0'); //如果加数和被加数的位数不同,则位数少的那个加完后其余地方的填充位为0,而不是期望的'0'(对应的ASCII码为48
char rightTmp = (right[i])?(right[i]):('0');
int sum = 0;
int tmpValue = (leftTmp - '0') + (rightTmp - '0');
sum = (true == biggerThan10)?(tmpValue+1):(tmpValue);
biggerThan10 = (sum>=10)?(true):(false);
result[i] = sum % 10 + '0'; //当前的进位值为取余计算
}
if ( true == biggerThan10 )
{
result[i] = '1';
}
reverseInPlace(left); //翻转回去,方便打印
reverseInPlace(right);
reverseInPlace(result); //由于是从个位开始计算,输出的时候习惯个位在最右面,需要翻转
return ;
}
int main(int argc, char* argv[])
{
int grpnumber;
cin >> grpnumber;
char left[1024];
char right[1024];
char result[1024] = {0};
for ( int i = 1 ; i <= grpnumber ; i ++ )
{
memset(left,1024);
memset(right,1024);
memset(result,1024);
scanf("%s %s",left,right);
sum(left,right,result);
cout << "Case " << i <<":"<<endl;
printf ("%s + %s = %s\n",result);
if ( i != grpnumber )
{
cout << endl; //坑爹的ACM判定系统,当处理最后一个输出时,不需要加'\n'
}
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。