我一直在研究一种算法来将“a + b * c-d/e”转换为它的后缀形式。我已经准备好了http://en.wikipedia.org/wiki/Shunting-yard_algorithm wiki,但是我的逻辑有问题。当我打印出我的队列时,我得到了一个没有操作员的“一个骰子”。似乎没有任何东西被推入我的堆栈?或者如果是,它不会被推入我的队列。我的队列/堆栈正在由我创建的双链表类实现。插入后缀算法
#include <iostream>
#include "LinkedList.h"
#include "Stack.h"
#include "Queue.h"
using namespace std;
int oper(char c)
{
switch(c) {
case '!':
return 4;
case '*': case '/': case '%':
return 3;
case '+': case '-':
return 2;
case '=':
return 1;
}
return 0;
}
int main() {
LinkedList* list = new LinkedList();
string infix = "a+b*c-d/e";
Stack *holder = new Stack();
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->push(temp);
prev = temp;
continue;
}
if(oper(temp)<oper(prev))
{
newstring->queue(holder->popStack());
temp = '\0';
continue;
}
else
holder->push(temp);
prev = temp;
}
else
newstring->queue(temp);
}
while(!holder->isEmpty())
{
newstring->queue(holder->popStack());
}
newstring->printQueue();
return 0;
}
获取调试器,并亲自查看:) – NPE 2013-03-17 07:17:57