逆波兰表达式_数据结构作业
本文适合中级读者阅读 作者:郭郭 来源:vcLover.com 日期: 2008/01/31 浏览:
逆波兰表达式 rpn(Reverse Polish Notation)
逆波兰表达式,它的语法规定,表达式必须以逆波兰表达式的方式给出。逆波兰表达式又叫做后缀表达式。这个知识点在数据结构和编译原理这两门课程中都有介绍,下面是一些例子:
正常的表达式 逆波兰表达式
a+b a,b,+
a+(b-c) a,b,c,-,+
a+(b-c)*d a,d,b,c,-,*,+
a=1+3 a=1,3 +
http=(smtp+http+telnet)/1024 写成什么呢?
http=smtp,http,telnet,+,+,1024,/
代码运行结果如下:

折叠 C/C++ Code
- //V*C爱好者vc*lover.com
- //www.vcLover.com
- #include<stdio.h>
- #include<math.h>
- #include<conio.h>
- #include<stdlib.h>
- #define maxsize 80
- #define datatype float
- typedef struct
- {
- datatype a[maxsize];
- int top;
- }stack;
- void initiate(stack * s)
- {
- s->top=0;
- }
- /* 为便于调用时进行判断,将堆栈空设为0,不空设为1*/
- int empty(stack s)
- {
- if(s.top<=0)
- return 0;
- else
- return 1;
- }
- int full(stack s) /* 满设为0 不满设为1 */
- {
- if(s.top>=maxsize)
- return 0;
- else return 1;
- }
- datatype top(stack s)
- {
- if(empty(s))
- return s.a[s.top-1];
- else exit(1);
- }
- void push(stack * s,datatype i)
- {
- s->a[s->top]=i;
- s->top++;
- }
- datatype pop(stack * s)
- {
- datatype d;
- d=s->a[s->top-1];
- s->top--;
- return d;
- }
- void disply(void)
- {
- printf("Press any key to continue...\n");
- while(getchar()==NULL);
- }
- void main(void)
- {
- char c[80];
- int i,j,tag,c0;
- float c1,c2;
- stack s,num;
- initiate(&s);
- initiate(&num);
- printf("输入要转化的表达式:\n");
- gets(c);
- for(i=0;c[i]!='\0';i++)
- {
- if(c[i]>=48&&c[i]<=57)
- if(tag==1)
- (&num,c[i]);
- else
- {
- push(&num,' '); /* seperate two integers : xyz and abc */
- push(&num,c[i]);
- tag=1;
- }
- else if(c[i]=='(')
- push(&s,c[i]);
- else if(c[i]=='+')
- {
- while(empty(s)&&(top(s)=='*'||top(s)=='/'||top(s)=='^'||top(s)=='-'))
- {
- push(&num,pop(&s));
- }
- push(&s,c[i]);
- tag=0;
- }
- else if(c[i]=='-')
- {
- while(empty(s)&&(top(s)=='*'||top(s)=='/'||top(s)=='^'||top(s)=='+'))
- {
- push(&num,pop(&s));
- }
- push(&s,c[i]);
- tag=0;
- }
- else if(c[i]=='*')
- {
- while(empty(s)&&(top(s)=='^'||top(s)=='/'))
- push(&num,pop(&s));
- push(&s,c[i]);
- tag=0;
- }
- else if(c[i]=='/')
- {
- while(empty(s)&&(top(s)=='^'||top(s)=='*'))
- push(&num,pop(&s));
- push(&s,c[i]);
- tag=0;
- }
- else if(c[i]=='^')
- {
- push(&s,c[i]);
- tag=0;
- }
- else if(c[i]==')')
- {
- while(top(s)!='(')
- push(&num,pop(&s));
- pop(&s); /* delete ( */
- }
- } /* for */
- while(empty(num))
- push(&s,pop(&num)); /* invert order to output the postfix */
- printf("波兰表达式: ");
- tag=0;
- while(empty(s))
- {
- c0=(int)pop(&s);
- printf("%c",c0);
- if(c0>='0'&&c0<='9')
- if(tag==1)
- push(&num,10*pop(&num)+(c0-48));
- else
- {
- push(&num,(c0-48));
- tag=1;
- }
- else
- {
- switch (c0)
- {
- case ' ': break;
- case '+': c1=pop(&num);c2=pop(&num);push(&num,c2+c1);break;
- case '-': c1=pop(&num);c2=pop(&num);push(&num,c2-c1);break;
- case '*': c1=pop(&num);c2=pop(&num);push(&num,c2*c1);break;
- case '/': c1=pop(&num);c2=pop(&num);push(&num,c2/c1);break;
- case '^': c1=pop(&num);c2=pop(&num);push(&num,pow(c2,c1));break;
- default:break;
- }
- tag=0;
- }
- }
- printf("\n结果:%.0f\n",pop(&num));
- }
上一篇:排序算法比较_冒泡,插入,快速,希尔排序算法 下一篇:单链表实现一元多项式相加
相关文章
查看全部评论相关评论
What's New?
What's Hot?
Google Adsense!