第五周项目5-后缀表达式(选做)
来源:互联网 发布:如何学软件技术培训 编辑:程序博客网 时间:2024/04/28 07:01
代码:
1.sqstack.h
- #ifndef SQSTACK_H_INCLUDED
- #define SQSTACK_H_INCLUDED
-
- #define MaxSize 100
- typedef int ElemType;
- typedef struct
- {
- ElemType data[MaxSize];
- int top;
- } SqStack;
-
- 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
2.sqstack.cpp
- #include <stdio.h>
- #include <malloc.h>
- #include "sqstack.h"
-
- 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");
- }
3.main.cpp
- #include <stdio.h>
- #include <stdlib.h>
- #include "sqstack.h"
- #define MaxOp 7
-
- struct
- {
- char ch;
- int pri;
- }
- lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},
- rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};
-
- 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);
- }
-
- int main()
- {
- char exp[]="(56-20)/(4+2)";
- char postexp[200];
- trans(exp,postexp);
- printf("中缀表达式:%s\n",exp);
- printf("后缀表达式:%s\n",postexp);
- return 0;
- }
运行结果:
0 0