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

实现简单计算器功能

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

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

int main() {

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

    // write your code here......
    int i = 0;
    string operation;
    int a = 0, b = 0;
    while (str[i] != '\0' && !isspace(str[i]))
    {
        char t = (str[i] >= 'a' && str[i] <= 'z') ? str[i] : str[i] - 'A' + 'a';
        operation = operation + t;
        i++;
    }
    i++;
    while (str[i] != '\0' && !isspace(str[i]))
    {
        int d = str[i] - '0';
        a = 10 * a + d;
        i++;
    }
    i++;
    while (str[i] != '\0' && !isspace(str[i]))
    {
        int d = str[i] - '0';
        b = 10 * b + d;
        i++;
    }

    if (operation == "add")
        cout << a + b;
    else if (operation == "sub")
        cout << a - b;
    else if (operation == "mul")
        cout << a * b;
    else if (operation == "div")
    {
        if (b == 0)
            cout << "Error";
        else
            cout << a / b;
    }

    return 0;
}

按题目要求写代码就可以了

全部评论

相关推荐

Java抽象带篮子:安卓怎么你了
投递荣耀等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务