#include
#include
#include
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define MAXSIZE 100
typedef char DataType;
typedef struct
{
DataType data[MAXSIZE];
int top;
}SqStack;
int InitStack(SqStack &S)
{ //构造一个空栈
S.top= -1;
return 1;
}
int StackEmpty(SqStack S)
//判栈为空栈时返回值为真,反之为假
{ return(S.top==-1? TRUE:FALSE);}
int StackFull(SqStack S)
//判栈为满栈时返回值为真,反之为假
{ return(S.top==MAXSIZE-1?TRUE:FALSE);}
int Push(SqStack &S, DataType e)
{ //将元素e插入到栈中,作为的新栈顶
if(StackFull(S)) return ERROR; //栈满
S.top++; // top加1,栈顶位置上移
S.data[S.top]=e; //数据e存入当前栈顶
return 1;
}
int Pop(SqStack &S,DataType &e)
{//若栈不为空,则删除栈顶元素
if(StackEmpty(S)) return ERROR; //栈空
e=S.data[S.top]; //取出数据放入e所指单元中
S.top--; // top减1,栈顶位置下移
return 1;
}
DataType GetTop(SqStack S)
{//若栈不为空,则取栈顶元素
DataType e;
if(StackEmpty(S)) return ERROR; //栈空
e=S.data[S.top]; //取出数据,top不变
return e;
}
typedef char DataType;
int bracketmatching()
{
SqStack S;
DataType ch;
InitStack(S);
while ((ch=getchar())!='\n')
{
switch (ch) //遇左括号入栈;遇到右括号时,分别检测匹配情况
{
case '(':
case '[':
case '{':
Push(S,ch);break;
case ')':
if (StackEmpty(S)) return FALSE;
else
{ Pop(S,ch); if (ch!= '(') return FALSE; }
break;
case ']':
if (StackEmpty(S)) return FALSE;
else
{ Pop(S,ch); if (ch!= '[') return FALSE; }
break;
case '}':
if (StackEmpty(S)) return FALSE;
else
{ Pop(S,ch); if (ch!='{') return FALSE; }
break;
default:break;
}
}
if (StackEmpty(S)) return TRUE;
else return FALSE;
}
int In(char ch) /*判断字符ch是否为算符*/
{
int i,flag=0;
char op[7]={'+','-','*','/','(',')','='}; /*算符数组*/
for (i=0;i<7;i++)
if(ch==op[i])
{
flag=1;
break;
}
return flag;
}
DataType Operate(DataType a,char theta,DataType b)
{ DataType z;
switch (theta)
{
case '+':z=a+b;break;
case '-':z=a-b;break;
case '*':z=a*b;break;
case '/':z=a/b;break;
}
return(z);
}
char Precede (char a,char b) /*比较两个运算符的优先级*/
{
char z;
if((b=='+')||(b=='-')||(b=='*')||(b=='/')|| (b=='(')||(b==')') ||(b=='='))
switch (a)
{case '+':
case '-':
if((b=='*')||(b=='/')||(b=='(')) z='<';
else z='>'; break;
case '*':
case '/':
if(b=='(') z='<';
else z='>';break;
}
return z;
}
float CaculateExpression(char *str)
{ //算术表达式求值的算符优先算法。设optr和opnd分别为算符栈和操作数栈,
//OP为算符集合
char a,b,theta,x,u;
SqStack optr,opnd;
char *p;
int k;
InitStack(optr);
Push(optr, '=');
InitStack(opnd);
p=str;//c=getchar();
while(*p!='=' ||GetTop(optr)!='=')
{
if(!In(*p))
{
u=0;
k=0;//统计小数位数
while(*p>='0'&&*p<='9'||*p=='.')
{
if(*p!='.')u=u*10+*p-'0';
if(*p=='.'||k>0) k++;
p++;
}
if(k>0) u=u/pow(10,k-1);
Push(opnd,u);
} //不是算符则进栈
else
switch(Precede(GetTop(optr),*p))
{ case '<' : //栈顶算符优先级低
Push(optr,*p);
p++;//c=getchar();
break;
case '=' : //优先级相同,脱去括号并读入下一字符
Pop(optr,x);
p++;//c=getchar();
break;
case '>' : //栈顶算符优先级高,退栈并将运算结果入栈
Pop(optr,theta);
Pop(opnd,b);Pop(opnd,a);
Push(opnd,Operate(a,theta,b));
break;
}}
return GetTop(opnd);
}
int JudgeExpression(char *str)
{
char*p;
p=str;
while(*p!='\0') p++;
p--;
if(*p!='=')
{
printf("表达式要以等号结束!");
return 0;
}
return 1;
}
int main()
{
char str[81];
printf("请输入表达式:");
scanf("%s",str);
if(JudgeExpression(str))
printf("%s%f\n",str,CaculateExpression(str));
return 0;
}