1. /*  
  2. 做这道题之前我用了一上午头的时间都没有明白怎么变成后缀式,但是一旦知道后缀式怎么变成表达式的时候思路就很清晰了(感谢秦川同学,尚灿芳同学//scanf= =...,赵鹏同学对小的不离不弃不厌倦= =。。。);  
  3.  
  4.  
  5. 转换成后缀式无非就是遵循保证优先级高或者是相等时候入栈的的先打出来(此题中即先出栈打印),遇到()时先把括号里的输出在输出括号外面的;  
  6.  
  7. */ 
  8. #include<stdio.h>  
  9. #include<stdlib.h>  
  10. struct stack  
  11. {  
  12.     char c[100];  
  13.     int top;  
  14. };  
  15.  
  16. int Push ( struct stack *p,char ch )  
  17. {  
  18.     if( p->top == 49 )  
  19.         return 0;  
  20.     else 
  21.     {  
  22.         p->top++;  
  23.         p->c[p->top] = ch;  
  24.         return 1;  
  25.     }  
  26. }  
  27.  
  28. int Pop ( struct stack *p,char *ch)  
  29. {  
  30.     if( p->top<0 )return 0;  
  31.     else 
  32.     {     
  33.         *ch = p->c[p->top];  
  34.         p->top--;  
  35.         return 1;  
  36.     }  
  37.  
  38. }  
  39.  
  40. int sw(char c)//优先级的选择很重要。而且()的优先级也很重要,否则下面的思路会混乱  
  41.  {  
  42.      if(c=='+' || c=='-'return 1;  
  43.      if(c=='*' || c=='/')  return 2;  
  44.      if(c=='('return 3;  
  45.      if(c==')'return 4;  
  46.  }  
  47.  
  48. int main()  
  49. {  
  50.  
  51.     struct stack *p;  
  52.     char ch,a,*h,g;  
  53.     int i,leap = 0;  
  54.     h=&a;  
  55.     p = (struct stack *)malloc(sizeof(struct stack));  
  56.     p->top = -1;  
  57.     while((ch = getchar())!='#' )  
  58.     {  
  59.         if( ch <= 'z' && ch >= 'a')  //若果是字母就打印出来
  60.         {  
  61.             printf("%c",ch);  
  62.         }  
  63.         else //如果是标号****难点***
  64.         {  
  65.             if(p->top == -1)  //如果是栈底直接压入
  66.                 Push(p,ch);  
  67.             else 
  68.             {  
  69.                 if(sw(ch)>=sw(p->c[p->top]))  //如果输入的优先级高
  70.                 {  
  71.                     if(ch == ')')  //最高点是),如果是他就把(前的全部弹出
  72.                     {  
  73.                         while(p->c[p->top]!='(')  
  74.                         {  
  75.                             printf("%c",p->c[p->top]);  
  76.                             Pop(p,h);  
  77.                         }  
  78.                     Pop(p,h);  //这一点很重要,是排出(
  79.                     }  
  80.                     else //如果不是)就压入,不论是不是(;
  81.                     {     
  82.                             Push(p,ch);  
  83.                     }  
  84.                 }  
  85.                 else //如果输入的优先级低或者等于而且分两种情况
  86.                 {  
  87.                         if(p->c[p->top]=='(')  //低的话有两种可能一个是他是加减法,另一个就是前面的是‘(’(不可能是),因为它不入栈)
  88.                         {  
  89.                             Push(p,ch);  //直接压入
  90.                         }  
  91.                         else 
  92.                         {  
  93.                         printf("%c",p->c[p->top]); //保证了存在站里面的(之前的永远是低的(换句话说就是+-)
  94.                         Pop(p,h);  
  95.                         Push(p,ch);  
  96.                           
  97.                     }  
  98.                 }  
  99.             }  
  100.         }  
  101.           
  102.     }  
  103.     while(p->top!=-1)  
  104.                         {  
  105.                             printf("%c",p->c[p->top]);  
  106.                             Pop(p,h);  
  107.                         }return 0;  
  108. }  
  109.