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

来源:互联网 发布:邪恶的mmd骑马动作数据 编辑:程序博客网 时间:2024/05/06 05:20

问题及代码:

[cpp] view plain copy
  1. *Copyright(c)2016,烟台大学计算机与控制工程学院  
  2.  *All right reserved.  
  3.  *文件名称:后缀表达式.cpp  
  4.  *作者:陈晓琳  
  5.  *完成日期;2016年9月30日  
  6.  *版本号;v1.0  
  7.  *  
  8.  *问题描述: 利用sqstack.h中栈的基本运算,实现将一个中缀表达式转换为对应的后缀表达式的算法。  
  9.  例如,输入(56-20)/(4+2),输出后缀表达式::56#20#-4#2#+/要求在数字后加#。  
  10.   
  11.   
  12.   
  13.   
  14.  *输入描述:表达式  
  15.  *程序输出:后缀表达式  
  16. */  
  17.   
  18.   
  19. #include <stdio.h>  
  20. #include "sqstack.h"  
  21. #include <stdlib.h>  
  22.   
  23.   
  24. int main()  
  25. {  
  26.     char exp[]="(56-20)/(4+2)"//可将exp改为键盘输入  
  27.     char postexp[200];  
  28.     trans(exp,postexp);  
  29.     printf("中缀表达式:%s\n",exp);  
  30.     printf("后缀表达式:%s\n",postexp);  
  31.     return 0;  
  32. }  
  33.   
  34.   
  35. //sqstack.cpp  
  36. #include <stdio.h>  
  37. #include <malloc.h>  
  38. #include "sqstack.h"  
  39. int leftpri(char op)    //求左运算符op的优先级  
  40. {  
  41.     int i;  
  42.     for (i=0; i<MaxOp; i++)  
  43.         if (lpri[i].ch==op)  
  44.             return lpri[i].pri;  
  45. }  
  46.   
  47. int rightpri(char op)  //求右运算符op的优先级  
  48. {  
  49.     int i;  
  50.     for (i=0; i<MaxOp; i++)  
  51.         if (rpri[i].ch==op)  
  52.             return rpri[i].pri;  
  53. }  
  54.   
  55. bool InOp(char ch)       //判断ch是否为运算符  
  56. {  
  57.     if (ch=='(' || ch==')' || ch=='+' || ch=='-'  
  58.             || ch=='*' || ch=='/')  
  59.         return true;  
  60.     else  
  61.         return false;  
  62. }  
  63.   
  64. int Precede(char op1,char op2)  //op1和op2运算符优先级的比较结果  
  65. {  
  66.     if (leftpri(op1)==rightpri(op2))  
  67.         return 0;  
  68.     else if (leftpri(op1)<rightpri(op2))  
  69.         return -1;  
  70.     else  
  71.         return 1;  
  72. }  
  73. void trans(char *exp,char postexp[])  
  74. //将算术表达式exp转换成后缀表达式postexp  
  75. {  
  76.     SqStack *opstack;               //定义运算符栈  
  77.     int i=0;                //i作为postexp的下标  
  78.     ElemType ch;  
  79.     InitStack(opstack);   //用初始化栈运算为栈分配空间,务必要做  
  80.     Push(opstack, '=');  
  81.     while (*exp!='\0')      //exp表达式未扫描完时循环  
  82.     {  
  83.         if (!InOp(*exp))        //为数字字符的情况  
  84.         {  
  85.             while (*exp>='0' && *exp<='9'//判定为数字  
  86.             {  
  87.                 postexp[i++]=*exp;  
  88.                 exp++;  
  89.             }  
  90.             postexp[i++]='#';   //用#标识一个数值串结束  
  91.         }  
  92.         else    //为运算符的情况  
  93.         {  
  94.             GetTop(opstack, ch);   //取得栈顶的运算符  
  95.             switch(Precede(ch ,*exp))  
  96.             {  
  97.             case -1:           //栈顶运算符的优先级低:进栈  
  98.                 Push(opstack, *exp);  
  99.                 exp++;     //继续扫描其他字符  
  100.                 break;  
  101.             case 0:        //只有括号满足这种情况  
  102.                 Pop(opstack, ch);      //将(退栈  
  103.                 exp++;     //继续扫描其他字符  
  104.                 break;  
  105.             case 1:             //退栈并输出到postexp中  
  106.                 postexp[i++]=ch;  
  107.                 Pop(opstack, ch);  
  108.                 break;  
  109.             }  
  110.         }  
  111.   
  112.     } //while (*exp!='\0')  
  113.     Pop(opstack, ch);  
  114.     while (ch!='=')  
  115.         //此时exp扫描完毕,退栈到'='为止  
  116.     {  
  117.         postexp[i++]=ch;  
  118.         Pop(opstack, ch);  
  119.     }  
  120.     postexp[i]='\0';    //给postexp表达式添加结束标识  
  121.     DestroyStack(opstack);  
  122. }  
  123.   
  124. void InitStack(SqStack *&s)  
  125. {  
  126.     s=(SqStack *)malloc(sizeof(SqStack));  
  127.     s->top=-1;  
  128. }  
  129. void DestroyStack(SqStack *&s)  
  130. {  
  131.     free(s);  
  132. }  
  133. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  134. {  
  135.     return(s->top+1);  
  136. }  
  137. bool StackEmpty(SqStack *s)  
  138. {  
  139.     return(s->top==-1);  
  140. }  
  141. bool Push(SqStack *&s,ElemType e)  
  142. {  
  143.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  144.         return false;  
  145.     s->top++;  
  146.     s->data[s->top]=e;  
  147.     return true;  
  148. }  
  149. bool Pop(SqStack *&s,ElemType &e)  
  150. {  
  151.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  152.         return false;  
  153.     e=s->data[s->top];  
  154.     s->top--;  
  155.     return true;  
  156. }  
  157. bool GetTop(SqStack *s,ElemType &e)  
  158. {  
  159.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  160.         return false;  
  161.     e=s->data[s->top];  
  162.     return true;  
  163. }  
  164.   
  165. void DispStack(SqStack *s)  //输出栈  
  166. {  
  167.     int i;  
  168.     for (i=s->top;i>=0;i--)  
  169.         printf("%c ",s->data[i]);  
  170.     printf("\n");  
  171. }  
  172. #ifndef SQSTACK_H_INCLUDED  
  173. #define SQSTACK_H_INCLUDED  
  174. #define MaxOp 7  
  175. #define MaxSize 100  
  176. typedef char ElemType;  
  177. typedef struct  
  178. {  
  179.     ElemType data[MaxSize];  
  180.     int top;                //栈指针  
  181. } SqStack;  
  182. struct  //设定运算符优先级  
  183. {  
  184.     char ch;   //运算符  
  185.     int pri;   //优先级  
  186. }  
  187. lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},  
  188. rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};  
  189.                  //顺序栈类型定义  
  190. void trans(char *exp,char postexp[]);  
  191. int Precede(char op1,char op2);  
  192. bool InOp(char ch);  
  193. int rightpri(char op);  
  194. int leftpri(char op);  
  195. void InitStack(SqStack *&s);    //初始化栈  
  196. void DestroyStack(SqStack *&s);  //销毁栈  
  197. bool StackEmpty(SqStack *s);     //栈是否为空  
  198. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  199. bool Push(SqStack *&s,ElemType e); //入栈  
  200. bool Pop(SqStack *&s,ElemType &e); //出栈  
  201. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  202. void DispStack(SqStack *s);  //输出栈  
  203.   
  204.   
  205.   
  206. #endif // SQSTACK_H_INCLUDED  


运行结果:


知识点总结:

1 初始化运算符栈op,将“=”进栈作为栈底元素(“=”不属于表达式中的字符,其优先级最低)。

             2 从exp中读取字符ch,若ch是运算数,将其放入postexp中,加“#”;若ch是运算符,将其和op栈顶运算符sop的优先级比较,若ch运算符的优先级大于sop运算符的优先级,ch进栈并读取exp的下一个字符,反之,sop进入postexp中,直到 ch的运算符优先级大于栈顶运算符的优先级。至exp扫描完,若栈op不为空,将“=”之前的运算符都退栈放入postexp。

              注意要设置运算符的优先级,左右括号的优先级相等。

             将算数表达式exp转换成后缀表达式postexp:


0 0
原创粉丝点击