问题 A: 字符串处理
时间限制: 1 Sec 内存限制: 32 MB
提交: 203 解决: 28
[提交][状态][讨论版][命题人:外部导入]
题目描述
读入两个字符串,字符串除了数字还可能包括 '—'、'E'、'e'、'.',相加之后输出结果,如果是浮点型,要求用科学计数法表示(最多包含10个有效数字)。
输入
输入包含多组测试数据。
每组输入占两行,每行一个字符串,测试数据保证字符串的构成严格按照题目中的描述。
输出
输出两个数字相加的结果,每组输出占一行。
样例输入
34.56 2.45e2
样例输出
2.7956e2
#include<stdio.h>
int main()
{
char str1[50], str2[50];
long long s, s1, s2, ans;
int i, a1, a2, a, b, c, w, flag;
while (scanf("%s %s", str1, str2) != EOF) {
s1 = s2 = flag = b = c = a1 = 0;
for (i = 0; str1[i]; i++) {
if (str1[i] == '-')
flag = 1;
else if (str1[i] == '.')
c = 1;
else if (str1[i] == 'e' || str1[i] == 'E') {
sscanf(str1 + i + 1, "%d", &b);
a1 += b;
break;
}
else {
s1 = s1 * 10 + str1[i] - '0';
a1 -= c;
}
}
if (flag) s1 = -s1;
flag = b = c = a2 = 0;
for (i = 0; str2[i]; i++) {
if (str2[i] == '-')
flag = 1;
else if (str2[i] == '.')
c = 1;
else if (str2[i] == 'e' || str2[i] == 'E') {
sscanf(str2 + i + 1, "%d", &b);
a2 += b;
break;
}
else {
s2 = s2 * 10 + str2[i] - '0';
a2 -= c;
}
}
if (flag) s2 = -s2;
if (a1<a2)
for (; a1<a2; a2--)
s2 *= 10;
else if (a1>a2)
for (; a1>a2; a1--)
s1 *= 10;
a = a1; s = s1 + s2;
if (!s) {
printf("0\n");
continue;
}
while (a<0 && s % 10 == 0) {
s /= 10;
a++;
}
if (a >= 0) {
printf("%lld", s);
for (i = 0; i<a; i++)
printf("0");
printf("\n");
continue;
}
flag = 0;
if (s<0) {
s = -s;
flag = 1;
}
ans = 1; w = 0;
while (ans <= s) {
ans *= 10;
w++;
}
if (ans>1) {
ans /= 10;
w--;
}
if (flag)
printf("-");
printf("%lld", s / ans);
if (ans>1)
printf(".%lld", s%ans);
printf("e%d\n", a + w);
}
return 0;
}
/*
自己写了一次,用了以前的部分代码,反而很乱,莫名其妙内存超限,没AC;
以上是网上的一份AC代码,因为没注释,不想看他的逻辑
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct Float {
int x[100] = { 0 }, y[100] = { 0 };
};
void add(string a, string b, int aflag, int bflag) {
Float p, q;
int flag = 1, px = 0, py = 0;
for (int i = 0; i < a.length(); i++) {
if (a[i] == '.') {
flag = 0;
continue;
}
if (flag) p.x[px++] = (a[i] - '0')*aflag;
else p.y[py++] = (a[i] - '0')*aflag;
}
reverse(p.x, p.x + px);//逆序存储整数
flag = 1;
int qx = 0, qy = 0;
for (int i = 0; i < b.length(); i++) {
if (b[i] == '.') {
flag = 0;
continue;
}
if (flag) q.x[qx++] = (b[i] - '0')*bflag;
else q.y[qy++] = (b[i] - '0')*bflag;
}
reverse(q.x, q.x + qx);//逆序存储整数
//小数部分相加
int len = max(py, qy);
for (int i = len - 1; i > 0; i--) {
p.y[i - 1] += (p.y[i] + q.y[i]) / 10;
p.y[i] = (p.y[i] + q.y[i]) % 10;
if (i == len - 1 && p.y[i] == 0)
len--;
}
p.x[0] += (p.y[0] + q.y[0]) / 10;
p.y[0] = (p.y[0] + q.y[0]) % 10;
//整数部分相加
for (int i = 0; i < max(px, qx); i++) {
p.x[i + 1] += (p.x[i] + q.x[i]) / 10;
p.x[i] = (p.x[i] + q.x[i]) % 10;
}
int temp;
if (p.x[max(px, qx)] != 0) {
cout << p.x[max(px, qx)];
temp = max(px, qx) - 1;
}
else {
cout << p.x[max(px, qx) - 1];
temp = max(px, qx) - 2;
}
if (len == 0) {
for (int i = temp; i >= 0; i--) {
cout << p.x[i];
}
cout << endl;
return;
}
else {
cout << ".";
for (int i = temp; i >= 0; i--) {
cout << p.x[i];
}
for (int i = 0; i < len; i++) {
cout << p.y[i];
}
cout << "e" << temp + 1 << endl;
}
}
string tran(string a) {
//科学计数法转为常规浮点型
int index = -1;
if (a.find('e') != string::npos) index = a.find('e');
else if (a.find('E') != string::npos) index = a.find('E');
if (index != -1) {
string s = a.substr(index + 1, a.length() - index);
a.erase(a.begin() + index, a.end());
int num = 0;
while (s.size()) {
num *= 10;
num += s[0] - '0';
s.erase(s.begin());
}
if (a.find('.') != string::npos) {
a.erase(a.begin() + 1);
if (num == a.length() - 1) {}
else if (num < a.length() - 1) {
a.insert(num + 1, ".");
}
else {
int t = num - a.length() + 1;
while (t--) {
a += '0';
}
}
}
else while (num--) {
a += '0';
}
}
return a;
}
int main() {
string a, b;
while (cin >> a >> b) {
int aflag = 1, bflag = 1;
if (a[0] == '-') {
aflag = -1;
a.erase(a.begin());
}
if (b[0] == '-') {
bflag = -1;
b.erase(b.begin());
}
a = tran(a);
b = tran(b);
add(a, b, aflag, bflag);
}
return 0;
}
*/