【问题标题】:RPN evaluation C++RPN 评估 C++
【发布时间】:2014-03-02 14:21:44
【问题描述】:

你好)这是我将中缀表达式转换为后缀表达式的代码,但是我无法理解如何评估我得到的后缀表达式,我将非常感谢任何提示。我不是要代码,尽管这会有所帮助。

#include <iostream> 
#include <stack> 
#include <string>

using namespace std; 
bool operation(char b) 
{
    return b=='+' || b=='-' || b=='*' || b=='/' ; 

} 

bool priority(char a, char b) 
{
    if(a=='(')
    {
        return true; 
    } 
    if(a=='+' || a=='-') 
    {
        return true; 

    } 
    if(b=='+' || b=='-') 
    {
        return false; 
    } 

    return true; 

} 
int main() 
{

    string a;
    string res; 
    stack<char> s; 
        cin>>a; 
    for(int i=0; i<a.size(); i++) 
    {
        if(a[i]!='(' && a[i]!=')' && operation(a[i])==false) 
        {

            res=res+a[i]; 
        } 
        if(a[i]=='(') 
        {
            s.push(a[i]) ; 
        } 
        if(a[i]==')') 
        {
            while(s.top()!='(') 
            {
                res+=s.top(); 
                s.pop();
            }
            s.pop(); 
        } 
        if(operation(a[i])==true) 
        {
            if(s.empty() || (s.empty()==false && priority(s.top(), a[i])) ) 
            {
                s.push(a[i]); 
            }
            else 
            {
                while(s.empty()==false && priority(s.top(),a[i])==false ) 
                { 
                    res+=s.top(); 
                    s.pop(); 
                }
                s.push(a[i]) ; 
            }

        } 

    } 
    while(s.empty()==false) 
    {
        res+=s.top(); 
        s.pop(); 
    } 
    cout<<res; 




    return 0; 
} 

附:我没有任何 cmets,但我认为代码本身是不言自明的))
附言先感谢您。

【问题讨论】:

  • 通常情况下,如果您实际尝试,学习一些东西会更容易。也许可以在一张纸上写下这些步骤(如果你愿意,也可以在文本编辑器中记录),然后按照流程进行 - 从简单的事情开始,然后一直到 (a + (b + c * d) / e) 等等。
  • 但是你没有展示你到目前为止所做的事情......
  • 真正的想法是你做的工作(这显然是为了学习目的,所以你是需要学习的人,你并没有通过阅读/复制别人的代码来真正学到任何东西!),我们给你建议。也许您正在寻找 wedoyourhomeworkforyou.com?
  • 当然,从阅读代码中学习是可能的。但是,您不会通过简单地获取别人的代码并将其插入您的程序来学习。因为编程的很大一部分是学习如何以程序员的身份思考——换句话说,如何从一个概念中提出代码。
  • 我的意思是,如果您向我们展示您的所作所为,我们可以为您提供指导。如果你只是说“我想做X”,而没有实际显示“你在哪里”,那么有两个明显的结论:1.你实际上什么都没做,希望互联网是所有问题的答案. 2. 你做了某事,但出于某种原因没有表现出来。不幸的是,如果没有您共享代码,很容易假设这两者中的#1。此外,在这种情况下,提出改进现有代码的方法比解释代码中的每一步要容易得多。

标签: c++ rpn


【解决方案1】:

如果您形成用空格分隔的 posfix 表达式,那么以下将是编写评估器的最简单方法之一,只需遵循评估的algorithm

这假设 RPN 类似于 5 1 2 + 4 * + 3 -(以空格分隔)

int evaluate_posfix ( const std::string& expression )
{

    int l,r,ans;
    std::stringstream postfix(expression);
    std::vector<int> temp;
    std::string s;
    while ( postfix >> s )
    {
        if( operation(s[0]) )
        {
            //Pull out top two elements
            r = temp.back();
            temp.pop_back();
            l = temp.back();
            temp.pop_back();
            // Perform the maths
            switch( s[0])
            {
                case '+': ans =  l + r ; break;
                case '-': ans =  l - r ; break;
                case '*': ans =  l * r ; break;
                case '/': ans =  l / r ; break; // check if r !=0
            }

            temp.push_back( ans ); // push the result of above operation
        }
        else
        {
            temp.push_back( std::stoi(s) );
        }
    }

    return temp[0] ; //last element is the answer
} 

【讨论】:

  • 这是未经测试的代码,您需要遵循它并根据您的需要进行相应的修改。它只是基本的方法。
  • 谢谢!))这真的很有帮助))
猜你喜欢
  • 2016-11-26
  • 1970-01-01
  • 2021-08-18
  • 2014-01-13
  • 2012-02-10
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多