需要创建中缀到后缀的算法
Need to create infix to postfix algorithm
我需要实现中缀到后缀的转换算法来计算表达式a+b*c-d/e
我还需要使用queue(我相信需要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);
}
我认为这是一个家庭作业,所以重要的是你自己找出编程细节。算法概要如下:
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.
我认为你应该创建一个操作符和值的树。
您可以根据树的遍历顺序从中缀转换为后缀再转换为前缀。
你的老师可能会给你布置作业,让你在三者之间进行转换。
这里有一些文章:
德克萨斯大学
YouTube视频
Wikipedia -表达式树
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 基于ELO的团队匹配算法
- C++选择排序算法中的逻辑错误
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C++A*算法并不总是在路径中具有目标节点
- 排序算法c++
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 算法问题:查找从堆栈中弹出的所有序列
- 下面是排序算法O(n)吗
- KMP算法和LPS表构造的运行时间
- 为什么我的排序算法会更改数组值
- 高效的字符串截断算法,按顺序删除相等的前缀和后缀
- 后缀评估算法
- 使用后缀数组算法进行Burrows-Wheeler变换
- Ukkonen的后缀树算法,有什么必要?
- 后缀数组算法
- 找出后缀数组的两种算法中哪一种更快,为什么?
- 中缀到后缀算法
- 需要创建中缀到后缀的算法