第六周项目5—后缀表达式

问题及代码

/*    
*烟台大学计控学院     
*作    者:郗传秀    
*完成日期:2016年9月29日 
*问题描述:利用sqstack.h中栈的基本运算,实现将一个中缀表达式转换为对应的后缀表达式的算法。例如,输入(56-20)/(4+2),输出后缀表达式::56#20#-4#2#+/要求在数字后加#。 
*/  

(1)sqstack.h

[cpp] view plain copy
#ifndef SQSTACK_H_INCLUDED  
#define SQSTACK_H_INCLUDED  
  
  
#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 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);  //输出栈  
int leftpri(char op);  
int rightpri(char op);  //求右运算符op的优先级  
bool InOp(char ch);      //判断ch是否为运算符  
int Precede(char op1,char op2);  //op1和op2运算符优先级的比较结果  
void trans(char *exp,char postexp[]);  
//将算术表达式exp转换成后缀表达式postexp  
  
  
  
#endif // SQSTACK_H_INCLUDED  

(2)sqstack.cpp

[cpp] view plain copy
#include <stdio.h>  
#include <stdlib.h>  
#include "sqstack.h"  
#define MaxOp 7  
#include"sqstack.h"  
#include<stdio.h>  
#include<malloc.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");  
}  
  
int leftpri(char op)    //求左运算符op的优先级  
{  
    int i;  
    for (i=0; i<MaxOp; i++)  
        if (lpri[i].ch==op)  
            return lpri[i].pri;  
}  
  
int rightpri(char op)  //求右运算符op的优先级  
{  
    int i;  
    for (i=0; i<MaxOp; i++)  
        if (rpri[i].ch==op)  
            return rpri[i].pri;  
}  
  
bool InOp(char ch)       //判断ch是否为运算符  
{  
    if (ch=='(' || ch==')' || ch=='+' || ch=='-'  
            || ch=='*' || ch=='/')  
        return true;  
    else  
        return false;  
}  
  
int Precede(char op1,char op2)  //op1和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[])  
//将算术表达式exp转换成后缀表达式postexp  
{  
    SqStack *opstack;               //定义运算符栈  
    int i=0;                //i作为postexp的下标  
    ElemType ch;  
    InitStack(opstack);   //用初始化栈运算为栈分配空间,务必要做  
    Push(opstack, '=');  
    while (*exp!='\0')      //exp表达式未扫描完时循环  
    {  
        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中  
                postexp[i++]=ch;  
                Pop(opstack, ch);  
                break;  
            }  
        }  
  
    } //while (*exp!='\0')  
    Pop(opstack, ch);  
    while (ch!='=')  
        //此时exp扫描完毕,退栈到'='为止  
    {  
        postexp[i++]=ch;  
        Pop(opstack, ch);  
    }  
    postexp[i]='\0';    //给postexp表达式添加结束标识  
    DestroyStack(opstack);  
}  
(3)main.cpp

[cpp] view plain copy
#include"sqstack.h"  
#include<stdio.h>  
int main()  
{  
    char exp[]="(56-20)/(4+2)"; //可将exp改为键盘输入  
    char postexp[200];  
    trans(exp,postexp);  
    printf("中缀表达式:%s\n",exp);  
    printf("后缀表达式:%s\n",postexp);  
    return 0;  
}  


运行结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值