#include
#include
#include
#include
#include
#define MAX 100 /*stac的容量*/
#define NUM '0' /*数字类型标识符*/
#define SMAX 200 /*缓冲池的容量*/
#define AGAIN -2 /*接着再接受输入的标识符*/
#define SIN -3 /*sin函数的标识符*/
#define COS -4 /*cos函数的标识符*/
#define EXP -5 /*exp函数的标识符*/
#define POW -6 /*pow函数的标识符*/
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("Welcome to my calculator!\n ");
printf("Illustration:\n");
printf("Inverse Poland type calculator:\n");
printf("This calculator includes the 'sin','cos','exp','pow':");
printf("Please input in the right order!\n");
printf("input 'flash'meaning flash the stack:\n");
while((type=getst())!=EOF) /*循环判断输入的情况*/
{ switch(type)
{
case SIN: /*求正弦*/
midans=sin(pop());
push(midans); /*回收结果*/
printf("%f\n",midans);
getch();
break;
case COS:
midans=cos(pop());
push(midans);
printf("%f\n",midans);
getch();
break;
case EXP:
midans=exp(pop());
push(midans);
printf("%f\n",midans);
getch();
break;
case POW:
mid=pop();
midans=pow(mid,pop());
push(midans);
printf("%f\n",midans);
getch();
break;
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
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,""))
{
return '\n';
}
if(!strcmp(string,"flash"))
{
while(k
{
stac[k]=0;
k++;
}
return AGAIN;
}
if(!strcmp(string,"sin"))
return SIN;
if(!strcmp(string,"cos"))
return COS;
if(!strcmp(string,"exp"))
return EXP;
if(!strcmp(string,"pow"))
return POW;
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()))
;
if(!(poi==0))
{
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() /*得到一个字符*/
{
if(poi>0)
return suf[--poi];
else
if(poi==0)
return AGAIN;
}
void unget(int c) /*若字符类型不符合,再放进缓冲池*/
{
if(poi>=SMAX)
{
printf("ERROR!The memory is limited !\n");
getch();
exit(0);
}
else
suf[poi++]=c;
}
/*************我就是程序;程序就是我!******************/