C语言数据结构与算法 - 表达式解析



像2*(3*4)这样的普通算术表达式,人脑更容易解析,但对于算法来说,解析这样的表达式相当困难。为了简化这个难度,可以使用两步法来解析算术表达式。

  • 将给定的算术表达式转换为后缀表达式。

  • 计算后缀表达式。

中缀表达式

普通的算术表达式遵循中缀表示法,其中运算符位于操作数之间。例如,A+B中,A是第一个操作数,B是第二个操作数,+是作用于这两个操作数的运算符。

后缀表达式

后缀表达式与普通的算术表达式或中缀表达式不同,运算符位于操作数之后。例如,考虑以下例子。

序号 中缀表达式 后缀表达式
1 A+B AB+
2 (A+B)*C AB+C*
3 A*(B+C) ABC+*
4 A/B+C/D AB/CD/+
5 (A+B)*(C+D) AB+CD+*
6 ((A+B)*C)-D AB+C*D-

中缀表达式转后缀表达式

在研究中缀表达式到后缀表达式的转换方法之前,我们需要考虑中缀表达式计算的以下基础知识。

  • 中缀表达式的计算从左到右开始。

  • 记住优先级,例如*的优先级高于+。例如

    • 2+3*4 = 2+12.

    • 2+3*4 = 14.

  • 使用括号覆盖优先级,例如

    • (2+3)*4 = 5*4.

    • (2+3)*4= 20.

现在让我们手动将简单的中缀表达式A+B*C转换为后缀表达式。

序号 读取的字符 已解析的中缀表达式 已生成的 后缀表达式 备注
1 A A A
2 + A+ A
3 B A+B AB
4 * A+B* AB 因为*的优先级高于+,所以不能复制+
5 C A+B*C ABC
6 A+B*C ABC* 因为有两个操作数B和C,所以复制*
7 A+B*C ABC*+ 因为有两个操作数BC和A,所以复制+

现在让我们使用栈将上述中缀表达式A+B*C转换为后缀表达式。

序号 读取的字符 已解析的中缀表达式 已生成的 后缀表达式 栈内容 备注
1 A A A
2 + A+ A + 将+运算符压入栈。
3 B A+B AB +
4 * A+B* AB +* *运算符的优先级高于+。将*运算符压入栈。否则,+将弹出。
5 C A+B*C ABC +*
6 A+B*C ABC* + 没有更多操作数,弹出*运算符。
7 A+B*C ABC*+ 弹出+运算符。

现在让我们看另一个例子,使用栈将中缀表达式A*(B+C)转换为后缀表达式。

序号 读取的字符 已解析的中缀表达式 已生成的 后缀表达式 栈内容 备注
1 A A A
2 * A* A * 将*运算符压入栈。
3 ( A*( A *( 将(压入栈。
4 B A*(B AB *(
5 + A*(B+ AB *(+ 将+压入栈。
6 C A*(B+C ABC *(+
7 ) A*(B+C) ABC+ *( 弹出+运算符。
8 A*(B+C) ABC+ * 弹出(运算符。
9 A*(B+C) ABC+* 弹出其余的运算符。

示例

现在我们将演示使用栈将中缀表达式转换为后缀表达式,然后计算后缀表达式的过程。

#include<stdio.h> 
#include<string.h> 

//char stack
char stack[25]; 
int top=-1; 

void push(char item) { 
   stack[++top]=item; 
}
char pop() { 
   return stack[top--]; 
}

//returns precedence of operators
int precedence(char symbol) { 
   switch(symbol) { 
      case '+': 
      case '-':
         return 2; 
         break; 
      case '*': 
      case '/':
         return 3; 
         break; 
      case '^': 
         return 4; 
         break; 
      case '(': 
      case ')': 
      case '#':
         return 1; 
         break; 
   }
}

//check whether the symbol is operator?
int isOperator(char symbol) { 
   switch(symbol){ 
      case '+': 
      case '-': 
      case '*': 
      case '/': 
      case '^': 
      case '(': 
      case ')':
         return 1; 
      break; 
         default:
         return 0; 
   } 
}
//converts infix expression to postfix
void convert(char infix[],char postfix[]){ 
   int i,symbol,j=0; 
   stack[++top]='#'; 
   for(i=0;i<strlen(infix);i++){ 
      symbol=infix[i]; 
      if(isOperator(symbol)==0){ 
         postfix[j]=symbol; 
         j++; 
      } else {
         if(symbol=='('){			
            push(symbol); 
         } else { 
            if(symbol==')'){ 
               while(stack[top]!='('){ 
                  postfix[j]=pop(); 
                  j++; 
               } 
               pop();//pop out (. 
            } else { 
               if(precedence(symbol)>precedence(stack[top])) {
                  push(symbol); 
               } else { 
                  while(precedence(symbol)<=precedence(stack[top])) { 
                     postfix[j]=pop(); 
                     j++; 
                  } 
                  push(symbol); 
               }
            }
         }
      }
   }
   while(stack[top]!='#') {
      postfix[j]=pop(); 
      j++; 
   }
   postfix[j]='\0';//null terminate string. 
} 
//int stack
int stack_int[25]; 
int top_int=-1; 

void push_int(int item) { 
   stack_int[++top_int]=item; 
} 

char pop_int() { 
   return stack_int[top_int--]; 
} 
//evaluates postfix expression
int evaluate(char *postfix){
   char ch;
   int i=0,operand1,operand2;

   while( (ch=postfix[i++]) != '\0') {
      if(isdigit(ch)){ 
	     push_int(ch-'0'); // Push the operand 
      } else {
         //Operator,pop two  operands 
         operand2=pop_int();
         operand1=pop_int();
         switch(ch) {
            case '+':
               push_int(operand1+operand2);
               break;
            case '-':
               push_int(operand1-operand2);
               break;
            case '*':
               push_int(operand1*operand2);
               break;
            case '/':
               push_int(operand1/operand2);
               break;
         }
      }
   }
   return stack_int[top_int];
}
void main() {
   char infix[25] = "1*(2+3)",postfix[25]; 
   convert(infix,postfix); 
   printf("Infix expression is: %s\n" , infix);
   printf("Postfix expression is: %s\n" , postfix);
   printf("Evaluated expression is: %d\n" , evaluate(postfix));
} 

输出

如果我们编译并运行上述程序,它将产生以下输出:

Infix expression is: 1*(2+3)
Postfix expression is: 123+*
Result is: 5
广告
© . All rights reserved.