题解 | #实现简单计算器功能#
实现简单计算器功能
https://www.nowcoder.com/practice/e7c08272a4b7497fb990ce7abb1ee952
#include <iostream> #include <sstream> #include <algorithm> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... string s {str}; // char 字符串 转换为 string istringstream is {s}; // 将一个string对象作为istream的源 string op; int n1, n2; is >> op >> n1 >> n2; transform(op.begin(), op.end(), op.begin(), ::tolower); if (op == "add") { cout << n1+n2; } else if (op == "sub") { cout << n1-n2; } else if (op == "mul") { cout << n1*n2; } else if (op == "div") { if (n2 == 0) cout << "Error"; else cout << n1/n2; } return 0; }