题解 | #实现简单计算器功能#
实现简单计算器功能
https://www.nowcoder.com/practice/e7c08272a4b7497fb990ce7abb1ee952
#include <cctype>
#include <iostream>
using namespace std;
int main() {
char str[100] = { 0 };
cin.getline(str, sizeof(str));
// write your code here......
int i = 0;
string operation;
int a = 0, b = 0;
while (str[i] != '\0' && !isspace(str[i]))
{
char t = (str[i] >= 'a' && str[i] <= 'z') ? str[i] : str[i] - 'A' + 'a';
operation = operation + t;
i++;
}
i++;
while (str[i] != '\0' && !isspace(str[i]))
{
int d = str[i] - '0';
a = 10 * a + d;
i++;
}
i++;
while (str[i] != '\0' && !isspace(str[i]))
{
int d = str[i] - '0';
b = 10 * b + d;
i++;
}
if (operation == "add")
cout << a + b;
else if (operation == "sub")
cout << a - b;
else if (operation == "mul")
cout << a * b;
else if (operation == "div")
{
if (b == 0)
cout << "Error";
else
cout << a / b;
}
return 0;
}
按题目要求写代码就可以了
查看16道真题和解析