后缀式求值
示例输入(基本操作数都是一位正整数!)
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步