#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);}
雅思考试 托福考试 留学攻略 留学院校库