后缀式求值

示例输入(基本操作数都是一位正整数!)

59*684/-3*+#

示例输出

57
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 struct node
 5 {
 6     int data;
 7     struct node *next;
 8 };
 9 
10 int main()
11 {
12     struct node *top,*p;
13     int x,y;
14     char c;
15     top=NULL;
16     while((c=getchar())!='#')
17     {
18         if(c>='0'&&c<='9')
19         {
20             p=(struct node*)malloc(sizeof(node));
21             p->data=c-'0';
22             p->next=top;
23             top=p;
24         }
25         else if(c=='+'||c=='-'||c=='*'||c=='/')
26         {
27             y=top->data;
28             top=top->next;
29             x=top->data;
30             if(c=='+')top->data=x+y;
31             else if(c=='-')top->data=x-y;
32             else if(c=='*')top->data=x*y;
33             else if(c=='/')top->data=x/y;
34         }
35     }printf("%d\n",top->data);
36     return 0;
37 }

 

posted @ 2013-08-15 09:54  WangLC  阅读(183)  评论(0编辑  收藏  举报