题解 | #实现简单计算器功能#
实现简单计算器功能
https://www.nowcoder.com/practice/e7c08272a4b7497fb990ce7abb1ee952
#include <iostream> #include "string.h" using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... char act[4]; int num_1, num_2; for (int i = 0; str[i] != '\0'; i++) { str[i] = tolower(str[i]); if (isspace(str[i])) { memcpy(act, str, i); num_1 = atoi(&str[i + 1]); num_2 = atoi(&str[i + 3]); break; } } if (!strcmp(act, "add")) cout << num_1 + num_2; else if (!strcmp(act, "sub")) cout << num_1 - num_2; else if (!strcmp(act, "mul")) cout << num_1* num_2; else if (!strcmp(act, "div")) { if (num_2 == 0) cout << "Error"; else cout << num_1 / num_2; } return 0; }