上图转自新浪微博。车主用一系列简单计算给出了自己的电话号码,即:
2/2=1、3+2=5、9=3、9=3、0%=0、叁=3、5−2=3、9/3=3、1×3=3、23=8、8/2=4,最后得到的电话号码就是 153 3033 3384。
本题就请你写个程序自动完成电话号码的转换,以帮助那些不会计算的人。
输入格式:
输入用 11 行依次给出 11 位数字的计算公式,每个公式占一行。这里仅考虑以下几种运算:加(+
)、减(-
)、乘(*
)、除(/
)、取余(%
,注意这不是上图中的百分比)、开平方根号(sqrt
)、指数(^
)和文字(即 0 到 9 的全小写汉语拼音,如 ling
表示 0)。运算符与运算数之间无空格,运算数保证是不超过 1000 的非负整数。题目保证每个计算至多只有 1 个运算符,结果都是 1 位整数。
输出格式:
在一行中给出电话号码,数字间不要空格。
输入样例:
2/2
3+2
sqrt9
sqrt9
6%2
san
5-2
9/3
1*3
2^3
8/2
输出样例:
15330333384
测试点4:“保证每个计算至多只有 1 个运算符”
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
for (int i = 0; i < 11; i ++ ) {
string line;
getline(cin, line);
int num = -1;
if (line.find("+") >= 0 && line.find("+") < line.size()) {
num = stoi(line.substr(0,line.find("+")))
+ stoi(line.substr(line.find("+") + 1,line.size() - line.find("+")));
} else if (line.find("-") >= 0 && line.find("-") < line.size()) {
num = stoi(line.substr(0,line.find("-")))
- stoi(line.substr(line.find("-") + 1,line.size() - line.find("-")));
} else if (line.find("*") >= 0 && line.find("*") < line.size()) {
num = stoi(line.substr(0,line.find("*")))
* stoi(line.substr(line.find("*") + 1,line.size() - line.find("*")));
} else if (line.find("/") >= 0 && line.find("/") < line.size()) {
num = stoi(line.substr(0,line.find("/")))
/ stoi(line.substr(line.find("/") + 1,line.size() - line.find("/")));
} else if (line.find("%") >= 0 && line.find("%") < line.size()) {
num = stoi(line.substr(0,line.find("%")))
% stoi(line.substr(line.find("%") + 1,line.size() - line.find("%")));
} else if (line.find("^") >= 0 && line.find("^") < line.size()) {
num = pow(stoi(line.substr(0,line.find("^"))), stoi(line.substr(line.find("^") + 1,line.size() - line.find("^"))));
} else if (line.find("sqrt") >= 0 && line.find("sqrt") < line.size()) {
num = sqrt(stoi(line.substr(line.find("sqrt") + 4,line.size() - line.find("sqrt"))));
} else if (line.find("ling") >= 0 && line.find("ling") < line.size()) {
num = 0;
} else if (line.find("yi") >= 0 && line.find("yi") < line.size()) {
num = 1;
} else if (line.find("er") >= 0 && line.find("er") < line.size()) {
num = 2;
} else if (line.find("san") >= 0 && line.find("san") < line.size()) {
num = 3;
} else if (line.find("si") >= 0 && line.find("si") < line.size()) {
num = 4;
} else if (line.find("wu") >= 0 && line.find("wu") < line.size()) {
num = 5;
} else if (line.find("liu") >= 0 && line.find("liu") < line.size()) {
num = 6;
} else if (line.find("qi") >= 0 && line.find("qi") < line.size()) {
num = 7;
} else if (line.find("ba") >= 0 && line.find("ba") < line.size()) {
num = 8;
} else if (line.find("jiu") >= 0 && line.find("jiu") < line.size()) {
num = 9;
} else {
num = stoi(line);
}
cout << num;
}
return 0;
}