CPP54. 实现简单计算器功能
描述
输入描述
输出描述
输出运算后的结果(除法不考虑小数),如果除数为 0,则不做运算,输出“Error”示例1
输入:
add 10 3
输出:
13
示例2
输入:
sub 10 3
输出:
7
示例3
输入:
mul 10 3
输出:
30
示例4
输入:
div 10 3
输出:
3
示例5
输入:
div 10 0
输出:
Error
示例6
输入:
ADD 20 20
输出:
40
C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-11-06
#include <iostream> #include<string> #include<vector> using namespace std; std::vector<string> str_fenge(const string &str,const string&patt) { std::vector<string>vec; if (patt.empty() || str.empty()) return vec; size_t start = 0; size_t index = str.find_first_of(patt, start); while (index != str.npos) { //if(start!=index) vec.push_back(str.substr(start, index - start)); start = index + 1; index = str.find_first_of(patt, start); } if (!str.substr(start).empty()) { vec.push_back(str.substr(start)); } return vec; } int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... string strs(str); string patt(" "); auto vec = str_fenge(str,patt); string ss=vec.at(0); int num1=atoi(vec.at(1).c_str()); int num2=atoi(vec.at(2).c_str()); if(ss.compare("add")==0) { cout<<num1+num2<<endl; } else if(ss.compare("sub")==0) { cout<<num1-num2<<endl; } else if(ss.compare("mul")==0) { cout<<num1*num2<<endl; } else if(ss.compare("div")==0) { if(num2==0) { cout<<"Error"<<endl; return -1; } cout<<num1/num2<<endl; } else { } return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-10-24
#include <iostream> #include <cstring> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); char* msg[30] = { str, nullptr }; int i = 0; while ((msg[i] = strtok(msg[i], " ")) && ++i); char* op = msg[0]; int num1 = atoi(msg[1]); int num2 = atoi(msg[2]); if (strcasecmp(op, "add") == 0) { cout << num1 + num2 << endl; } else if (strcasecmp(op, "sub") == 0) { cout << num1 - num2 << endl; } else if (strcasecmp(op, "mul") == 0) { cout << num1 * num2 << endl; } else if (strcasecmp(op, "div") == 0) { if (num2 == 0) { cout << "Error" << endl; } else { cout << (double)num1 / num2 << endl; } } return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 416KB, 提交时间: 2022-02-09
#include <iostream> #include <cstring> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); char * msg[30] = {str,nullptr}; int i = 0; while ((msg[i] = strtok(msg[i], " ")) && ++i); char *op = msg[0]; int num1 = atoi(msg[1]); int num2 = atoi(msg[2]); if(strcasecmp(op,"add")==0){ cout<<num1+num2<<endl; } else if(strcasecmp(op,"sub")==0){ cout<<num1-num2<<endl; } else if(strcasecmp(op,"mul")==0){ cout<<num1*num2<<endl; }else if(strcasecmp(op,"div")==0){ if(num2==0){ cout<<"Error"; }else{ cout<<num1/num2; } } // write your code here...... return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 532KB, 提交时间: 2022-01-25
#include <iostream> #include<string> #include<cstring> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... size_t len = strlen(str); std::string way; std::string num1; std::string num2; int space = 0; for (int i = 0; i < len; i++) { if (str[i] == ' ') space++; else if (space == 0) way += (str[i]); else if (space == 1) num1 += str[i]; else if (space == 2) num2 += str[i]; } int nub1 = std::atoi(&num1[0]); int nub2 = std::atoi(&num2[0]); if (strcasecmp("add", way.c_str()) == 0) std::cout << nub1 + nub2 << std::endl; else if (strcasecmp("sub", way.c_str()) == 0) std::cout << nub1 - nub2 << std::endl; else if (strcasecmp("mul", way.c_str()) == 0) std::cout << nub1 * nub2 << std::endl; else if (strcasecmp("div", way.c_str()) == 0 && nub2 != 0) std::cout << nub1 / nub2 << std::endl; else if (nub2 == 0) std::cout << "Error" << std::endl; return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 304KB, 提交时间: 2021-10-16
#include <iostream> #include <string.h> #include <string> #include<cstdlib> using namespace std; int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); string op = ""; string num1 = ""; string num2 = ""; int space = 0; for(int i = 0; str[i] != '\0'; i++){ if(str[i] == ' '){ space++; continue; } else if(space == 0) //第一空格之前都是操作符 op += str[i]; else if(space == 1) //第一二个空格之间,为第一个数字 num1 += str[i]; else if(space == 2) num2 += str[i]; //第二个空格之后,为第二个数字 } int x = stoi(num1); int y = stoi(num2); if(strcasecmp(op.c_str(), "add") == 0) cout << x + y << endl; else if(strcasecmp(op.c_str(), "sub") == 0) cout << x - y << endl; else if(strcasecmp(op.c_str(), "mul") == 0) cout << x * y << endl; else if(strcasecmp(op.c_str(), "div") == 0){ if(y == 0) cout << "Error" << endl; else cout << (double) x / y << endl; } return 0; }