【WEISS算法书】中缀转后缀

WEISS的书看到栈,顺手把中缀转后缀写了出来。。照着书的思路写的。。只能支持加号乘号左右括号。。

乱写一气,功能实现了就好。。栈的实现就不写了。。太简单了。。

Code:
  1. #include "stdafx.h"   
  2. #include <stdio.h>   
  3. #include <stdlib.h>   
  4. #include "Stack.h"   
  5.   
  6. void  
  7. DispPostFix(char res[],int len)   
  8. {   
  9.     Stack s;   
  10.     s=CreateStack();   
  11.     for(int i=0;i<len;i++)   
  12.     {   
  13.         char c=res[i];   
  14.         switch(c)   
  15.         {   
  16.         case '+':   
  17.             while(true)   
  18.             {   
  19.                 if(IsEmpty(s)|| Top(s)=='(' )   
  20.                 {   
  21.                     Push(c,s);   
  22.                     break;   
  23.                 }   
  24.                 if(Top(s)=='*' || Top(s)=='+')   
  25.                 {   
  26.                     printf("%c",Top(s));   
  27.                     Pop(s);   
  28.                 }   
  29.             }   
  30.             break;   
  31.   
  32.         case '*':   
  33.             while(true)   
  34.             {   
  35.                 if(IsEmpty(s)||Top(s)=='+' || Top(s)=='(' )   
  36.                 {   
  37.                     Push(c,s);   
  38.                     break;   
  39.                 }   
  40.                 else  
  41.                 {   
  42.                     printf("%c",Top(s));   
  43.                     Pop(s);   
  44.                 }   
  45.             }   
  46.             break;   
  47.         case '(':   
  48.             Push(c,s);   
  49.             break;   
  50.   
  51.         case ')':   
  52.             while(Top(s)!='(')   
  53.             {   
  54.                 printf("%c",Top(s));   
  55.                 Pop(s);   
  56.             }   
  57.             Pop(s);   
  58.             break;   
  59.         default:   
  60.             printf("%c",c);   
  61.             break;   
  62.         }   
  63.     }   
  64.     while(!IsEmpty(s))   
  65.         {      
  66.             printf("%c",Top(s));   
  67.             Pop(s);   
  68.         }      
  69.         DisposeStack(s);   
  70. }   
  71.   
  72. int main()   
  73. {   
  74.     char inFix[]="a+b*c+(d*e+f)*g";   
  75.     DispPostFix(inFix,sizeof(inFix)/sizeof(inFix[0])-1);   
  76.        
  77.     return 0;   
  78. }  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值