postfix.c

来自「《数据结构》教材源程序,可以让你轻松的根据教材学习数据结构」· C语言 代码 · 共 37 行

C
37
字号
 /************************************************/
 /*将一个中缀表达式,转换为与它等价的后缀表达式  */
 /*      文件名postfix.c,函数名postfix()        */
 /************************************************/
 void postfix(char e[],char f[])
 { int i=0,j=0;
   char opst[100];
   int top,t;
   top=0;
   opst[top]='#';top++;
   while(e[i]!='#')
   {
     if ((e[i]>='0'&&e[i]<='9')||e[i]=='.')
       f[j++]=e[i];                  /*遇到数字和小数点直接写入后缀表达式*/
     else if (e[i]=='(')                /*遇到左括号进入操作符栈*/
          { opst[top]=e[i];top++;}
          else if (e[i]==')')
            /*遇到右括号将其对应的左括号后的操作符全部写入后缀表达式*/
	    { t=top-1;
	      while (opst[t]!='(') {f[j++]=opst[--top];t=top-1;}
              top--;            /*'('出栈*/
            }
	   else if (is_operation(e[i]))         /* '+ ,-, *, /' */
                  { f[j++]=' ';           /*用空格分开两个操作数*/
	            while (priority(opst[top-1])>=priority(e[i]))
                    f[j++]=opst[--top];

                    opst[top]=e[i];top++;     /*当前元素进栈*/
                  }
       i++;  /*处理下一个元素*/
    }

    while (top) f[j++]=opst[--top];

  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?