中缀表达式转后缀表达式

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define ADD(x) (x == '+')
#define SUB(x) (x == '-')
#define ADD_SUB(x) (ADD(x) || SUB(x)) /* add or subtract*/
#define DIV(x) (x == '/')
#define MUL(x) (x == '*')
#define DIV_MUL(x) (DIV(x) || MUL(x)) /* divison or multiple*/

#define OPERATOR(x) (DIV_MUL(x) || ADD_SUB(x)) /* operator  */

#define LP(x) (x == '(') /* left parentheses*/
#define RP(x) (x == ')') /* right parentheses*/
#define PAREN(x) (LP(x) || RP(x)) /* parentheses*/

#define OPSIGN(x) (PAREN(x) || OPERATOR(x))

void die(char *err)
{
    perror(err);
    exit(EXIT_FAILURE);
}

struct stack;
typedef struct stack stack;
typedef struct stack* stackptr;

struct stack{
    char oprand;
    stackptr next;
};

void push(stackptr *top, char oprand);
char pop(stackptr* top);
int empty(stackptr top);

void popall(stackptr *top);
void list(stackptr top)
{
    while(top) {
        printf("%3c", top->oprand);
        top = top->next;
    }
    printf("\n");
}

int main(void)
{
    stack *top = NULL;
    char op;
    while((op = getchar()) != '\n') {
        if (isdigit(op)) {
            printf("%c", op);
            continue;
        }

        if(OPERATOR(op))
            putchar(' ');
        if (DIV_MUL(op)) {
            if (empty(top)) {
                push(&top, op);
            } else if (ADD_SUB(top->oprand)) {
                push(&top, op);
            } else if (DIV_MUL(top->oprand)) {
                printf("%c ", pop(&top));
                push(&top, op);
            } else {
                push(&top, op);
            }
        }

        if (ADD_SUB(op)) {
            if (empty(top)) {
                push(&top, op);
            } else if (DIV_MUL(top->oprand)) {
                while(top && OPERATOR(top->oprand)) {
                    printf("%c ", pop(&top));
                }
                push(&top, op);
            } else if (ADD_SUB(top->oprand)) {
                printf("%c ", pop(&top));
                push(&top, op);
            } else {
                push(&top, op);
            }
        }

        if (PAREN(op)) {
            if (empty(top) || LP(op)) {
                push(&top, op);
            } else if (RP(op)) {
                putchar(' ');
                while(top && OPERATOR(top->oprand)) {
                    printf("%c ", pop(&top));
                };
                pop(&top); /* pop left parentheses */
            }
        }
    }
    popall(&top);
    putchar('\n');
    return 0;
}

void push(stackptr *top, char oprand)
{
    stackptr item = malloc(sizeof(stack));
    if (item == NULL)
        die("push() failed");

    item->oprand = oprand;
    item->next = *top;
    *top = item;
}

char pop(stackptr *top)
{
    if (*top == NULL)
        die("pop() failed");

    stackptr item = *top;
    char oprand = item->oprand;
    *top = item->next;

    free(item);
    return oprand;
}

void popall(stackptr *top)
{
    putchar(' ');
    while(*top) {
        printf("%c ", pop(top));
    }
}

int empty(stackptr top)
{
    return top == NULL;
}

请添加图片描述

请指示

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值