题目没给字符串分割的库,大家怎么都用上了呢?
在这给一种常规方法。
#include <iostream>
using namespace std;
int main() {
char str[100] = { 0 };
cin.getline(str, sizeof(str));
// write your code here......
char t[3];
int a = 0, b = 0;
int f = 0, k = 0;
for (auto i = str; *i != 0; i++) {
if (*i == ' ') {
f += 1;
continue;
}
if (f == 0)
t[k++] = *i;
if (f == 1)
a = a * 10 + (*i - '0');
if (f == 2)
b = b * 10 + (*i - '0');
}
if (*t == 'a' or * t == 'A')
cout << a + b;
if (*t == 's' or * t == 'S')
cout << a - b;
if (*t == 'm' or * t == 'M')
cout << a* b;
if (*t == 'd' or * t == 'D')
if (b != 0)
cout << a / b;
else
cout << "Error";
return 0;
}