使用C++编写的简单示例,实现一个计算器,支持加减乘除四则运算

1.使用C++编写的简单示例,实现一个计算器,支持加减乘除四则运算

#ifndef CALCULATOR_H
#define CALCULATOR_H

class Calculator {
public:
    Calculator();
    double add(double a, double b);
    double subtract(double a, double b);
    double multiply(double a, double b);
    double divide(double a, double b);
};

#endif

在Calculator类中,我们声明了构造函数、加法、减法、乘法和除法的成员函数。

2.创建一个calculator.cpp源文件,实现Calculator类的成员函数,代码如下:

#include "calculator.h"

Calculator::Calculator() {
    // 构造函数
}

double Calculator::add(double a, double b) {
    return a + b;
}

double Calculator::subtract(double a, double b) {
    return a - b;
}

double Calculator::multiply(double a, double b) {
    return a * b;
}

double Calculator::divide(double a, double b) {
    if (b == 0) {
        return 0;  // 除数为0,返回0
    } else {
        return a / b;
    }
}

在add()函数中,我们计算a和b的和。在subtract()函数中,我们计算a和b的差。在multiply()函数中,我们计算a和b的积。在divide()函数中,我们首先判断除数是否为0,如果为0则返回0,否则计算a和b的商。

3.创建一个main.cpp源文件,实现程序的主要逻辑,代码如下:

#include <iostream>
#include "calculator.h"

int main() {
    Calculator calc;

    // 加法
    std::cout << "2 + 3 = " << calc.add(2, 3) << std::endl;

    // 减法
    std::cout << "5 - 1 = " << calc.subtract(5, 1) << std::endl;

    // 乘法
    std::cout << "4 * 6 = " << calc.multiply(4, 6) << std::endl;

    // 除法
    std::cout << "8 / 2 = " << calc.divide(8, 2) << std::endl;
    std::cout << "8 / 0 = " << calc.divide(8, 0) << std::endl;

    return 0;
}

在main()函数中,我们创建一个Calculator对象,然后分别进行加法、减法、乘法和除法的计算,并输出结果。

上述代码只是一个简单的C++计算器示例,实际开发中需要根据具体需求进行相应的调整和开发。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一个简单实现,具体步骤如下: 1. 定义一个字符串变量,用于存储用户输入的算式。 2. 通过cin将用户输入的算式存入字符串变量中。 3. 定义一个函数,用于将字符串中的数字和运算符提取出来,并存储到两个栈中。 4. 定义一个函数,用于计算两个数的结果。 5. 定义一个函数,用于根据运算符的优先级进行计算。 6. 定义一个函数,用于计算整个算式的结果。 以下是示例代码: ```c++ #include <iostream> #include <stack> #include <cstring> using namespace std; // 判断字符是否为数字 bool isDigit(char c) { return c >= '0' && c <= '9'; } // 判断字符是否为运算符 bool isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } // 将字符串中的数字和运算符提取出来,并存储到两个栈中 void parseExpression(string expr, stack<int>& nums, stack<char>& ops) { int num = 0; bool hasNum = false; for (int i = 0; i < expr.length(); i++) { char c = expr[i]; if (isDigit(c)) { num = num * 10 + (c - '0'); hasNum = true; } else { if (hasNum) { nums.push(num); num = 0; hasNum = false; } if (isOperator(c)) { while (!ops.empty() && (ops.top() == '*' || ops.top() == '/') && (c == '+' || c == '-')) { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(op == '+' ? num1 + num2 : num1 - num2); } ops.push(c); } else if (c == '(') { ops.push(c); } else if (c == ')') { while (ops.top() != '(') { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(op == '+' ? num1 + num2 : num1 - num2); } ops.pop(); } } } if (hasNum) { nums.push(num); } while (!ops.empty()) { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(op == '+' ? num1 + num2 : num1 - num2); } } // 计算两个数的结果 int calc(int num1, int num2, char op) { if (op == '+') { return num1 + num2; } else if (op == '-') { return num1 - num2; } else if (op == '*') { return num1 * num2; } else if (op == '/') { return num1 / num2; } return 0; } // 根据运算符的优先级进行计算 void calculate(stack<int>& nums, stack<char>& ops) { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); int result = calc(num1, num2, op); nums.push(result); } // 计算整个算式的结果 int evaluate(string expr) { stack<int> nums; stack<char> ops; parseExpression(expr, nums, ops); return nums.top(); } int main() { string expr; cout << "请输入算式:" << endl; cin >> expr; int result = evaluate(expr); cout << "结果为:" << result << endl; return 0; } ``` 这个实现还有一些不足之处,比如没有对输入的算式进行校验,如果用户输入的算式不符合规范,程序可能会崩溃。但是这个实现可以帮助你了解计算器程序的基本原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值