第五周项目5 -——后缀表达式

  1. /*  
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院  
  3. * All rights reserved.  
  4. * 文件名称:wu.cpp  
  5. * 作    者:武昊  
  6. * 完成日期:2016年9月29日  
  7. * 版 本 号:v1.0   
  8. *问题描述::利用sqstack.h中栈的基本运算,实现将一个中缀表达式转换为对应的后缀表达  
  9.           式的算法。例如,输入(56-20)/(4+2),输出后缀表达式::56#20#-4#2#+/要求  
  10.           在数字后加#   
  11. *输入描述:前缀表达式  
  12. *程序输出:中缀表达式 后缀表达式  
  13. */    
  1. #include "sqstack.h"  
  2. #define MaxOp 7  
  3.   
  4. struct  //设定运算符优先级  
  5. {  
  6.     char ch;   //运算符  
  7.     int pri;   //优先级  
  8. }  
  9. lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},  
  10. rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};  
  11.   
  12. int leftpri(char op)    //求左运算符op的优先级  
  13. {  
  14.     int i;  
  15.     for (i=0; i<MaxOp; i++)  
  16.         if (lpri[i].ch==op)  
  17.             return lpri[i].pri;  
  18. }  
  19.   
  20. int rightpri(char op)  //求右运算符op的优先级  
  21. {  
  22.     int i;  
  23.     for (i=0; i<MaxOp; i++)  
  24.         if (rpri[i].ch==op)  
  25.             return rpri[i].pri;  
  26. }  
  27.   
  28. bool InOp(char ch)       //判断ch是否为运算符  
  29. {  
  30.     if (ch=='(' || ch==')' || ch=='+' || ch=='-'  
  31.             || ch=='*' || ch=='/')  
  32.         return true;  
  33.     else  
  34.         return false;  
  35. }  
  36.   
  37. int Precede(char op1,char op2)  //op1和op2运算符优先级的比较结果  
  38. {  
  39.     if (leftpri(op1)==rightpri(op2))  
  40.         return 0;  
  41.     else if (leftpri(op1)<rightpri(op2))  
  42.         return -1;  
  43.     else  
  44.         return 1;  
  45. }  
  46. void trans(char *exp,char postexp[])  
  47. //将算术表达式exp转换成后缀表达式postexp  
  48. {  
  49.     SqStack *opstack;               //定义运算符栈  
  50.     int i=0;                //i作为postexp的下标  
  51.     ElemType ch;  
  52.     InitStack(opstack);   //用初始化栈运算为栈分配空间,务必要做  
  53.     Push(opstack, '=');  
  54.     while (*exp!='\0')      //exp表达式未扫描完时循环  
  55.     {  
  56.         if (!InOp(*exp))        //为数字字符的情况  
  57.         {  
  58.             while (*exp>='0' && *exp<='9'//判定为数字  
  59.             {  
  60.                 postexp[i++]=*exp;  
  61.                 exp++;  
  62.             }  
  63.             postexp[i++]='#';   //用#标识一个数值串结束  
  64.         }  
  65.         else    //为运算符的情况  
  66.         {  
  67.             GetTop(opstack, ch);   //取得栈顶的运算符  
  68.             switch(Precede(ch ,*exp))  
  69.             {  
  70.             case -1:           //栈顶运算符的优先级低:进栈  
  71.                 Push(opstack, *exp);  
  72.                 exp++;     //继续扫描其他字符  
  73.                 break;  
  74.             case 0:        //只有括号满足这种情况  
  75.                 Pop(opstack, ch);      //将(退栈  
  76.                 exp++;     //继续扫描其他字符  
  77.                 break;  
  78.             case 1:             //退栈并输出到postexp中  
  79.                 postexp[i++]=ch;  
  80.                 Pop(opstack, ch);  
  81.                 break;  
  82.             }  
  83.         }  
  84.   
  85.     } //while (*exp!='\0')  
  86.     Pop(opstack, ch);  
  87.     while (ch!='=')  
  88.         //此时exp扫描完毕,退栈到'='为止  
  89.     {  
  90.         postexp[i++]=ch;  
  91.         Pop(opstack, ch);  
  92.     }  
  93.     postexp[i]='\0';    //给postexp表达式添加结束标识  
  94.     DestroyStack(opstack);  
  95. }  
  96.   
  97. int main()  
  98. {  
  99.     char exp[]="(56-20)/(4+2)"//可将exp改为键盘输入  
  100.     char postexp[200];  
  101.     trans(exp,postexp);  
  102.     printf("中缀表达式:%s\n",exp);  
  103.     printf("后缀表达式:%s\n",postexp);  
  104.     return 0;  
  105. }  
sqstack.h
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #define MaxSize 100  
  4. typedef char ElemType;  
  5. typedef struct  
  6. {  
  7.     ElemType data[MaxSize];  
  8.     int top;                //栈指针  
  9. } SqStack;                  //顺序栈类型定义  
  10.   
  11. void InitStack(SqStack *&s);    //初始化栈  
  12. void DestroyStack(SqStack *&s);  //销毁栈  
  13. bool StackEmpty(SqStack *s);     //栈是否为空  
  14. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  15. bool Push(SqStack *&s,ElemType e); //入栈  
  16. bool Pop(SqStack *&s,ElemType &e); //出栈  
  17. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  18. void DispStack(SqStack *s);  //输出栈  
  1. #include "sqstack.h"  
  2. void InitStack(SqStack *&s)//初始化栈  
  3. {  
  4.     s=(SqStack *)malloc(sizeof(SqStack));  
  5.     s->top=-1;  
  6. }  
  7. void DestroyStack(SqStack *&s)//销毁栈  
  8. {  
  9.     free(s);  
  10. }  
  11. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  12. {  
  13.     return(s->top+1);  
  14. }  
  15. bool StackEmpty(SqStack *s)//判断是否为空栈  
  16. {  
  17.     return(s->top==-1);  
  18. }  
  19. bool Push(SqStack *&s,ElemType e)  
  20. {  
  21.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  22.         return false;  
  23.     s->top++;  
  24.     s->data[s->top]=e;  
  25.     return true;  
  26. }  
  27. bool Pop(SqStack *&s,ElemType &e)  
  28. {  
  29.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  30.         return false;  
  31.     e=s->data[s->top];  
  32.     s->top--;  
  33.     return true;  
  34. }  
  35. bool GetTop(SqStack *s,ElemType &e)  
  36. {  
  37.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  38.         return false;  
  39.     e=s->data[s->top];  
  40.     return true;  
  41. }  
  42.   
  43. void DispStack(SqStack *s)  //输出栈  
  44. {  
  45.     int i;  
  46.     for (i=s->top;i>=0;i--)  
  47.         printf("%c ",s->data[i]);  
  48.     printf("\n");  
  49. }  
运行结果:
知识点总结:
栈的具体问题实际应用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值