oidd @ 2022-11-29 22:33:59
#include <bits/stdc++.h>
using namespace std;
int n;
char bi[100][15];
int main()
{
cin >> n; //cin 不读回车
cin.get();
for (int i = 0; i < n; ++i)
cin.getline(bi[i], 15);
char pre;
//int num1, num2;
for (int i = 0; i < n; ++i)
{
int num1 = 0, num2 = 0;
int tol = 0;
bool flag1 = false;
int length1 = 0, length2 = 0, length3 = 0;
if (bi[i][0] >= 'a' && bi[i][0] <= 'c')
{
pre = bi[i][0];
int length = 2;
while (bi[i][length])
{
if (bi[i][length] == ' ')
{
flag1 = true;
length++;
}
if (!flag1)
{
num1 = num1 * 10 + (bi[i][length] - 48);
length1++; length++;
}
else
{
num2 = num2 * 10 + (bi[i][length] - 48);
length2++; length++;
}
}
}
else
{
int length = 0;
while (bi[i][length])
{
if (bi[i][length] == ' ')
{
flag1 = true;
length++;
}
if (!flag1)
{
num1 = num1 * 10 + (bi[i][length] - 48);
length1++; length++;
}
else
{
num2 = num2 * 10 + (bi[i][length] - 48);
length2++; length++;
}
}
}
if (pre == 'a')
{
tol = num1 + num2;
cout << num1 << '+' << num2 << '=' << num1 + num2 << endl;
}
else if (pre == 'b')
{
tol = num1 - num2;
cout << num1 << '-' << num2 << '=' << num1 - num2 << endl;
}
else
{
tol = num1 * num2;
cout << num1 << '*' << num2 << '=' << num1 * num2 << endl;
}
bool flag2 = false;
if (tol < 0)
flag2 = true;
while (tol)
{
length3++;
tol /= 10;
}
int ans = length1 + length2 + length3 + 2;
flag2 ? cout << ans + 1 << endl : cout << ans << endl;
}
return 0;
}
by Chesapeake_Ripper @ 2022-11-29 22:43:37
我和你写的基本一样,95%一样,洛谷全错,本地就对
by omemi @ 2022-11-30 00:08:50
为什么要读一行,这题完全可以string cin一直读啊
by oidd @ 2022-11-30 12:19:39
@Breath_the_shy 我找到我的问题了,上面cin>>n之后用这个
while((ch = getchar()) != '\n')
continue;
清一下缓冲区
然后这个cin.getline() 他会把回车也读进去,这些字符串最后面会带一个回车(ASCII码是13
by oidd @ 2022-11-30 12:21:22
@oidd 然后那个```
while (tol)
{
length3++;
tol /= 10;
}
这里如果最后运算的结果是零的话,也会有问题
by oidd @ 2022-11-30 12:32:31
\r\n: windows 系统行末结束符 cin.getline()会吃掉\r但不处理\n ?
by Chesapeake_Ripper @ 2022-11-30 15:36:28
@oidd 我用了两个 cin.get();在洛谷的ide上正常运行了,但是结果有乱码咋办
by oidd @ 2022-12-01 13:34:16
@Breath_the_shy 我是菜鸡,乱码就不清楚了,我是在洛谷ide多加了几句cout才发现的问题,在感觉有问题的地方输出一下看看。反正也没法调试