题解 | #实现简单计算器功能#

实现简单计算器功能

https://www.nowcoder.com/practice/e7c08272a4b7497fb990ce7abb1ee952

#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    char* msg[30] = {str, nullptr};

    for (int i = 0; (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 << num1 / num2 << endl;
        }
    }


    return 0;
}

#c++#
全部评论

相关推荐

真没意想到
从明天开始狠狠卷JV...:我建议释放offer,然后这个offer流到我头上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务