1、中缀(infix)表达式(expression)转换为后缀(postfix)表达式:(注:为了方便起见,表达式对象的输入输出均用空格分开)

  首先需要从头到尾读取中缀表达式中的每一个对象,建立一个堆栈

  (1)遇到操作数,直接输出;

  (2)遇到运算符,当该运算符大于栈顶运算符的优先级时,将该运算符压入栈中;如果该运算符的优先级小于或等于栈顶元素的优先级时,将栈顶元素弹出并输出,并比较新的

     栈顶运算符,知道该运算符大于栈顶运算符,并将该运算符压入栈中。

  (3)遇到左括号,将其压入栈中;

  (4)遇到右括号,将括号内的运算符依次弹出并输出,直到遇到左括号(左括号也出栈,但不输出)。

  (5)若中缀表达式中的对象处理完毕,则将栈中剩余的运算符弹出并输出。

代码如下:

  

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<stdbool.h>
  4 #include<ctype.h>
  5 #define MAXOP 100
  6 typedef struct SNode{
  7     char *Data;
  8     int Top;
  9     int Maxsize;
 10 }Stack;
 11 
 12 Stack *CreateStack(int Maxsize)
 13 {
 14     Stack *S;
 15     S=(Stack *)malloc(sizeof(struct SNode));
 16     S->Top=-1;
 17     S->Data=(char *)malloc(Maxsize * sizeof(char));
 18     S->Maxsize=Maxsize;
 19     return S;
 20 }
 21 
 22 bool IsFull(Stack *S)
 23 {
 24     return (S->Top==S->Maxsize-1);
 25 }
 26 
 27 void Push(Stack *S,char op)
 28 {
 29     if(!IsFull(S))
 30     {
 31         S->Data[++(S->Top)]=op;
 32     }
 33 }
 34 
 35 bool IsEmpty(Stack *S)
 36 {
 37     return (S->Top==-1);
 38 }
 39 
 40 char Pop(Stack *S)
 41 {
 42     char op;
 43     if (!IsEmpty(S))
 44     {
 45         op=S->Data[(S->Top)--];
 46     }
 47     return op;
 48 }                                     //以上是建立栈,及栈的基本操作
 49 
 50 /* 取出栈顶元素的函数 */
 51 char Top(Stack *S)
 52 {
 53     char op;
 54     if (!IsEmpty(S))
 55     {
 56         op=S->Data[(S->Top)];
 57     }
 58     return op;
 59 }
 60 
 61 typedef enum {num,opr,end}Type;
 62 
 63 /* 扫描中缀表达式中的每个对象,将其存在str数组中 。start传进去的是地址。str数组在每次函数调用的过程中其值会不断的变化*/
 64 Type GetOp(char *Expr,int *start,char *str)
 65 {
 66     int i=0;
 67     while ((str[0]=Expr[(*start)++])==' '); //跳过表达式对象前的空格
 68     while (str[i]!=' ' && str[i]!='\0') //遇到空格或结尾标识符,结束一个对象的获取
 69     {
 70         str[++i]=Expr[(*start)++];   //表达式存在str中
 71     }
 72     if (str[i]=='\0')(*start)--;  //start指向表达式末尾结束符'\0'
 73     str[i]='\0';    //结束一个对象的获取
 74 
 75     if (i==0) return end;
 76     else if (isdigit(str[0])||isdigit(str[1])) return num;  //考虑到类似于".3"这样的对象
 77     else return opr;
 78 }
 79 /* 优先级函数 */
 80 int Prior(char op)
 81 {
 82     switch (op)
 83     {
 84         case '*':
 85         case '/': return 2;
 86         case '+':
 87         case '-': return 1;
 88         case '(': return 0;  //当左括号压入栈中后,其优先级变得最低
 89     }
 90 }
 91 /*核心,中缀表达式转换为后缀表达式*/
 92 void PrintPostfixExp(char *Expr)
 93 {
 94     int start=0;
 95     Type T;
 96     char str[MAXOP];
 97     char op1,op2;
 98     Stack *S;
 99     S=CreateStack(MAXOP);
100     while((T=GetOp(Expr,&start,str))!=end)
101     {
102         if (T==num)printf("%s ",str);  //数字直接输出
103         else
104         {
105             switch(str[0])
106             {
107                 case '(':Push(S,str[0]);break; //左括号直接压入栈中
108                 case ')':
109                 {
110                     while(Top(S)!='(')printf("%c ",Pop(S)); //括号内的操作符弹出并输出
111                     Pop(S); //弹出左括号
112                     break;
113                 }
114                 case '+':
115                 case '-':
116                 case '*':
117                 case '/':
118                 {
119                     if (IsEmpty(S)){Push(S,str[0]);break;}  //栈是空的时候,直接压入栈中
120                     else
121                     {
122                         op1=Top(S);
123                         while (Prior(str[0])<=Prior(op1)) //依次将该对象和栈中的元素进行比较,直到栈不为空并且该操作符的优先级大于栈顶操作符优先级
124                          {printf("%c ",Pop(S));
125                             if (IsEmpty(S))op1=Top(S); //栈空时,退出循环
126                             else break;
127                         }
128                         Push(S,str[0]); //将该运算符压栈
129                     }
130                     break;
131                 }
132 
133             }
134         }
135     }
136     while(!IsEmpty(S))printf("%c ",Pop(S)); //将栈中剩余的运算符弹出并输出
137 }
138 
139 int main()
140 {
141     char Expr[MAXOP];
142     gets(Expr);
143     PrintPostfixExp(Expr);
144     return 0;
145 }
View Code

相关文章: