C++计算器

不说别的,先上代码

#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
    double num1, num2;
    char operation;
    std::cout << "欢迎使用高级计算器!" << std::endl;
    std::cout << "请输入第一个数字: ";
    std::cin >> num1;
    std::cout << "请输入操作符 (+, -, *, /, sqrt): ";
    std::cin >> operation;
    if (operation == 's' || operation == 'S') {
        std::cout << "sqrt(" << num1 << ") = " << std::fixed << std::setprecision(2) << std::sqrt(num1) << std::endl;
    } else {
        std::cout << "请输入第二个数字: ";
        std::cin >> num2;

        switch (operation) {
            case '+':
                std::cout << num1 << " + " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 + num2) << std::endl;
                break;
            case '-':
                std::cout << num1 << " - " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 - num2) << std::endl;
                break;
            case '*':
                std::cout << num1 << " * " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 * num2) << std::endl;
                break;
            case '/':
                if (num2 != 0) {
                    std::cout << num1 << " / " << num2 << " = " << std::fixed << std::setprecision(2) << (num1 / num2) << std::endl;
                } else {
                    std::cout << "错误: 除数不能为零." << std::endl;
                }
                break;
            default:
                std::cout << "错误: 不支持的操作符." << std::endl;
                break;
        }
    }
    return 0;
}

代码不长,如果用的是Embarcadero Dev-C++,要把中文改成英文

感谢你看到最后,关注我一下,我会做出更好的C++代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值