程序最美(寻路)

你还在坚持练习你的技术吗?运动员天天训练,音乐家也会演练更难的曲章。你呢?

后缀表达式的计算

后缀表达式的计算

         对后缀表达式进行计算,得到表达式的值。

         例如有后缀表达式:

2 1 + 3 *

         其结果应为:

       9

         后缀表达式:

       1 3 5 * + 7 9 / -

         其结果应为:

15.222

         后缀表达式:

       1 3 + 5 7 - * 9 /

         其结果应为:

       -0.889

         后缀表达式计算程序如下:

// 后缀表达式的计算
#include <iostream>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <string>
using namespace std;

void get_postfix(vector<string>& postf)
{
    postf.clear();
    string line;
    getline(cin, line);
    istringstream sin(line);
    string tmp;
    while (sin >> tmp)
    {
        postf.push_back(tmp);
    }
}

void init_op(map<string, int>& ops)
{
    ops.clear();
    ops["+"] = 100;
    ops["-"] = 100;
    ops["*"] = 200;
    ops["/"] = 200;
    ops["("] = 1000;
    ops[")"] = 0;
}

bool is_operator(const string& hs, const map<string, int>& ops)
{
    map<string, int>::const_iterator cit = ops.find(hs);\
    if (cit != ops.end())
    {
        return true;
    }
    else
    {
        return false;
    }
}

double cal_post(const vector<string>& postf, const map<string, int>& ops)
{
    stack<double> or_st;
    double operand = 0.0, a = 0.0, b = 0.0, c = 0.0;
    for (vector<string>::size_type i = 0; i != postf.size(); ++i)
    {
        if (!is_operator(postf[i], ops))
        {
            operand = static_cast<double>(atof(postf[i].c_str()));
            or_st.push(operand);
        }
        else
        {
            switch (postf[i][0])
            {
            case '+':
                b = or_st.top();
                or_st.pop();
                a = or_st.top();
                or_st.pop();
                c = a + b;
                or_st.push(c);
                break;
            case '-':
                b = or_st.top();
                or_st.pop();
                a = or_st.top();
                or_st.pop();
                c = a - b;
                or_st.push(c);
                break;
            case '*':
                b = or_st.top();
                or_st.pop();
                a = or_st.top();
                or_st.pop();
                c = a * b;
                or_st.push(c);
                break;
            case '/':
                b = or_st.top();
                or_st.pop();
                a = or_st.top();
                or_st.pop();
                c = a / b;
                or_st.push(c);
                break;
            default:
                break;
            }
        }
    }
    if (or_st.size() == 1)
    {
        return or_st.top();
    }
    else
    {
        return -10000000000000.0;
    }
}

int main()
{
    map<string, int> ops;
    init_op(ops);
    vector<string> postf;
    
    while (1)
    {
        get_postfix(postf);
        double ret = cal_post(postf, ops);
        cout << ret << endl << endl;
    }
    
    system("PAUSE");
    return 0;
}

 

         上述后缀表达式计算过程中用到了数据结构为,该栈存储的是中间结果的操作数。

         数据结构:

         0.设置一个栈,该栈用于存储中间计算的操作数;

         具体的算法逻辑为:

         1.从左到右顺序扫描整个后缀表达式;

         2.如果是操作数,则将该操作数压入到栈中;

         3.如果是操作符,则从栈中弹出对应的操作数,注意操作数的顺序;根据操作符进行运

            算,并将结果重新压入到栈中;

         4.直至将整个栈扫描完毕;

         5.如果后缀表达式是合法的,则扫描完毕后,栈中只有一个元素,该元素的值即为后缀

            表达式的结果。

posted on 2013-07-17 00:02  unixfy  阅读(4169)  评论(0编辑  收藏  举报

导航