数据结构之栈的应用中缀表达式转后缀表达式及求值

手写栈版本

#include <iostream>

using namespace std;

typedef double ElemType;
const int MaxSize = 100;
const int inf = 0x3f3f3f3f;

typedef struct StackNode
{
    ElemType data;
    StackNode *next;
}SqStack;

void InitStack(SqStack *&s);
bool StackEmpty(SqStack *s);
bool GetTop(SqStack *s, ElemType &e);
bool Pop(SqStack *&s, ElemType &e);
void Push(SqStack *&s, ElemType e);
void DestroyStack(SqStack *&s);
void trans(char *exp, char postexp[]);
double compvalue(char *postexp);

int main(void)
{
    char exp[MaxSize];
    char postexp[MaxSize];
    SqStack *s;
    InitStack(s);/*
    scanf("%s", exp);
    trans(exp, postexp);
    cout << postexp << endl;*/
    scanf("%s", postexp);
    cout << compvalue(postexp) << endl;
    
    
    return 0;
}

void InitStack(SqStack *&s)
{
    s = new SqStack;
    s->next = NULL;
}

bool StackEmpty(SqStack *s)
{
    return (s->next == NULL);
}

void Push(SqStack *&s, ElemType e)
{
    SqStack *q = new SqStack;
    q->data = e;
    q->next = s->next;
    s->next = q;
}

bool Pop(SqStack *&s, ElemType &e)
{
    if (s->next == NULL)
        return false;
    else
    {
        e = s->next->data;
        SqStack *p = s->next;
        s->next = p->next;
        delete p;
        p = NULL;
        return true;
    }
}

bool GetTop(SqStack *s, ElemType &e)
{
    if (s->next == NULL)
        return false;
    else
    {
        e = s->next->data;
        return true;
    }
}

void DestroyStack(SqStack *&s)
{
    SqStack *p = s, *q = s->next;
    while (q != NULL)
    {
        delete p;
        p = q;
        q = q->next;
    }
    delete p;
}

/*
void trans(char *exp, char postexp[])
{
    int i = 0;
    char e;
    SqStack *Optr;
    InitStack(Optr);
    while (*exp != '\0')
    {
        switch (*exp)
        {
            case '(':
                Push(Optr, *exp);
                exp++;
                break;
            case ')':
                Pop(Optr, e);
                while (e != '(')
                {
                    postexp[i++] = e;
                    Pop(Optr, e);
                }
                exp++;
                break;
            case '+':
            case '-':
                while (!StackEmpty(Optr))
                {
                    GetTop(Optr, e);
                    if (e != '(')
                    {
                        Pop(Optr, e);
                        postexp[i++] = e;
                    }
                    else
                        break;
                }
                Push(Optr, *exp);
                exp++;
                break;
            case '*':
            case '/':
                while (!StackEmpty(Optr))
                {
                    GetTop(Optr, e);
                    if (e == '*' || e == '/')
                    {
                        Pop(Optr, e);
                        postexp[i++] = e;
                    }
                    else
                        break;
                }
                Push(Optr, *exp);
                exp++;
                break;
            default:
                while (*exp >= '0' && *exp <= '9')
                {
                    postexp[i++] = *exp;
                    exp++;
                }
                postexp[i++] = '#';
        }
    }
    while (!StackEmpty(Optr))
    {
        Pop(Optr, e);
        postexp[i++] = e;
    }
    postexp[i] = '\0';
    DestroyStack(Optr);
}
*/

