栈的应用-中缀表达式转后缀表达式及后缀表达式计算

本文详细介绍栈数据结构的实现及应用,包括初始化、判空、入栈、出栈等基本操作,通过具体实例展示了如何使用栈进行括号匹配检查、中缀表达式转后缀表达式以及后缀表达式的求值过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include<stdio.h>
using namespace std;
#define MaxSize 100
typedef char ElemType;
typedef struct SqStack {
    ElemType data[MaxSize];
    int top;
}SqStack;

//初始化
void InitStack(SqStack &S) {
    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.data[++S.top] = e;
    return true;
}

///出栈
bool Pop(SqStack& S,ElemType &e) {
    if (S.top == -1) {
        return false;
    }
    e = S.data[S.top--];
    return true;
}

//获取栈顶元素
bool GetTop(SqStack S, ElemType& e) {
    if (S.top == -1) {
        return false;
    }
    e = S.data[S.top];
    return true;
}

bool bracketCheck(char str[], int length) {
    ElemType data[MaxSize];
    int top = -1;
    for (int i = 0; i < length; i++) {
        if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
            data[++top] = str[i];
        }
        else {
            if (top == -1) {
                return false;
            }
            ElemType e = data[top--];
            if (!(e == '(' && str[i] == ')' || e == '[' && str[i] == ']' || e == '{' && str[i] == '}')) {
                return false;
            }
        }
    }
    return top == -1;
}
char* parseToSuffixExpression(char* str) {
    SqStack S;
    InitStack(S);
    int len = strlen(str);
    char* res = (char*)malloc(sizeof(char) * len);
    int k = 0;
    for (int i = 0; i < len; i++) {
        if (str[i] >= '0' && str[i] <= '9') {
            res[k++] = str[i];
        }
        else if (str[i] == '(') {
            Push(S, str[i]);
        }
        else if (str[i] == ')') {
            char e;
            while (!StackEmpty(S)) {
                Pop(S, e);
                if (e == '(') {
                    break;
                }
                res[k++] = e;
            } 
        }
        else {
            if (str[i] == '+' || str[i] == '-') {
                char e;
                GetTop(S, e);
                while (!StackEmpty(S)&&e != '(') {
                    Pop(S, e);
                    res[k++] = e;
                    GetTop(S, e);
                }
                Push(S,str[i]);
            }
            else if (str[i] == '*' || str[i] == '/') {
                char e;
                GetTop(S, e);
                while (!StackEmpty(S)&&(e == '*' || e == '/')&&e!='(') {
                    Pop(S, e);
                    res[k++] = e;
                    GetTop(S, e);
                }
                Push(S, str[i]);
            }
        }
    }
    char  e;
    while (!StackEmpty(S)) {
        Pop(S, e);
        res[k++] = e;
    }
    res[k] = '\0';
    return res;
}

int evalRPN(char* str) {
    SqStack S;
    InitStack(S);
    int res = 0;
    for (int i = 0; i < strlen(str); i++) {
        if (str[i] >= '0' && str[i] <= '9') {
            Push(S, str[i]);
        }
        else {
            char num1, num2;
            Pop(S, num1);
            Pop(S, num2);
            if (str[i] == '+') {
                res = (num2 - '0') + (num1 - '0');
            }
            if (str[i] == '-') {
                res = (num2 - '0') - (num1 - '0');
            }
            if (str[i] == '*') {
                res = (num2 - '0') * (num1 - '0');
            }
            if (str[i] == '/') {
                res = (num2 - '0') / (num1 - '0');
            }
            Push(S, res + '0');
        }
    }
    return res;
}
int main()
{
    char str[] = "1+2-3*4/(4-3*2)";
    char *str1 = parseToSuffixExpression(str);
    printf("%d\n", evalRPN(str1));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Baal Austin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值