C语言编写逆波兰计算器
发布时间: 2022-10-01 00:09:01
#include<stdio.h>#include<stdbool.h>#include<stdlib.h> #defineSTACK_SIZE 20 intmake_empty(void);boolis_empty(void);boolis_full(void);voidpush(char );voidpop(char );voidstack_overflow(void);voidstack_underflow(void); charcontents[STACK_SIZE]= {0},top; intmain(int argc, char *argv[]){ char ch='1'; while(ch!='q'){ make_empty(); printf("Enter an RPNexpression:"); do{ scanf(" %c",&ch); if(ch>='1'&&ch<='9') push(ch); else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){ top--;pop(ch); } else if(ch=='='){ if((top-1)==0) pop(ch); else printf("Nunber notused!"); break; } else if(ch=='\n'); else{ ch='q';break; } } while(ch!='\n'); } return 0;} intmake_empty(void){ /* return top=0;} boolis_empty(void){ return top==0;} boolis_full(void){ return top==STACK_SIZE;} voidpush(char ch){ if(is_full()) stack_overflow(); else contents[top++]=ch-'0';} voidpop(char ch){ if(is_empty()) stack_underflow(); else switch(ch){ case'+':contents[top-1]+=contents[top];break; case '-':contents[top-1]-=contents[top];break; case'*':contents[top-1]*=contents[top];break; case'/':contents[top-1]/=contents[top];break; case '=':printf("Value;n",(int)contents[0]);break; }} voidstack_overflow(void){ printf("expression is toocomplex!"); exit(EXIT_FAILURE);} voidstack_underflow(void){ printf("Not enough operands inexpression!"); exit(EXIT_FAILURE);}
温馨提示:
本文【C语言编写逆波兰计算器】由作者教培参考提供。该文观点仅代表作者本人,学分高考系信息发布平台,仅提供信息存储空间服务,若存在侵权问题,请及时联系管理员或作者进行删除。