double compvalue(char *postexp)
{
    double d, a, b, c, e;
    SqStack *Opnd;
    InitStack(Opnd);
    while (*postexp != '\0')
    {
        switch(*postexp)
        {
            case '+':
                Pop(Opnd, b);
                Pop(Opnd, a);
                c = a+b;
                Push(Opnd, c);
                break;
            case '-':
                Pop(Opnd, b);
                Pop(Opnd, a);
                c = a-b;
                Push(Opnd, c);
                break;
            case '*':
                Pop(Opnd, b);
                Pop(Opnd, a);
                c = a*b;
                Push(Opnd, c);
                break;
            case '/':
                Pop(Opnd, b);
                Pop(Opnd, a);
                if (b != 0)
                {
                    c = a/b;
                    Push(Opnd, c);
                    break;
                }
                else
                {
                    cout << "除零错误!" << endl;
                    return inf;
                }
            default:
                d = 0;
                while (*postexp >= '0' && *postexp <= '9')
                {
                    d = d*10+*postexp-'0';
                    postexp++;
                }
                Push(Opnd, d);
                break;
        }
        postexp++;
    }
    GetTop(Opnd, e);
    DestroyStack(Opnd);
    return e;
}

STL栈版本

#include <iostream>
#include <stack>

using namespace std;

typedef double ElemType;
const int MaxSize = 100;
const int inf = 0x3f3f3f3f;

void trans(char *exp, char postexp[]);
double compvalue(char *postexp);

int main(void)
{
    char exp[MaxSize];
    char postexp[MaxSize];
    cout << "中缀表达式:";
    scanf("%s", exp);
    trans(exp, postexp);
    cout << "后缀表达式:" << postexp << endl;
    cout << "表达式的值:" << compvalue(postexp) << endl;
    
    
    return 0;
}

void trans(char *exp, char postexp[])
{
    stack<char> Optr;
    int i = 0;
    char e;
    while (*exp != '\0')
    {
        switch (*exp)
        {
            case '(':
                Optr.push(*exp);
                exp++;
                break;
            case ')':
                e = Optr.top();
                Optr.pop();
                while (e != '(')
                {
                    postexp[i++] = e;
                    e = Optr.top();
                    Optr.pop();
                }
                exp++;
                break;
            case '+':
            case '-':
                while (!Optr.empty())
                {
                    if (Optr.top() != '(')
                    {
                        e = Optr.top();
                        postexp[i++] = e;
                        Optr.pop();
                    }
                    else
                        break;
                }
                Optr.push(*exp);
                exp++;
                break;
            case '*':
            case '/':
                while (!Optr.empty())
                {
                    if (Optr.top() == '*' || Optr.top() == '/')
                    {
                        e = Optr.top();
                        postexp[i++] = e;
                        Optr.pop();
                    }
                    else
                        break;
                }
                Optr.push(*exp);
                exp++;
                break;
            default:
                while (*exp >= '0' && *exp <= '9')
                {
                    postexp[i++] = *exp;
                    exp++;
                }
                postexp[i++] = '#';
        }
    }
    while (!Optr.empty())
    {
        e = Optr.top();
        postexp[i++] = e;
        Optr.pop();
    }
    postexp[i] = '\0';
}

double compvalue(char *postexp)
{
    double d, a, b, c, e;
    stack<double> Opnd;
    while (*postexp != '\0')
    {
        switch (*postexp)
        {
            case '+':
                b = Opnd.top();
                Opnd.pop();
                a = Opnd.top();
                Opnd.pop();
                c = a+b;
                Opnd.push(c);
                break;
            case '-':
                b = Opnd.top();
                Opnd.pop();
                a = Opnd.top();
                Opnd.pop();
                c = a-b;
                Opnd.push(c);
                break;
            case '*':
                b = Opnd.top();
                Opnd.pop();
                a = Opnd.top();
                Opnd.pop();
                c = a*b;
                Opnd.push(c);
                break;
            case '/':
                b = Opnd.top();
                Opnd.pop();
                a = Opnd.top();
                Opnd.pop();
                if (b == 0)
                {
                    cout << "除零错误!" << endl;
                    return inf;
                }
                else
                {
                    c = a/b;
                    Opnd.push(c);
                }
                break;
            default:
                d = 0;
                while (*postexp >= '0' && *postexp <= '9')
                {
                    d = d*10+*postexp-'0';
                    postexp++;
                }
                Opnd.push(d);
                break;
        }
        postexp++;
    }
    e = Opnd.top();
    Opnd.pop();
    return e;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值