/*************************************************************
中缀式求值 实现文件
更新于2020年4月27日
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “Expression.h”
/子函数/
/操作数栈的基本操作/
void InitStack1(SqStack1 &s)//栈的初始化
{ s.top=-1; }
int StackEmpty1(SqStack1 s)//判栈空
{
if(s.top==-1) return 1;
else return 0;
}
void Push1(SqStack1 &s, double x)//:将元素x入栈
{
if(s.topMAXSIZE-1)//判栈满
{ printf(“Stack is full\n”);return; }
s.top++; s.elem[s.top]=x;
}
void Pop1(SqStack1 &s, double &x)//将栈顶元素出栈
{
if(s.top-1)//判栈空
{ printf(“Stack is empty1\n”);return; }
x=s.elem[s.top]; s.top–;
}
void GetTop1(SqStack1 s, double &x)//取栈顶
{
if(s.top==-1)
{ printf(“Stack is empty\n”);return; }
x=s.elem[s.top];
}
/运算符栈的基本操作/
void InitStack2(SqStack2 &s)//栈的初始化
{ s.top=-1; }
int StackEmpty2(SqStack2 s)//判栈空
{
if(s.top==-1) return 1;
else return 0;
}
void Push2(SqStack2 &s, char x)//:将元素x入栈
{
if(s.topMAXSIZE-1)//判栈满
{ printf(“Stack is full\n”);return; }
s.top++; s.elem[s.top]=x;
}
void Pop2(SqStack2 &s, char &x)//将栈顶元素出栈
{
if(s.top-1)//判栈空
{ printf(“Stack is empty2\n”);return; }
x=s.elem[s.top]; s.top–;
}
void GetTop2(SqStack2 s, char &x)//取栈顶
{
if(s.top==-1)
{ printf(“Stack is empty\n”);return; }
x=s.elem[s.top];
}
/其它子函数/
double Operate(double a, char theta, double b)//求两数a和b作θ运算的结果
{
switch(theta)
{
case ‘+’: return(a+b);
case ‘-’: return(a-b);
case '’: return(ab);
case ‘/’: return(a/b);
}
}
char Precede(char theta1, char theta2)//比较两个运算符θ1和θ2的优先关系,返回’>’、’<‘或’=’。
{
// 请在这里补充代码,完成本关任务
/********** Begin /
int i,j;
switch(theta1){
case ‘#’:i = -1;break;
case ‘(’:i = 3;break;
case ‘)’:i = 0;break;
case '’:i = 2;break;
case ‘/’:i = 2;break;
case ‘+’:i = 1;break;
case ‘-’:i = 1;break;
// default :return ERROR;
}
switch(theta2){
case ‘#’:j = -1;break;
case ‘(’:j = 3;break;
case ‘)’:j = 0;break;
case '’:j = 2;break;
case ‘/’:j = 2;break;
case ‘+’:j = 1;break;
case ‘-’:j = 1;break;
// default :return ERROR;
}
if(i == j) return ‘=’;
if(i < j) return ‘<’;
if(i > j) return ‘>’;
/*** End **********/
}
double Evaluation(char exp[80])//中缀式求值
{
SqStack1 opnd;SqStack2 optr; char s[80],x,top,c,d;double sum,a,b,result; int i,j=0,n;
InitStack1(opnd);//构造栈opnd存放操作数
InitStack2(optr);//构造栈optr存放运算符
Push2(optr,’#’);
GetTop2(optr, top);//取出栈顶运算符,用top保存
i=0;
Push1(opnd,i);
while(exp[i]!=’#’||top!=’#’)
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if((exp[i]<=‘9’&&exp[i]>=‘0’)||exp[i] == ‘.’)
{
for(j = 0;(exp[i]<=‘9’&&exp[i]>=‘0’)||exp[i] == ‘.’;j++)
{
s[j] = exp[i];
i++;
s[j+1] = ‘\0’;
}
b = atof(s);
if(exp[i-j-1] == ‘-’)
{
if(exp[i-j-2] == ‘(’)
{
b = -b;
Pop2(optr,d);
}
}
Push1(opnd,b);
}
else
{
//printf(“exp = %c\n”,exp[i]);
c = exp[i];
GetTop2(optr,x);
if(x ==’#’)
{
Push2(optr,c);
//printf(“1\n”);
}
for (;(x != ‘#’&& c!= ‘#’)😉
{
//printf(“2\n”);
//printf(“x = %c\nc = %c\n”,x,c);
//printf(“判断 = %c\n”,Precede(x,c));
if(Precede(x,c) == ‘<’||x == ‘(’)
{
Push2(optr,c);
break;
}
else
{
Pop2(optr,x);
Pop1(opnd,b);
Pop1(opnd,a);
//printf(“a = %lf\n”,a);
//printf(“x = %c\n”,x);
//printf(“b = %lf\n”,b);
sum = Operate(a,x,b);
Push1(opnd,sum);
//printf(“sum[%d} = %lf\n”,i,sum );
//printf(“x1 = %c\nc1 = %c\n”,x,c);
if (c != ‘)’)
{
Push2(optr,c);
}
else//消去括号
{
Pop2(optr,x);
GetTop2(optr,x);
//printf("%c\n",x);
if(x == ‘(’)
{
//printf(“x\n”);
Pop2(optr,x);
Pop2(optr,x);
GetTop2(optr,x);
//printf(“x2= %c\n”,x);
}
}
GetTop2(optr,c);
if(c != '#')
{
Pop2(optr,c);
GetTop2(optr,x);
if(x == '#')
{
Push2(optr,c);
break;
}
}
}
}//for
// Push2(optr,c);
i++;
// int j=0;
// j++;
// if(j == 5)break;
}//压栈else
//printf(“expi+1 = %c\n”,exp[i]);
if(exp[i] == ‘#’)
{
Pop2(optr,x);
Pop1(opnd,b);
Pop1(opnd,a);
//printf(“11a = %lf\n”,a);
//printf(“x = %c\n”,x);
//printf(“b = %lf\n”,b);
sum = Operate(a,x,b);
Push1(opnd,sum);
//printf(“sum[%d} = %lf\n”,i,sum );
}
/********** End **********/
}
Pop1(opnd,result);
return(result);
}