(盐城工学院数据结构课程设计)栈的应用表达式求值 下载本文

/*******

Whereat 说明:

Whereat 记录了操作数和运算符号的位置,用A和B区分。A = operand, B = operator. (例如 1+2转换成12+,在whereat中的形式应该是 AAB) OpHolder说明:

Char类型的堆栈Opholder用来保存运算符号。 ******/

/******将中缀表带式转换为后缀表达式******/

void ConvertToPost(FILE *In, Stack Whereat, FILE *Temp) {

Stack OpHolder; char holder; char lastseen;

int digitcounter = 0; /*操作数的计数器*/ OpHolder = malloc(sizeof(struct Node)); /*初始化*/ OpHolder->Next = NULL; holder=getc(In);

lastseen = '@'; /*用来防止输入格式错误,例如两个小数点*/ putc(' ',Temp);

while ((holder !='\\n') && (holder != EOF)){ if (holder == ' '){ digitcounter = 0; } else if ( IsOperator(holder) == -1){ /*如果holder不是操作数或运算符号*/ PrintError = 1; }

else if (IsOperator(holder)==0){ if ((lastseen == holder) && (lastseen == '.')){ /*错误处理*/ PrintError = 1; } else lastseen = holder; if (digitcounter == 0){ Push('A',Whereat); /*进栈*/ digitcounter++; /*计数器加一*/ putc(' ',Temp);

} putc(holder,Temp); } else{ digitcounter = 0; if ((lastseen == holder) && (lastseen != '(') && (lastseen != ')')) /*\情况特殊对待*/ PrintError = 1; else lastseen = holder; 6))

if(IsEmpty(OpHolder)==1){ /*当OpHolder为空*/ Push(holder,OpHolder); }

else if(OperatorValue(Top(OpHolder)) == 6){ /*OpHolder是\的情况*/ if(OperatorValue(holder)==5) Pop(OpHolder); else Push(holder,OpHolder); }

else if(OperatorValue(holder) == 6){ Push(holder,OpHolder); }

else if(OperatorValue(holder) == 5){ /* OpHolder是\的情况*/ while ((IsEmpty(OpHolder) != 1) && (OperatorValue(Top(OpHolder)) != {

putc(' ',Temp);

Push('B',Whereat);

putc(Top(OpHolder),Temp); Pop(OpHolder); } if (IsEmpty(OpHolder) == 1){ /*错误处理,括号不匹配*/ PrintError = 1; } else Pop(OpHolder); }

else if((OperatorValue(holder) == OperatorValue(Top(OpHolder)))

&& (OperatorValue(holder) == 3)){ /*幂运算情况*/ Push(holder,OpHolder); }

else if((OperatorValue(holder) < OperatorValue(Top(OpHolder))) && OperatorValue(Top(OpHolder)) == 3){ /*幂运算情况*/ putc(' ',Temp); Push('B',Whereat); putc(Top(OpHolder),Temp); Pop(OpHolder); while((IsEmpty(OpHolder) != 1) && (OperatorValue(Top(OpHolder)) }

{ Push('B',Whereat); putc(' ',Temp); putc(Top(OpHolder),Temp); Pop(OpHolder); }

Push(holder,OpHolder);

== 3))

/******

如果当前运算符号的优先级小于或者等于堆栈中的运算符号的优先级,则将其放入temp中,并且将堆栈中的运算符号出栈,放入temp中,直到堆栈为空或者优先级小于堆栈顶元素的优先级 ******/ else if(OperatorValue(Top(OpHolder)) >= OperatorValue(holder)){ while((IsEmpty(OpHolder) != 1) && (OperatorValue(Top(OpHolder)) >= OperatorValue(holder))

&& (OperatorValue(Top(OpHolder))!=6)) { putc(' ',Temp); putc(Top(OpHolder),Temp); Push('B',Whereat); Pop(OpHolder); } Push(holder,OpHolder); }

else if(OperatorValue(Top(OpHolder)) < OperatorValue(holder)){

/******

如果当前运算符号的优先级大于堆栈中的运算符号的优先级,则将其压入堆栈中 ******/ Push(holder,OpHolder); } } holder=getc(In);

} /* While循环结束*/ while(IsEmpty(OpHolder)!=1){ /******

最后如果堆栈中还有运算符号,则一并放到temp中 ******/ Push('B',Whereat); putc(' ',Temp); putc(Top(OpHolder),Temp); Pop(OpHolder); }

MakeEmpty(OpHolder); free(OpHolder);

}

/******判断类型,1为运算符号,0为操作数,-1为错误******/ int IsOperator(char ToCompare) {

if (ToCompare == '(' || ToCompare == ')'|| ToCompare == '+' || ToCompare == '-' || ToCompare == '*'|| ToCompare == '/' || ToCompare == '^'|| ToCompare == '%') { return 1; }

else if (ToCompare == '1' || ToCompare == '2'|| ToCompare == '3' || ToCompare == '4' || ToCompare == '5'|| ToCompare == '6' || ToCompare == '7'|| ToCompare == '8' || ToCompare == '9' || ToCompare == '0'|| ToCompare == '.') { return 0; } else{ return -1; }