题解 | #实现简单计算器功能#
实现简单计算器功能
https://www.nowcoder.com/practice/e7c08272a4b7497fb990ce7abb1ee952
#include <iostream> #include <string> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // 遍历字符串,根据空格数目,进行op,整数1,整数2的划分 int spacenum = 0; string op = ""; string num_1 = ""; string num_2 = ""; for (int i = 0; str[i] != '\0'; i++){ if (str[i] == ' '){ spacenum++; continue; } else if (spacenum == 0){ str[i] = tolower(str[i]); op += str[i]; //op为数组 } else if (spacenum == 1) num_1 += str[i]; else if (spacenum == 2) num_2 += str[i]; } //字符串类型转化为整型 int x = stoi(num_1); int y = stoi(num_2); if ((string)op == "add") //数组类型转化为字符串类型 cout << x + y; else if ((string)op == "sub") cout << x - y; else if ((string)op == "mul") cout << x*y; else if ((string)op == "div"){ if (y != 0) cout << x/y; else cout << "Error"; } return 0; }#你的秋招进展怎么样了##我的求职思考##零基础学习C++#