您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之栈二:一般算术表达式转换成后缀式

2016-08-03 09:27 447 查看
</pre><center style="font: 12px/18px 微软雅黑, 黑体, 宋体, Verdana, Helvetica, Arial, Geneva, sans-serif; margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;"><h2 style="margin: 10px 0px 7px; padding: 0px; color: rgb(124, 169, 237); line-height: 24px; font-family: 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; font-size: 20px; font-weight: normal;">数据结构实验之栈二:一般算术表达式转换成后缀式</h2><h4 style="margin: 0px 0px 5px; padding: 0px; color: red; line-height: 20px; font-family: 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; font-size: 16px; font-weight: normal;"></h4></center><center style="font: 12px/18px 微软雅黑, 黑体, 宋体, Verdana, Helvetica, Arial, Geneva, sans-serif; margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;"><h4 style="margin: 0px 0px 5px; padding: 0px; color: rgb(51, 51, 51); line-height: 20px; font-family: 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; font-size: 16px; font-weight: normal;">Time Limit: 1000MS Memory limit: 65536K</h4></center><h2 style="font: 20px/24px 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; margin: 10px 0px 7px; padding: 0px; color: rgb(124, 169, 237); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;">题目描述</h2><div style="font: 12px/18px 微软雅黑, 黑体, 宋体, Verdana, Helvetica, Arial, Geneva, sans-serif; margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;" class="pro_desc">对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。</div><h2 style="font: 20px/24px 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; margin: 10px 0px 7px; padding: 0px; color: rgb(124, 169, 237); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;">输入</h2><div style="font: 12px/18px 微软雅黑, 黑体, 宋体, Verdana, Helvetica, Arial, Geneva, sans-serif; margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;" class="pro_desc">输入一个算术表达式,以‘#’字符作为结束标志。</div><h2 style="font: 20px/24px 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; margin: 10px 0px 7px; padding: 0px; color: rgb(124, 169, 237); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;">输出</h2><div style="font: 12px/18px 微软雅黑, 黑体, 宋体, Verdana, Helvetica, Arial, Geneva, sans-serif; margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;" class="pro_desc">输出该表达式转换所得到的后缀式。</div><h2 style="font: 20px/24px 微软雅黑, 'Trebuchet MS', Helvetica, Arial, Geneva, sans-serif; margin: 10px 0px 7px; padding: 0px; color: rgb(124, 169, 237); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;">示例输入</h2><div style="font: 16px/20px 'Courier New', Courier, monospace; margin: 0px; padding: 0px; border: 1px solid rgb(173, 173, 173); text-align: left; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: pre; font-size-adjust: none; font-stretch: normal; background-color: rgb(224, 224, 224); -webkit-text-stroke-width: 0px;" class="data"><pre style="margin: 0px; padding: 0px;">a*b+(c-d/e)*f#


示例输出

ab*cde/-f*+


提示

 

#include <stdio.h>
#include <stdlib.h>
#define maxsize 10000
typedef struct
{
int *base;
int *top;
}sq;
int init(sq *s)
{
s->base=(int *)malloc(maxsize*sizeof(int));
if(!s->base)exit(-1);
s->top=s->base;
return 1;
}
int push(sq *s,int e)
{
*s->top++=e;
return 1;
}
int pop(sq *s)
{
s->top--;
return 1;
}
int gettop(sq *s)
{
int e;
e=*(s->top-1);
return e;
}
int empty(sq *s)
{
if(s->base==s->top)return 0;
else return 1;
}
int print(sq *s)
{

while(s->base!=s->top)
printf("%c",*(s->base++));
return 1;
}
int main()
{
int i;
sq l1,l2;
char s[100];
scanf("%s",s);
init(&l1);
init(&l2);
for(i=0;s[i]!='#';i++)
{
if(s[i]>='a'&&s[i]<='z')
push(&l1,s[i]);
else if(s[i]=='(')
push(&l2,s[i]);
else if(s[i]==')')
{
while(gettop(&l2)!='(')
{
push(&l1,gettop(&l2));
pop(&l2);
}
pop(&l2);
}
else
{
if(!empty(&l2)||gettop(&l2)=='(')push(&l2,s[i]);
else
{
if((s[i]=='*'&&gettop(&l2)!='*'&&gettop(&l2)!='/')||(s[i]=='/'&&gettop(&l2)!='*'&&gettop(&l2)!='/'))
push(&l2,s[i]);
else
{
push(&l1,gettop(&l2));
pop(&l2);
i--;
}
}
}
}
while(empty(&l2))
{
push(&l1,gettop(&l2));
pop(&l2);
}
print(&l1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: