问题 C: 浮点数加法
时间限制: 1 Sec 内存限制: 32 MB
献花: 38 解决: 21
[献花][花圈][TK题库]
题目描述
求2个浮点数相加的和
题目中输入输出中出现浮点数都有如下的形式:
P1P2…Pi.Q1Q2…Qj
对于整数部分,P1P2…Pi是一个非负整数
对于小数部分,Qj不等于0
输入
对于每组案例,第1行是测试数据的组数n,每组测试数据占2行,分别是两个加数。
每组测试数据之间有一个空行,每行数据不超过100个字符
输出
每组案例是n行,每组测试数据有一行输出是相应的和。
输出保证一定是一个小数部分不为0的浮点数
样例输入
2
3.756
90.564
4543.5435
43.25
样例输出
94.32
4586.7935
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstring>
#include <string>
using namespace std;
struct BigData
{
int data[1024];
int size;
BigData()
{
memset(data, 0, sizeof(data));
size = 0;
}
};
void SetValue(BigData &e, char a[], int size)
{
int i = size -1;
e.size = size;
for (int j = 0; j < size; ++j, --i)
{
e.data[j] = a[i] - '0';
}
}
void PrintZ(BigData e)
{
int idx = e.size - 1;
while (idx >= 0)
{
printf("%d", e.data[idx--]);
}
}
BigData AddZ(BigData a, BigData b)
{
int idx = 0 ,carry = 0;
BigData res;
while (idx < a.size || idx < b.size)
{
res.data[idx] = (carry + a.data[idx] + b.data[idx]) % 10;
carry = (carry + a.data[idx] + b.data[idx]) / 10;
++idx;
}
if (carry)
{
res.data[idx++] = carry;
}
res.size = idx;
return res;
}
BigData AddW(BigData a, BigData b,int &cry)
{
int idx = 0, carry = 0;
BigData res;
while (idx < a.size || idx < b.size)
{
res.data[idx] = (carry + a.data[idx] + b.data[idx]) % 10;
carry = (carry + a.data[idx] + b.data[idx]) / 10;
++idx;
}
cry = carry;
res.size = idx;
return res;
}
void PrintW(BigData e)
{
int idx = e.size - 1;
int zero = 0;
while (zero < e.size && e.data[zero] == 0)++zero;
while (idx >= zero)
{
printf("%d", e.data[idx--]);
}
}
bool isPrintBD(BigData e)
{
int idx = 0;
while (idx < e.size)
{
if (e.data[idx++])
return true;
}
return false;
}
void clear(BigData &e)
{
memset(e.data, 0, sizeof(int) * 1024);
e.size = 0;
}
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
char str1[120], str2[120], tmp[120];
int d1p, d2p, N, l1 ,l2 = 0,len,carry = 0 ,i;
BigData a, b,zs,ws;
while (scanf("%d",&N) != EOF)
{
while (N--)
{
scanf("%s %s", str1, str2);
l1 = strlen(str1);
l2 = strlen(str2);
for (d1p = 0; str1[d1p] != '.'; ++d1p); // 找到标点符号.
for (d2p = 0; str2[d2p] != '.'; ++d2p);
str1[d1p] = 0;
str2[d2p] = 0;
len = (l1 - d1p) > (l2 - d2p) ? (l2 - d2p -1) : (l1 - d1p -1);
for (i = 0; i < len; ++i)
tmp[i] = str1[d1p + 1 + i];
tmp[i] = 0;
SetValue(a, tmp, len);
for (i = 0; i < len; ++i)
tmp[i] = str2[d2p + 1 + i];
tmp[i] = 0;
SetValue(b, tmp, len);
ws = AddW(a, b, carry);
clear(a);
clear(b);
str1[d1p - 1] += carry;
strcpy(tmp, str1);
SetValue(a, tmp, d1p);
strcpy(tmp, str2);
SetValue(b, tmp, d2p);
zs = AddZ(a, b);
clear(a);
clear(a);
PrintZ(zs);
clear(zs);
if (isPrintBD(ws))
printf(".");
PrintW(ws);
clear(ws);
for (int i = d1p + len + 1; i < l1; ++i)
printf("%c", str1[i]);
for (int i = d2p + len + 1; i < l2; ++i)
printf("%c", str2[i]);
printf("\n");
getchar(); getchar();//消除空行
}
}
return 0;
}
/**************************************************************
Problem: 1922
User: Sharwen
Language: C++
Result: 升仙
Time:1 ms
Memory:1708 kb
****************************************************************/