第五周项目5-后缀表达式(选做)

来源:互联网 发布:如何学软件技术培训 编辑:程序博客网 时间:2024/04/28 07:01

/*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松  

* 完成日期:2016年9月29日   

* 问题及代码

* 问题描述:利用sqstack.h中栈的基本运算,实现将一个中缀表达式转换为对应的后缀表达式的算法。

* 输入描述:输入(56-20)/(4+2),  

* 程序输出:56#20#-4#2#+/要求在数字后加#。

*/

代码:

1.sqstack.h

[csharp] view plain copy
  1. #ifndef SQSTACK_H_INCLUDED  
  2. #define SQSTACK_H_INCLUDED  
  3.  
  4. #define MaxSize 100  
  5. typedef int ElemType;  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  
  9.     int top;                //栈指针  
  10. } SqStack;                  //顺序栈类型定义  
  11.   
  12. void InitStack(SqStack *&s);    //初始化栈  
  13. void DestroyStack(SqStack *&s);  //销毁栈  
  14. bool StackEmpty(SqStack *s);     //栈是否为空  
  15. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  16. bool Push(SqStack *&s,ElemType e); //入栈  
  17. bool Pop(SqStack *&s,ElemType &e); //出栈  
  18. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  19. void DispStack(SqStack *s);  //输出栈  
  20.  
  21. #endif // SQSTACK_H_INCLUDED  


2.sqstack.cpp

[csharp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "sqstack.h"  
  4.   
  5. void InitStack(SqStack *&s)  
  6. {  
  7.     s=(SqStack *)malloc(sizeof(SqStack));  
  8.     s->top=-1;  
  9. }  
  10. void DestroyStack(SqStack *&s)  
  11. {  
  12.     free(s);  
  13. }  
  14. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  15. {  
  16.     return(s->top+1);  
  17. }  
  18. bool StackEmpty(SqStack *s)  
  19. {  
  20.     return(s->top==-1);  
  21. }  
  22. bool Push(SqStack *&s,ElemType e)  
  23. {  
  24.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  25.         return false;  
  26.     s->top++;  
  27.     s->data[s->top]=e;  
  28.     return true;  
  29. }  
  30. bool Pop(SqStack *&s,ElemType &e)  
  31. {  
  32.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  33.         return false;  
  34.     e=s->data[s->top];  
  35.     s->top--;  
  36.     return true;  
  37. }  
  38. bool GetTop(SqStack *s,ElemType &e)  
  39. {  
  40.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  41.         return false;  
  42.     e=s->data[s->top];  
  43.     return true;  
  44. }  
  45.   
  46. void DispStack(SqStack *s)  //输出栈  
  47. {  
  48.     int i;  
  49.     for (i=s->top;i>=0;i--)  
  50.         printf("%c ",s->data[i]);  
  51.     printf("\n");  
  52. }  


3.main.cpp

[csharp] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "sqstack.h"  
  4. #define MaxOp 7  
  5.   
  6. struct  //设定运算符优先级  
  7. {  
  8.     char ch;   //运算符  
  9.     int pri;   //优先级  
  10. }  
  11. lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},  
  12. rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};  
  13.   
  14. int leftpri(char op)    //求左运算符op的优先级  
  15. {  
  16.     int i;  
  17.     for (i=0; i<MaxOp; i++)  
  18.         if (lpri[i].ch==op)  
  19.             return lpri[i].pri;  
  20. }  
  21.   
  22. int rightpri(char op)  //求右运算符op的优先级  
  23. {  
  24.     int i;  
  25.     for (i=0; i<MaxOp; i++)  
  26.         if (rpri[i].ch==op)  
  27.             return rpri[i].pri;  
  28. }  
  29.   
  30. bool InOp(char ch)       //判断ch是否为运算符  
  31. {  
  32.     if (ch=='(' || ch==')' || ch=='+' || ch=='-'  
  33.             || ch=='*' || ch=='/')  
  34.         return true;  
  35.     else  
  36.         return false;  
  37. }  
  38.   
  39. int Precede(char op1,char op2)  //op1和op2运算符优先级的比较结果  
  40. {  
  41.     if (leftpri(op1)==rightpri(op2))  
  42.         return 0;  
  43.     else if (leftpri(op1)<rightpri(op2))  
  44.         return -1;  
  45.     else  
  46.         return 1;  
  47. }  
  48. void trans(char *exp,char postexp[])  
  49. //将算术表达式exp转换成后缀表达式postexp  
  50. {  
  51.     SqStack *opstack;               //定义运算符栈  
  52.     int i=0;                //i作为postexp的下标  
  53.     ElemType ch;  
  54.     InitStack(opstack);   //用初始化栈运算为栈分配空间,务必要做  
  55.     Push(opstack, '=');  
  56.     while (*exp!='\0')      //exp表达式未扫描完时循环  
  57.     {  
  58.         if (!InOp(*exp))        //为数字字符的情况  
  59.         {  
  60.             while (*exp>='0' && *exp<='9'//判定为数字  
  61.             {  
  62.                 postexp[i++]=*exp;  
  63.                 exp++;  
  64.             }  
  65.             postexp[i++]='#';   //用#标识一个数值串结束  
  66.         }  
  67.         else    //为运算符的情况  
  68.         {  
  69.             GetTop(opstack, ch);   //取得栈顶的运算符  
  70.             switch(Precede(ch ,*exp))  
  71.             {  
  72.             case -1:           //栈顶运算符的优先级低:进栈  
  73.                 Push(opstack, *exp);  
  74.                 exp++;     //继续扫描其他字符  
  75.                 break;  
  76.             case 0:        //只有括号满足这种情况  
  77.                 Pop(opstack, ch);      //将(退栈  
  78.                 exp++;     //继续扫描其他字符  
  79.                 break;  
  80.             case 1:             //退栈并输出到postexp中  
  81.                 postexp[i++]=ch;  
  82.                 Pop(opstack, ch);  
  83.                 break;  
  84.             }  
  85.         }  
  86.   
  87.     } //while (*exp!='\0')  
  88.     Pop(opstack, ch);  
  89.     while (ch!='=')  
  90.         //此时exp扫描完毕,退栈到'='为止  
  91.     {  
  92.         postexp[i++]=ch;  
  93.         Pop(opstack, ch);  
  94.     }  
  95.     postexp[i]='\0';    //给postexp表达式添加结束标识  
  96.     DestroyStack(opstack);  
  97. }  
  98.   
  99. int main()  
  100. {  
  101.     char exp[]="(56-20)/(4+2)"//可将exp改为键盘输入  
  102.     char postexp[200];  
  103.     trans(exp,postexp);  
  104.     printf("中缀表达式:%s\n",exp);  
  105.     printf("后缀表达式:%s\n",postexp);  
  106.     return 0;  
  107. }  


运行结果:

0 0
原创粉丝点击