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

来源:互联网 发布:软件概要设计说明 编辑:程序博客网 时间:2024/05/19 00:50

求测试

#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;}


原创粉丝点击