算术表达式的转换

 

算术表达式的转换

Time Limit: 1000MS Memory limit: 65536K

题目描述

小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。
   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。

输入

 输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)

输出

 输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。

示例输入

a*b+(c-d/e)*f#

示例输出

+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+
这题错了好多次
最后看着同学的写出来,感觉很悲伤…………sad………………
下面是她的地址 http://www.cnblogs.com/LK1994/p/3221196.html有一个小错误,不知道她是否修改了
 
 
代码
  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<iostream>
  5 #include<queue>
  6 #include<stack>
  7 using namespace std;
  8 struct node
  9 {
 10     char data;
 11     struct node *l,*r;
 12 };
 13 stack<char>sta;
 14 stack<node*>stanode;
 15 queue<char>que;
 16 char s[100];
 17 void preorder(struct node *p)
 18 {
 19     if(p == NULL)
 20         return;
 21     printf("%c",p->data);
 22     preorder(p->l);
 23     preorder(p->r);
 24 }
 25 void inorder(struct node *p)
 26 {
 27     if(p == NULL)
 28         return;
 29     inorder(p->l);
 30     printf("%c",p->data);
 31     inorder(p->r);
 32 }
 33 void postorder(struct node *p)
 34 {
 35     if(p== NULL)
 36         return;
 37     postorder(p->l);
 38     postorder(p->r);
 39     printf("%c",p->data);
 40 }
 41 void f()
 42 {
 43     int i;
 44     for(i=0; s[i]!='#'; i++)
 45     {
 46         if(s[i]>='a'&&s[i]<='z')
 47             que.push(s[i]);
 48         else if(sta.empty()||sta.top()=='('||s[i]=='(')
 49             sta.push(s[i]);
 50         else if(s[i]==')')
 51         {
 52             while(sta.top()!='(')
 53             {
 54                 que.push(sta.top());
 55                 sta.pop();
 56             }
 57             sta.pop();
 58         }
 59         else if(s[i]=='+'||s[i]=='-')
 60         {
 61             while(!sta.empty()&&sta.top()!='(')
 62             {
 63                 que.push(sta.top());
 64                 sta.pop();
 65             }
 66             sta.push(s[i]);
 67         }
 68         else if(s[i]=='*'||s[i]=='/')
 69         {
 70             while(!sta.empty()&&sta.top()!='('&&(sta.top()=='*'||sta.top()=='/'))
 71             {
 72                 que.push(sta.top());
 73                 sta.pop();
 74             }
 75             sta.push(s[i]);
 76         }
 77     }
 78     while(!sta.empty())
 79     {
 80         que.push(sta.top());
 81         sta.pop();
 82     }
 83 }
 84 int main()
 85 {
 86     node *p,*root;
 87     gets(s);
 88     f();
 89     while(!que.empty())
 90     {
 91         if(que.front()>='a'&&que.front()<='z')
 92         {
 93             p=(node*)malloc(sizeof(node));
 94             p->data=que.front();
 95             que.pop();
 96             p->l=NULL;
 97             p->r=NULL;
 98             stanode.push(p);
 99         }
100         else
101         {
102             p=(node*)malloc(sizeof(node));
103             root=p;
104             p->data=que.front();
105             que.pop();
106             p->r=stanode.top();
107             stanode.pop();
108             p->l=stanode.top();
109             stanode.pop();
110             stanode.push(p);
111         }
112     }
113     preorder(root);
114     printf("\n");
115     inorder(root);
116     printf("\n");
117     postorder(root);
118     printf("\n");
119     while(!que.empty())
120     {
121         printf("%c",que.front());
122         que.pop();
123     }
124     printf("\n");
125     return 0;
126 }
View Code

转载于:https://www.cnblogs.com/kongkaikai/p/3224683.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值