回顾数据结构之栈的应用-表达式求值

求测试

#include <iostream>
#include <map>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;

stack<char> op;
stack<double> s;
char hash[256][256];
char c[1000];
int num;
char buf[1005];
int id;

void init()
{
    hash['+']['+'] = '>';hash['+']['-'] = '>';hash['+']['*'] = '<';hash['+']['/'] = '<';
    hash['+']['('] = '<';hash['+'][')'] = '>';hash['+']['#'] = '>';

    hash['-']['+'] = '>';hash['-']['-'] = '>';hash['-']['*'] = '<';hash['-']['/'] = '<';
    hash['-']['('] = '<';hash['-'][')'] = '>';hash['-']['#'] = '>';

    hash['*']['+'] = '>';hash['*']['-'] = '>';hash['*']['*'] = '>';hash['*']['/'] = '>';
    hash['*']['('] = '<';hash['*'][')'] = '>';hash['*']['#'] = '>';

    hash['/']['+'] = '>';hash['/']['-'] = '>';hash['/']['*'] = '>';hash['/']['/'] = '>';
    hash['/']['('] = '<';hash['/'][')'] = '>';hash['/']['#'] = '>';

    hash['(']['+'] = '<';hash['(']['-'] = '<';hash['(']['*'] = '<';hash['(']['/'] = '<';
    hash['(']['('] = '<';hash['('][')'] = '=';hash['(']['#'] = ' ';

    hash[')']['+'] = '>';hash[')']['-'] = '>';hash[')']['*'] = '>';hash[')']['/'] = '>';
    hash[')']['('] = ' ';hash[')'][')'] = '>';hash[')']['#'] = '>';

    hash['#']['+'] = '<';hash['#']['-'] = '<';hash['#']['*'] = '<';hash['#']['/'] = '<';
    hash['#']['('] = '<';hash['#'][')'] = ' ';hash['#']['#'] = '=';
}


double GetNum(double a, char th, double b)
{
    if(th == '+')
        return a + b;
    else if(th == '-')
        return a - b;
    else if(th == '*')
        return a * b;
    else
        return a / b;
}

char Precede(char a, char b)
{
    return hash[a][b];
}

void read()
{
    while(buf[id] == ' ' || buf[id] == '\t'){
        id++;
    }
    c[num++] = buf[id++];
}

int main()
{
    init();
    while(gets(buf)){
        int i;
        for(i = 0; buf[i]; ++i){
            if(buf[i] < '0' || buf[i] > '9'){
                break;
            }
        }

        if(buf[i] == 0){
            printf("%s.0000\n", buf);
            continue;
        }

        int len = strlen(buf);
        buf[len++]='#';


        id = 0;
        num = 0;
        while(!op.empty())
            op.pop();
        while(!s.empty())
            s.pop();

        op.push('#');
        int flag = 0;
        int tt = 0;

        read();
        while(c[num - 1] != '#' || op.top() != '#'){
            if(c[num - 1] >= '0' && c[num - 1] <= '9'){
                tt = tt * 10 + c[num - 1] - '0';
                flag = 1;
                read();
            }
            else{
                if(flag){
                    s.push(tt + 0.0);
                    flag = tt = 0;
                }

                if(c[num - 1] == '-' || c[num - 1] == '+'){
                    if(num >= 2 && (c[num - 2] == ')' || (c[num - 2] >= '0' && c[num - 2] <= '9')))
                        ;
                    else{
                        s.push(0.0);
                    }
                }

                switch(Precede(op.top(), c[num - 1])){
                case '<':
                    op.push(c[num - 1]);
                    read();
                    break;
                case '=':
                    op.pop();
                    read();
                    break;
                case '>':
                    char th = op.top();
                    op.pop();
                    double b = s.top(); s.pop();
                    double a = s.top(); s.pop();
                    s.push(GetNum(a, th, b));
                    break;
                }
            }
        }
        printf("%.4lf\n", s.top());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值