C語言編寫逆波蘭計算器


推選答案#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);
C語言編寫逆波蘭計算器

}
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();
C語言編寫逆波蘭計算器

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);
}
網(wǎng)上報名
  • 姓名:
  • 專業(yè):
  • 層次: ??分數(shù):
  • 電話:
  • QQ/微信:
  • 地址:

文中圖片素材來源網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系644062549@qq.com刪除

轉(zhuǎn)載注明出處:http://www.tengyi66.com