2013-03-14 63 views
0

我需要实现缀到后缀转换算法来计算表达式a + b *的CD/E需要创建缀以后缀算法

我还需要做到这一点使用队列(我相信2个不同的队列堆栈是需要)

我使用DoubleLinkList创建了队列类,现在只需要创建这个问题的算法。尽管如此,但我对于如何解决这个问题却很失落。任何帮助,将不胜感激!

至今(我知道这是非常错误的),我有:

string infix = "a+b*c-d/e"; 
    Queue *holder = new Queue(); 
    Queue *newstring = new Queue(); 
    int length = infix.length(); 
    char temp; 
    char prev; 
    for(int i=0; i<length; i++) 
    { 
     temp = infix[i]; 
     if((temp == '+') || (temp == '-') || (temp == '*') || (temp == '/')) 
     { 
      if (holder->isEmpty()) 
      { 
       holder->queue(temp); 
      } 
      if(temp<holder.enqueue()) 
      { 

      } 
     } 
     holder->queue(temp); 

    } 
+0

快速谷歌搜索 “转换到缀后缀” 产生[本内容丰富的文章(http://scriptasylum.com/tutorials/infix_postfix /algorithms/infix-postfix/index.htm)。你可能想从那里开始。 – 2013-03-14 17:32:33

回答

2

我想这是一个家庭作业,让你找出你自己的编程细节是非常重要的。该算法的大纲如下:

Define a stack 
Go through each character in the string 
If it is between 0 to 9, append it to output string. 
If it is left brace push to stack 
If it is operator *+-/ then 
      If the stack is empty push it to the stack 
      If the stack is not empty then start a loop: 
          If the top of the stack has higher precedence 
          Then pop and append to output string 
          Else break 
        Push to the stack 

If it is right brace then 
      While stack not empty and top not equal to left brace 
      Pop from stack and append to output string 
      Finally pop out the left brace. 

If there is any input in the stack pop and append to the output string. 
+0

对不起,碰到一个旧帖子,但我已经搜索了同样的东西,我想知道为什么这总是使用这种特定的堆栈算法,以及它背后的原理。我可以看到它的工作原理,但对于为什么我有点困惑,我使用了一种替代算法来获得相同的结果。你可以添加的任何细节都会很棒。 – ffledgling 2013-12-30 21:02:03