本人写了一个程序:大家给点意见
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct SqStack_c
{
char *base;
char *top;
int stacksize;
}SqStack_c;
int Empty_c(SqStack_c *S)
{
return((S->top==S->base)? 1:0);
}
char InitStack_char(SqStack_c *S)
{
S->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S->base)
return 0;
else
{
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
}
char Push_char(SqStack_c *S, char e)
{
if(S->top - S->base >= S->stacksize)
{
S->base=(char *)realloc(S->base, (S->stacksize+STACKINCREMENT) * sizeof(char));
if(S->base!=NULL)
return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e ;
return 1;
}
char Pop_char(SqStack_c *S,char e)
{
Empty_c(S);/*有何用处?*/
e=*--S->top;
return e;
}
char Top_char(SqStack_c *S)
{
char e;
Empty_c(S);
e=*(S->top-1);
return e;
}
typedef struct SqStack_f
{
float *base;
float *top;
int stacksize;
}SqStack_f;
int Empty_f(SqStack_f *S)
{
return((S->top==S->base)? 1:0);
}
float InitStack_float(SqStack_f *S)
{
S->base=(float *)malloc(STACK_INIT_SIZE*sizeof(float));
if(!S->base)
return 0;
else
{
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
}
float Push_float(SqStack_f *S, float e)
{
if(S->top - S->base >= S->stacksize)
{
S->base=(float *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(float));
if(S->base!=NULL)
return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return 1;
}
float Pop_float(SqStack_f *S,float e)
{
Empty_f(S);
e=*--S->top;
return e;
}
float Top_float(SqStack_f *S)
{
float e;
Empty_f(S);
e=*(S->top-1);
return e;
}
float char_to_float(char c)
{
return ((float)c-48);
}
float Operate(float a,char R,float b)
{
switch(R)
{
case '+': return a+b;/*无break语句?*/
case '-': return a-b;
case '*': return a*b;
case '/': if(b==0)
{
printf("The divisor is 0!");
exit(-1);
}
else
return a/b;
default : return 0;
}
}
char Precede(char ch,char c)
{
if(ch=='+'||ch=='-')
{
if(c=='+'||c=='-'||c==')'||c=='=')
return '>';
else if(c=='*'||c=='/'||c=='(')
return '<';
}
else if(ch=='*'||ch=='/')
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='=')
return '>';
else if(c=='(')
return '<';
}
else if(ch=='(')
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
else if(c==')')
return '=';
else if(c=='=')
return ' ';
}
else if(ch==')')
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='=')
return '>';
else if(c=='(')
return ' ';
}
else if(ch=='=')
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
else if(c=='=')
return '=';
else if(c==')')
return ' ';
}
}
char In(char c)
{
if(c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0'||c=='.')
return 1;
else
return 0;
}
float EvalueteExpression()
{
SqStack_c *OPTR;
SqStack_f *OPND;
char c,R,x;
float a,b;
int i;
clrscr();
InitStack_char(OPTR);
Push_char(OPTR,'=');
InitStack_float(OPND);
printf("please input the expression:\n");
c=getchar();
while(c!='='||Top_char(OPTR)!='=')
{
if(In(c))
{
Push_float(OPND,char_to_float(c));
c=getchar();
while(In(c))
{
if(c=='.')/*处理小数点!*/
{
Push_float(OPND,Pop_float(OPND,a));
c=getchar();
i=1;
while(In(c))
{
Push_float(OPND,Pop_float(OPND,a)+(char_to_float(c)/(10.0*i)));
i=i*10;
c=getchar();
}
}
else
{
Push_float(OPND,Pop_float(OPND,a)*10+char_to_float(c));
c=getchar();
}
}
}
else
switch(Precede(Top_char(OPTR),c))
{
case'<':Push_char(OPTR,c);
c=getchar();
break;
case'=':Pop_char(OPTR,x);
c=getchar();
break;
case'>':R=Pop_char(OPTR,R);
a=Pop_float(OPND,a);
b=Pop_float(OPND,b);
Push_float(OPND,Operate(b,R,a));
break;
}
}
return Top_float(OPND);
}
main()
{
float y;
y=EvalueteExpression();
printf("%f",y);
}