第五周--项目5后缀表达式
来源:互联网 发布:邪恶的mmd骑马动作数据 编辑:程序博客网 时间:2024/05/06 05:20
问题及代码:
- *Copyright(c)2016,烟台大学计算机与控制工程学院
- *All right reserved.
- *文件名称:后缀表达式.cpp
- *作者:陈晓琳
- *完成日期;2016年9月30日
- *版本号;v1.0
- *
- *问题描述: 利用sqstack.h中栈的基本运算,实现将一个中缀表达式转换为对应的后缀表达式的算法。
- 例如,输入(56-20)/(4+2),输出后缀表达式::56#20#-4#2#+/要求在数字后加#。
-
-
-
-
- *输入描述:表达式
- *程序输出:后缀表达式
- */
-
-
- #include <stdio.h>
- #include "sqstack.h"
- #include <stdlib.h>
-
-
- int main()
- {
- char exp[]="(56-20)/(4+2)";
- char postexp[200];
- trans(exp,postexp);
- printf("中缀表达式:%s\n",exp);
- printf("后缀表达式:%s\n",postexp);
- return 0;
- }
-
-
-
- #include <stdio.h>
- #include <malloc.h>
- #include "sqstack.h"
- int leftpri(char op)
- {
- int i;
- for (i=0; i<MaxOp; i++)
- if (lpri[i].ch==op)
- return lpri[i].pri;
- }
-
- int rightpri(char op)
- {
- int i;
- for (i=0; i<MaxOp; i++)
- if (rpri[i].ch==op)
- return rpri[i].pri;
- }
-
- bool InOp(char ch)
- {
- if (ch=='(' || ch==')' || ch=='+' || ch=='-'
- || ch=='*' || ch=='/')
- return true;
- else
- return false;
- }
-
- int Precede(char op1,char op2)
- {
- if (leftpri(op1)==rightpri(op2))
- return 0;
- else if (leftpri(op1)<rightpri(op2))
- return -1;
- else
- return 1;
- }
- void trans(char *exp,char postexp[])
-
- {
- SqStack *opstack;
- int i=0;
- ElemType ch;
- InitStack(opstack);
- Push(opstack, '=');
- while (*exp!='\0')
- {
- if (!InOp(*exp))
- {
- while (*exp>='0' && *exp<='9')
- {
- postexp[i++]=*exp;
- exp++;
- }
- postexp[i++]='#';
- }
- else
- {
- GetTop(opstack, ch);
- switch(Precede(ch ,*exp))
- {
- case -1:
- Push(opstack, *exp);
- exp++;
- break;
- case 0:
- Pop(opstack, ch);
- exp++;
- break;
- case 1:
- postexp[i++]=ch;
- Pop(opstack, ch);
- break;
- }
- }
-
- }
- Pop(opstack, ch);
- while (ch!='=')
-
- {
- postexp[i++]=ch;
- Pop(opstack, ch);
- }
- postexp[i]='\0';
- DestroyStack(opstack);
- }
-
- void InitStack(SqStack *&s)
- {
- s=(SqStack *)malloc(sizeof(SqStack));
- s->top=-1;
- }
- void DestroyStack(SqStack *&s)
- {
- free(s);
- }
- int StackLength(SqStack *s)
- {
- return(s->top+1);
- }
- bool StackEmpty(SqStack *s)
- {
- return(s->top==-1);
- }
- bool Push(SqStack *&s,ElemType e)
- {
- if (s->top==MaxSize-1)
- return false;
- s->top++;
- s->data[s->top]=e;
- return true;
- }
- bool Pop(SqStack *&s,ElemType &e)
- {
- if (s->top==-1)
- return false;
- e=s->data[s->top];
- s->top--;
- return true;
- }
- bool GetTop(SqStack *s,ElemType &e)
- {
- if (s->top==-1)
- return false;
- e=s->data[s->top];
- return true;
- }
-
- void DispStack(SqStack *s)
- {
- int i;
- for (i=s->top;i>=0;i--)
- printf("%c ",s->data[i]);
- printf("\n");
- }
- #ifndef SQSTACK_H_INCLUDED
- #define SQSTACK_H_INCLUDED
- #define MaxOp 7
- #define MaxSize 100
- typedef char ElemType;
- typedef struct
- {
- ElemType data[MaxSize];
- int top;
- } SqStack;
- struct
- {
- char ch;
- int pri;
- }
- lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},
- rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};
-
- void trans(char *exp,char postexp[]);
- int Precede(char op1,char op2);
- bool InOp(char ch);
- int rightpri(char op);
- int leftpri(char op);
- void InitStack(SqStack *&s);
- void DestroyStack(SqStack *&s);
- bool StackEmpty(SqStack *s);
- int StackLength(SqStack *s);
- bool Push(SqStack *&s,ElemType e);
- bool Pop(SqStack *&s,ElemType &e);
- bool GetTop(SqStack *s,ElemType &e);
- void DispStack(SqStack *s);
-
-
-
- #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