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

实现简单计算器功能

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;
}

全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务