代码如下:
#include <bits/stdc++.h>
using namespace std;
int i;
int tempa, tempb;
string s[51], temps;
char tempch = '+';
char *ch;
namespace prb {
int digit(int num) {
int ans = 0;
while (num != 0)
num /= 10, ans++;
return ans;
//整形位数判断函数
}
}
string str(int func, int x, int y, int xl, int yl) {
temps = "\0";
int funx = 1, funnum = 0, digit;
for (int funi = y; funi >= x; funi--, funx *= 10)
tempa = tempa + (s[func][funi] - '0') * funx;
for (int funi = yl, funx = 1; funi >= xl; funi--, funx *= 10)
tempb = tempb + (s[func][funi] - '0') * funx;
if (tempch == '+')
funnum = tempa + tempb;
else if (tempch == '-')
funnum = tempa - tempb;
else if (tempch == '*')
funnum = tempa * tempb;
tempa = 0, tempb = 0;
if (funnum < 0)
temps.push_back('-');
funnum = abs(funnum);
digit = prb::digit(funnum);
while (funnum != 0) {
temps.push_back((funnum % 10) + '0');
funnum /= 10;
}
if (temps[0] == '-')
reverse(temps.begin() + 1, temps.end());
else
reverse(temps.begin(), temps.end());
return temps;
}
int main() {
cin >> i;
temps = getchar();
for (int tempi = 0; tempi < i; tempi++) {
getline(cin, s[tempi]);
if (s[tempi][0] == 'a') {
tempch = '+';
s[tempi] = s[tempi].substr(2, s[tempi].size() - 2);
} else if (s[tempi][0] == 'b') {
tempch = '-';
s[tempi] = s[tempi].substr(2, s[tempi].size() - 2);
} else if (s[tempi][0] == 'c') {
tempch = '*';
s[tempi] = s[tempi].substr(2, s[tempi].size() - 2);
}
for (int tempj = s[tempi].size() - 1; tempj >= 0; tempj--) {
if (s[tempi][tempj] == ' ') {
s[tempi][tempj] = tempch;
s[tempi] = s[tempi] + '=' + str(tempi, 0, tempj - 1, tempj + 1, s[tempi].size() - 1);
break;
}
}
}
for (int tempi = 0; tempi < i; tempi++) {
cout << s[tempi] << endl;
cout << s[tempi].size() << endl;
}
return 0;
}
测试点1测试代码 只改动了输出 将75-78行代码改为
for (int tempi = 0; tempi < i; tempi++)
{
cout << "99+999=1098" << endl;
cout << 11 << endl;
}
能过 改为
for (int tempi = 0; tempi < i; tempi++)
{
if (s[tempi] == "99+999=1098")
{
cout << "99+999=1098" << endl;
cout << 11 << endl;
}
}
不能过。 这是为什么。 dev运行一模一样。