简单的表达式求值

#include <bits/stdc++.h>
using namespace std;
#define M 100
char ops[7] = {'+', '-', '*', '/', '(', ')', '='};
char cmp[7][7]= {{'>', '>', '<', '<', '<', '>', '>'},
    {'>', '>', '<', '<', '<', '>', '>'},
    {'>', '>', '>', '>', '<', '>', '>'},
    {'>', '>', '>', '>', '<', '>', '>'},
    {'<', '<', '<', '<', '<', '=', ' '},
    {'>', '>', '>', '>', ' ', '>', '>'},
    {'<', '<', '<', '<', '<', ' ', '='}
};
bool check(char ch)
{
    for (int i = 0; i < 7; i++)
        if (ch == ops[i])
            return true;
    return false;
}
char C(char ch1, char ch2)
{
    int i, m, n;
    for (i = 0; i < 7; i++)
    {
        if (ch1 == ops[i])
            m = i;
        if (ch2 == ops[i])
            n = i;
    }
    return   cmp[m][n];

}
void cal(int x, char op, int y, int &z)
{
    switch (op)
    {
    case '+':
        z = x + y;
        break ;
    case '-':
        z = x - y;
        break ;
    case '*':
        z = x * y;
        break ;
    case '/':
        z = x / y;
        break ;

    }
}

void work(char *str, int &result)
{
    int a, b, v;
    char ch, op;
    int temp, i = 0;
    stack <char> oper;
    stack <int> num;
    oper.push('=');
    ch = str[i++];
    while (ch != '=' || oper.top() != '=')
    {
        if (check(ch))
        {
            switch (C(oper.top(), ch))
            {
            case '<':
                oper.push(ch);
                ch = str[i++];
                break;
            case '=':
                oper.pop();
                ch = str[i++];
                break;
            case '>':
                op = oper.top();
                oper.pop();
                b = num.top();
                num.pop();
                a = num.top();
                num.pop();
                cal(a, op, b, v);
                num.push(v);
                break;

            }
        }
        else
        {
            temp = ch - '0';
            num.push(temp);
            ch=str[i++];
        }

    }
    result = num.top();
    printf("%d\n",result);
}
int main()
{
    int i = 0;
    int result;
    char *str;
    str = new char[M];
    cin >> str[i];
    while (str[i] != '=')
    {
        cin >> str[++i];
    }
    work(str, result);
    return 0;
}

数据结构中的简单表达式求值是指对一个由数字和运算符组成的表达式进行计算得到结果的过程。常见的简单表达式包括四则运算(加、减、乘、除)以及括号。 在进行简单表达式求值时,可以使用栈这种数据结构来辅助计算。具体步骤如下: 1. 创建两个栈,一个用于存储操作数(数字),另一个用于存储运算符。 2. 从左到右遍历表达式的每个字符。 3. 如果当前字符是数字,则将其压入操作数栈。 4. 如果当前字符是运算符,则进行如下操作: - 如果运算符栈为空,或者栈顶的运算符是左括号,则将当前运算符压入运算符栈。 - 否则,比较当前运算符与栈顶运算符的优先级: - 如果当前运算符的优先级大于栈顶运算符的优先级,则将当前运算符压入运算符栈。 - 否则,从操作数栈中弹出两个操作数,从运算符栈中弹出一个运算符,进行计算,并将结果压入操作数栈。然后将当前运算符压入运算符栈。 5. 如果当前字符是左括号,则将其压入运算符栈。 6. 如果当前字符是右括号,则进行如下操作: - 从操作数栈中弹出两个操作数,从运算符栈中弹出一个运算符,进行计算,并将结果压入操作数栈,直到遇到左括号为止。将左括号从运算符栈中弹出。 7. 遍历完整个表达式后,如果运算符栈不为空,则进行如下操作: - 从操作数栈中弹出两个操作数,从运算符栈中弹出一个运算符,进行计算,并将结果压入操作数栈,直到运算符栈为空。 8. 最终,操作数栈中的唯一元素即为表达式的求值结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值