简单表达式求值

Warning: 只支持0-9的加减乘除,测试一下博客园功能

Env: Win7+VS2010

$cl main.cpp
$main.exe (3+(4*8))+((6+7)/5)

348*+67+5/+
Result : 37
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>

char buff[1000] = {0};
int count=0;//指向buff中下一个可用的位置
char current;//currentExp所指向的字符
char* currentExp;//指向表达式的指针

void T(void);
void F(void);
void G(void);
void fatal(const char* msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(-1);
}

void T(void)
{
    F();
    while(strchr("+-", current) != NULL && current != '\0')
    {
        char temp = current;
        current = *(++currentExp);
        F();
        buff[count] = temp;
        count++;
    }
}

void F(void)
{
    G();
    while(strchr("*/", current) != NULL && current != '\0')
    {
        char temp = current;
        current = *(++currentExp);
        G();
        buff[count] = temp;
        count++;
    }
}

void G(void)
{
    if (current>='0' && current<='9')
    {
        buff[count]=current;
        count++;
        current = *(++currentExp);
    }
    else if (current == '(')
    {
        current = *(++currentExp);
        T();
        if (current != ')') fatal("mismatched parantheses!");
        current = *(++currentExp);
    }
    else
    {
        fatal("bogus expression!");
    }

}
/*********************************************************************
** 中缀表达式转换为后缀表达式
**
**
** T-->F{(+|-)F}
** F-->G{(*|/)G}
** G--> 0-9
**  --> (T)
*********************************************************************/
void Infix2Postfix(char* expr)
{
    current = *(expr);
    T();
    buff[999] = '\0';
}

int Calculate()
{
    std::stack<int> s;
    int sum = 0;
    int oper1 = 0, oper2 = 0;
    int num = 0;
    char c;
    while(buff[num])
    {
        c = buff[num];
        if (c>'0' && c<'9')
        {
            s.push(c-'0');
        }
        else
        {
            switch (c)
            {
            	case '+':
                    oper1 = s.top();
                    s.pop();
                    oper2 = s.top();
                    s.pop();
                    sum = oper2 + oper1;
                    s.push(sum);
            		break;

                case '-':
                    oper1 = s.top();
                    s.pop();
                    oper2 = s.top();
                    s.pop();
                    sum = oper2 - oper1;
                    s.push(sum);
                    break;

                case '*':
                    oper1 = s.top();
                    s.pop();
                    oper2 = s.top();
                    s.pop();
                    sum = oper2 * oper1;
                    s.push(sum);
                    break;

                case '/':
                    oper1 = s.top();
                    s.pop();
                    oper2 = s.top();
                    s.pop();
                    sum = oper2 / oper1;
                    s.push(sum);
                    break;

            	default:
                    fatal("error");
                    exit(1);
            		break;
            }
        }
        num++;
    }
    return sum;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "usage: %s <expression>\n", argv[0]);
        exit(1);
    }
    currentExp = argv[1];
    Infix2Postfix(currentExp);
    printf( "%s\n", buff);
    printf( "Result : %d\n", Calculate());
    return 0;
}

  

转载于:https://www.cnblogs.com/zhangshine/archive/2011/10/08/2202418.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值