中缀表达式转换到后缀表达式

hqxiaoying 2004-12-23 09:25:14
按照以下方法进行转换,为什么结果不一样,当运算符的优先级一样时,如*和/时,是压栈还是出栈呢,请高手能够予以解答,谢谢
转换算法:
1)检查输入的下一元素。
2)假如是个操作数,输出。
3)假如是个开括号,将其压栈。
4)假如是个运算符,则
i) 假如栈为空,将此运算符压栈。
ii) 假如栈顶是开括号,将此运算符压栈。
iii) 假如此运算符比栈顶运算符优先级高,将此运算符压入栈中。
iv) 否则栈顶运算符出栈并输出,重复步骤4。
5)假如是个闭括号,栈中运算符逐个出栈并输出,直到遇到开括号。开括号出栈并丢弃。
6)假如输入还未完毕,跳转到步骤1。
7)假如输入完毕,栈中剩余的所有操作符出栈并输出它们。
以对下中缀表达式2*(x+y)/(1-x)转换成后缀表达式应该是什么?
...全文
461 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hqxiaoying 2004-12-27
  • 打赏
  • 举报
回复
多谢各位的指教,明白了
timgreen 2004-12-24
  • 打赏
  • 举报
回复
#include <cmath>
#include <stdio.h>
#include <string>
#include <stack>
using namespace std;

int t;
char s[401];
struct Tnode {char c; int next, last;} node[400];
int Nnode;
stack<int> op, e;

int GetType(char c){
if (c == '(')
return 7;
if (c == '^')
return 6;
if (c == '/')
return 5;
if (c == '*')
return 4;
if (c == '-')
return 3;
if (c == '+')
return 2;
if (c == ')')
return 1;
return -1;
}

int main(){
//freopen("ONP.in","r",stdin);
//freopen("ONP.out","w",stdout);
//read
scanf("%d\n", &t);
for (; t; --t){
scanf("%s\n", s);
//main
Nnode = 0;
int f, len = strlen(s), tmp;
for (f = 0; f < len; ++f)
if ((tmp = GetType(s[f])) > 0){
while (!op.empty() && GetType(node[op.top()].c) >= tmp && node[op.top()].c != '('){
int n1 = e.top(); e.pop();
int n2 = e.top(); e.pop();
node[node[n1].last].next = op.top();
node[node[n2].last].next = n1;
node[n2].last = node[node[n1].last].next;
op.pop();
e.push(n2);
}
if (tmp == 1)//if ')' go and find '(';
op.pop();//if this is not '(' the expression must be wrong;
else{
node[Nnode].c = s[f];
node[Nnode].next = -1;
node[Nnode].last = Nnode;
op.push(Nnode++);
}
}
else{
node[Nnode].c = s[f];
node[Nnode].next = -1;
node[Nnode].last = Nnode;
e.push(Nnode++);
}
while (!op.empty()){
int n1 = e.top(); e.pop();
int n2 = e.top(); e.pop();
node[node[n1].last].next = op.top();
node[node[n2].last].next = n1;
node[n2].last = node[node[n1].last].next;
op.pop();
e.push(n2);
}
//print
for (f = e.top(), e.pop(); f != -1; f = node[f].next)
printf("%c", node[f].c);
printf("\n");
}
return 0;
}
baryjim 2004-12-24
  • 打赏
  • 举报
回复
sorry,楼主,把代码贴错帖子了,实在抱歉
baryjim 2004-12-24
  • 打赏
  • 举报
回复
#include<stdlib.h>
#include<stdio.h>

struct node
{
char ch;
struct node *l;
struct node *r;
};

typedef struct node* Node;
Node h=NULL;
const int N=6;
char*pred="ABDECFG";
char*inod="DBEACGF";

void CreateBinTree(Node&,int,int,int,int);
void PrintBinTree(Node);
void main()
{
/*scanf("%s %s",pred,inod);*/
CreateBinTree(h,0,N,0,N);
printf("\n");
PrintBinTree(h);
printf("\n");
}
void CreateBinTree(Node& t,int i,int j,int u,int v)
{
int k,l;
t=(Node)malloc(sizeof(struct node));
t->ch=pred[i];
t->l=t->r=0;
if(i<j)
{
if(pred[i]!=inod[u]) //有左子树,创建左子树
{
l=u;
while(inod[l]!=pred[i])
l++;
l--;
k=i;

while(pred[k]!=inod[l])
k++;
CreateBinTree(t->l,i+1,k,u,l);
CreateBinTree(t->r,k+1,j,l+2,v);
}
else //没有左子树,直接创建右子树
CreateBinTree(t->r,i+1,j,u+1,v);
}
}

void PrintBinTree(Node t)
{
if(t){
printf("%c",t->ch);
PrintBinTree(t->l);
PrintBinTree(t->r);
} else return;
}
liem 2004-12-24
  • 打赏
  • 举报
回复
如果二个运算符级别相同:从左向右运算的是出栈,否则是进栈
rickone 2004-12-23
  • 打赏
  • 举报
回复
2*(x+y)/(1-x)

--->

2xy+*1x-/
baryjim 2004-12-23
  • 打赏
  • 举报
回复
如*和/时,是压栈还是出栈呢?

应该是出栈!
a*b/c变成ab*c/

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