一个计算器

lixinyan 爱因斯坦 2023-11-04 9:40:00 2023-11-07 17:38:59 6
#include <bits/stdc++.h>
using namespace std;


int main() {

    int a, b;
    char intt;
    cin >> a >> b >> intt;
    switch (intt) {
        case '+':
            cout << a + b;
            break;
        case '-':
            cout << a - b;
            break;
        case '*':
            cout << a * b;
            break;
        case '/':
            if (b == 0)
                cout << "Divided by zero!";
            else
                cout << a / b;
            break;
			case '%':
            if (b == 0)
                cout << "Divided by zero!";
            else
                cout << a % b;
            break;
        default:
            cout << "Invalid operator!";
    }

    return 0;
}
{{ vote && vote.total.up }}

共 2 条回复

Even226 逗比

#include #include #include #include #include

int getPriority(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; }

double executeOperation(double operand1, double operand2, char op) { switch (op) { case '+': return operand1 + operand2; case '-': return operand1 - operand2; case '*': return operand1 * operand2; case '/': if (operand2 == 0) { throw std::runtime_error("除零错误"); } return operand1 / operand2; default: return 0.0; } } double calculateExpression(const std::string& expression) { std::stack operands; std::stack operators; std::istringstream iss(expression); char token;

while (iss >> token) {
    if (isdigit(token) || (token == '.')) {
        double number;
        iss.putback(token);
        iss >> number;
        operands.push(number);
    } else if (token == '(') {
        operators.push(token);
    } else if (token == ')') {
        while (!operators.empty() && operators.top() != '(') {
            char op = operators.top();
            operators.pop();
            double operand2 = operands.top();
            operands.pop();
            double operand1 = operands.top();
            operands.pop();
            operands.push(executeOperation(operand1, operand2, op));
        }
        if (!operators.empty() && operators.top() == '(') {
            operators.pop();
        } else {
            throw std::runtime_error("括号不匹配");
        }
    } else if (token == '+' || token == '-' || token == '*' || token == '/') {
        while (!operators.empty() && getPriority(operators.top()) >= getPriority(token)) {
            char op = operators.top();
            operators.pop();
            double operand2 = operands.top();
            operands.pop();
            double operand1 = operands.top();
            operands.pop();
            operands.push(executeOperation(operand1, operand2, op));
        }
        operators.push(token);
    }
}

while (!operators.empty()) {
    char op = operators.top();
    operators.pop();
    double operand2 = operands.top();
    operands.pop();
    double operand1 = operands.top();
    operands.pop();
    operands.push(executeOperation(operand1, operand2, op));
}

if (operands.size() == 1) {
    return operands.top();
} else {
    throw std::runtime_error("表达式无效");
}

}

int main() { std::string expression; std::cout << "请输入一个表达式(支持四则运算和括号): "; std::getline(std::cin, expression);

try {
    double result = calculateExpression(expression);
    std::cout << "结果为: " << result << std::endl;
} catch (const std::exception& e) {
    std::cerr << "错误: " << e.what() << std::endl;
}

return 0;

}

Even226 逗比

我也有计算器