逆波兰式计算器
#include<stdio.h>#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAX 100
#define NUM '0'
#define SMAX 200
#define AGAIN -2
double pop(void);
void push(double);
int getst();
int get(void);
void unget(int);
double stac[MAX];
char suf[SMAX];
char string[SMAX];
char s[MAX];
int point,poi;
void main()
{
int type;
double mid,midans;
point=poi=0;
printf("input 'flash'meaning flash the stack:\n");
while((type=getst())!=EOF)
{
switch(type)
{
case NUM:
push(atof(s));
break;
case '+': midans=pop()+pop();
push(midans);
printf("%f\n",midans);
getch();
break;
case '*':
midans=pop()*pop();
push(midans);
printf("%f\n",midans);
getch();
break;
case '-':
mid=pop();
midans=pop()-mid;
push(midans);
printf("%f\n",midans);
getch();
break;
case '/':
mid=pop();
if(mid==0)
{
printf("ERROR!the location of the 0 is wrong!\n");
getch();
break;
}
else
{
midans=pop()/mid;
push(midans);
printf("%f\n",midans);
getch();
}
break;
case '%' :
mid=pop();
if(mid==0)
{
printf("ERROR!the location of the 0 is wrong!\n");
getch();
break;
}
else
{
midans=(int)pop()%(int)mid;
push(midans);
printf("%d\n",midans);
getch();
}
break;
case '\n':
printf("%f\n",pop());
printf("Please input :\n");
getch();
break;
case AGAIN :
break;
default :
printf("%d\n",type);
printf("ERROR!The get is wrong !\n");
break;
}
}
getch();
}
double pop(void)
{
if(point<=0)
{
printf("The array is empty!\n");
return 0;
}
else
return stac[--point];
}
void push(double f)
{
if(point<MAX)
stac[point++]=f;
else
{
printf("The memory is limited!\n");
}
}
int getst()
{
int type,i,c,k,length,m;
i=1;
k=0;
if(poi<1)
{
aa: gets(string);
if(!strcmp(string,"flash"))
{
while(k<MAX)
{
stac[k]=0;
k++;
}
return AGAIN;
}
poi=length=strlen(string);
if(length>SMAX-1)
{
printf("the memory of the suf is limited!\n");
printf("Please input again!\n");
goto aa;
}
for(m=0,length--;length>=0;length--,m++)
suf[m]=string[length];
suf[m]='\0';
}
for(;(s[0]=c=get())==' '||c=='\t';)
;
s[1]='\0';
if(!isdigit(c)&&c!='.'&&c!='-')
return c;
if(isdigit(c)||'-'==c)
{
while(isdigit(s[i++]=c=get()))
;
if(!isdigit(c)&&2==i&&s[0]=='-')
{
unget(c);
return '-';
}
if(c=='.')
{
while(isdigit(s[i++]=c=get()))
;
}
unget(c);
s[--i]='\0';
return NUM;
}
else if(c=='\n')
return '\n';
else
{
printf("ERROR!");
printf("The char cant't be recoganized!\n");
}
}
int get()
{
char c;
if(poi>0)
return suf[--poi];
else
if((c=getchar())=='\t')
{
printf("Please input !not the '\t'");
return AGAIN;
}
}
void unget(int c)
{
if(poi>=SMAX)
{
printf("ERROR!The memory is limited !\n");
getch();
exit(0);
}
else
suf[poi++]=c;
}
大家看哈啊:一起努力啊!