一个简单的计算器,除法运算功能不全,例如1024/2不能正确算出,有高手帮帮忙!
#include "stdio.h"#include "stdlib.h"
#include "math.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
char * base;
char * top;
int stacksize;
}SqStack;
enum Status
{
OK,
ERROR,
};
void main()
{
Status InitStack(SqStack &S);
Status GetTop(SqStack S,char &e);
Status Push(SqStack &S,char e);
Status Pop(SqStack &S,char &e);
char Precede(char a,char b);
int Operate(int a,char b,int c);
Status In(char a);
int change(char &a);
SqStack OPTR,OPND;
printf("\n\n\n\n\n\t***********本程序的功能为:用顺序栈实现整型算术表达式的求值**********\n\n");
printf("\t运算符说明如下所示 :\n");
printf("\t\t____________________________________________________________ \t\t\n\n");
printf("\t\t\t加法\t减法\t乘法\t除法\t乘方\t负号\t\t\n\n");
printf("\t\t\t'+'\t'-'\t'*'\t'/'\t'^'\t'~'\t\t\n");
printf("\t\t___________________________________________________________\t\t");
getchar();
system("cls"); //切换界面
int e,i=0;
char a,b,c[30],d,f,x,theta;
InitStack(OPTR);
Push(OPTR,'#');
InitStack(OPND);
printf("\t\t请输入表达式并以'#'结束:");
gets(c);
printf("\t\t操作过程如下:\n");
printf("_______________________________________________________________________________\n\n");
printf("步骤 OPTR栈 OPND栈 主要操作 \n");
printf("_______________________________________________________________________________\n");
GetTop(OPTR,d);
while(c[i]!='#'||d!='#')
{
if(In(c[i])==ERROR)
{
e=change(c[i]);
while(In(c[++i])==ERROR)/*此处实现的功能为当输入的数字为多位数时*/
{
e*=10;
e+=c[i]-'0';
}
Push(OPND,e);
e=0;
}
else
switch(Precede(d,c[i]))
{
case'<':
Push(OPTR,c[i]);
i++;
break;
case'=':
Pop(OPTR,x);
i++;
break;
case'>':
Pop(OPTR,theta);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
GetTop(OPTR,d);
}
GetTop(OPND,f);
printf("\n*********************************************************\n");
printf("结果为:%d\n",f);
printf("\n*********************************************************\n");
printf("\nInput '' to quit and ENTER run again:");
}
Status InitStack(SqStack &S)
{
S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base)
exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S,char &e)
{
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}
Status Push(SqStack &S,char e)
{
if(S.top-S.base==S.stacksize)
{
S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,char &e)
{
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
char Precede(char a,char b)
{
char c,d,e;
c='>';
d='=';
e='<';
if(((a=='+'||a=='-')&&(b=='+'||b=='-'||b==')'||b=='#'))||((a=='*'||a=='/')&&(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#')))
return (c);
if((a=='('&&b==')')||(a=='#'&&b=='#'))
return (d);
else
return (e);
}
int Operate(int a,char b,int c)
{
switch(b)
{
case'+': return (a+c);
case'-': return (a-c);
case'*': return (a*c);
case'/': if(c==0)
printf("分母为零,error");
else
return (a/c);
default: printf("Operator error!\n");
exit(0);
}
}
Status In(char a)
{
if(a<'0'||a>'9')
return OK;
else
return ERROR;
}
int change(char &a)
{
if(a!='+'||'-'||'*'||'/'||'('||')'||'#')
return (a-'0');
}