21xrx.com
2024-12-22 23:42:24 Sunday
登录
文章检索 我的文章 写文章
C++ 算术表达式求值代码
2023-06-30 20:12:23 深夜i     --     --
C++ 算术表达式 求值 代码 计算器

本文将介绍一个用C++编写的算术表达式求值代码,该代码可以对基本的算术表达式进行求值,包括加减乘除和括号。下面是代码的实现过程:

首先,我们使用栈来存储数字和运算符。具体步骤如下:

1. 遍历整个表达式,从左到右逐个读取字符。

2. 如果读取的是数字,则将其入栈。

3. 如果读取的是运算符,则从栈中弹出两个数字,计算出它们的结果,再将结果入栈。

4. 如果读取的是左括号,则将其入栈。

5. 如果读取的是右括号,则从栈中弹出数字和运算符,直到遇到左括号,计算出中间结果,再将结果入栈。

6. 当遍历完整个表达式时,栈中剩余的数字和运算符一定可以计算出最终结果。

下面是完整的C++代码实现:


#include <iostream>

#include <stack>

#include <string>

using namespace std;

int evaluate(string expression) {

  stack<int> numbers;

  stack<char> operators;

  int number = 0;

  for (int i = 0; i < expression.length(); i++) {

    char current = expression[i];

    if (isdigit(current)) {

      number = number * 10 + (current - '0');

    }

    else if (current == '+' || current == '-' || current == '*' || current == '/') {

      numbers.push(number);

      number = 0;

      while (!operators.empty() && ((current != '*' && current != '/') || (operators.top() == '*' || operators.top() == '/'))) {

        int operand2 = numbers.top();

        numbers.pop();

        int operand1 = numbers.top();

        numbers.pop();

        char op = operators.top();

        operators.pop();

        int result;

        if (op == '+') {

          result = operand1 + operand2;

        }

        else if (op == '-')

          result = operand1 - operand2;

        

        else if (op == '*') {

          result = operand1 * operand2;

        }

        else

          result = operand1 / operand2;

        

        numbers.push(result);

      }

      operators.push(current);

    }

    else if (current == '(') {

      operators.push(current);

    }

    else if (current == ')') {

      while (operators.top() != '(') {

        int operand2 = numbers.top();

        numbers.pop();

        int operand1 = numbers.top();

        numbers.pop();

        char op = operators.top();

        operators.pop();

        int result;

        if (op == '+') {

          result = operand1 + operand2;

        }

        else if (op == '-')

          result = operand1 - operand2;

        

        else if (op == '*') {

          result = operand1 * operand2;

        }

        else

          result = operand1 / operand2;

        

        numbers.push(result);

      }

      operators.pop();

    }

  }

  numbers.push(number);

  while (!operators.empty()) {

    int operand2 = numbers.top();

    numbers.pop();

    int operand1 = numbers.top();

    numbers.pop();

    char op = operators.top();

    operators.pop();

    int result;

    if (op == '+') {

      result = operand1 + operand2;

    }

    else if (op == '-')

      result = operand1 - operand2;

    

    else if (op == '*') {

      result = operand1 * operand2;

    }

    else

      result = operand1 / operand2;

    

    numbers.push(result);

  }

  return numbers.top();

}

int main() {

  string expression = "1+2*3+(4-2)/2";

  cout << evaluate(expression) << endl;

  return 0;

}

以上就是C++算术表达式求值代码的实现过程和完整代码示例。通过这个代码,我们可以轻松地对基本的算术表达式进行求值,实现计算器等功能。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复