逆波兰表达式(算数表达式求解)


#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
typedef struct Stack {
    int elem[MAX];
    int top;
}Stack;

typedef struct Stack2 {
    int elem[MAX];
    int top;
}Stack2;

//初始化
Stack2 *InitStack2() {
    Stack2 *s;
    s = (Stack2 *)malloc(sizeof(Stack2));
    s->top = -1;
    return s;
}
//判空栈
int Empty2(Stack2 *s) {
    if (s->top == -1) {
        return 1;
    } else {
        return 0;
    }
}
//入栈
void Push2(Stack2 *s, int ch) {
    s->top++;
    s->elem[s->top] = ch;
}
//出栈
void Pop2(Stack2 *s) {
    s->top--;
}
//取栈顶元素
int GetTop2(Stack2 *s) {
    if (Empty2(s)) {
        return 0;//栈空
    } else {
        return s->elem[s->top];
    }
}

//初始化
Stack *InitStack() {
    Stack *s;
    s = (Stack *)malloc(sizeof(Stack));
    s->top = -1;
    return s;
}
//判空栈
int Empty(Stack *s) {
    if (s->top == -1) {
        return 1;
    } else {
        return 0;
    }
}
//入栈
void Push(Stack *s, char ch) {
    s->top++;
    s->elem[s->top] = ch;
}
//出栈
void Pop(Stack *s) {
    s->top--;
}
//取栈顶元素
char GetTop(Stack *s) {
    if (Empty(s)) {
        return 0;//栈空
    } else {
        return s->elem[s->top];
    }
}
//判断是否属于操作符
int In(char ch) {
    char operator[7] = "+-*/()#";
    for (int i = 0; i < 7; i++) {
        if (ch == operator[i])
            return i;
    }
    return -1;
}
//判断优先级
char compare(char ch1, char ch2) {
    char com[7][7] =
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=',' '},
        {'>','>','>','>',' ','>','>'},
        {'<','<','<','<','<',' ','='},
    };
    int i = In(ch1);
    int j = In(ch2);
    return com[i][j];
}
char res[100];
void zhuan(char *s) {
    //printf("表达式: %s\n", s);
    Stack *operatorStack;
    int j = 0;
    //运算符
    operatorStack = InitStack();
    int i = 0;
    while (s[i] != 0) {
        if (In(s[i]) == -1) {
            res[j] = s[i];
            j++;
            i++;
        } else {
            if (Empty(operatorStack) == 1) {
                //printf("+:%c\n", s[i]);
                Push(operatorStack, s[i]);
                i++;
            } else if (s[i] == '(') {
                Push(operatorStack, s[i]);
                i++;
            } else if (s[i] == ')') {
                while (GetTop(operatorStack) != '(') {
                    res[j] = GetTop(operatorStack);
                    j++;
                    Pop(operatorStack);
                }
                Pop(operatorStack);
                i++;
            } else {
                char t = compare(GetTop(operatorStack), s[i]);
                if (t == '<') {
                    Push(operatorStack, s[i]);
                } else {
                    if (s[i] == ')') {
                        while (GetTop(operatorStack) != '(') {
                            res[j] = GetTop(operatorStack);
                            j++;
                            Pop(operatorStack);
                        }
                    } else {
                        while ((t == '>' || t == '=') && Empty(operatorStack) != 1) {
                            //printf("top1:%d\n", operatorStack->top);
                            res[j] = GetTop(operatorStack);
                            j++;
                            Pop(operatorStack);
                            t = compare(GetTop(operatorStack), s[i]);
                        }
                        //printf("+:%c\n", s[i]);
                        Push(operatorStack, s[i]);
                        //printf("验证:%d\n", operatorStack->top);
                    }
                    
                }
                i++;
            }
        }
    }
    while (Empty(operatorStack) != 1) {
        res[j] = GetTop(operatorStack);
        //printf("top2:%d\n", operatorStack->top);
        //printf("%c\n", GetTop(operatorStack));
        j++;
        Pop(operatorStack);
    }
    res[j] = '\0';
    printf("逆波兰表达式(后缀表达式):\n%s\n", res);
}
//1+2*3+(4*5+6)*7
void Cal() {
    //printf("%s\n", res);
    Stack2 *operands;
    operands = InitStack2();
    int i = 0;
    while (res[i] != 0) {
        if (In(res[i]) == -1) {
            res[i] = res[i] - '0';
            //printf("验证:%d\n", res[i]);
            Push2(operands, res[i]);
        } else {
            int a = GetTop2(operands);
            //printf("a:%d\n", a);
            Pop2(operands);
            int b = GetTop2(operands);
            //printf("b:%d\n", b);
            Pop2(operands);
            int c = 0;
            switch (res[i]) {
                case '+':
                        c = a + b;
                        break;
                case '-':
                        c = b - a;
                        break;
                case '/':
                        c = b / a;
                        break;
                case '*':
                        c = a * b;
                        break;
            }
            //printf("c:%d\n", c);
            Push2(operands, c);
        }
        i++;
    }
    printf("结果:%d\n", GetTop2(operands));
}
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("请输入一个表达式: \n");
    char s[20];
    scanf("%s", s);
    //printf("%s", s);
    zhuan(s);
    Cal();
    return 0;
}

验证

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值