鱼C论坛

 找回密码
 立即注册
查看: 1583|回复: 0

[学习笔记] 中缀表达式转后缀表达式(PN-->RPN)

[复制链接]
发表于 2019-1-8 10:40:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

#define STACK_INIT_SIZE 20
#define STACKINCREAMENT 10
#define MAXBUFFER 10

typedef char ElementType;

typedef struct {
        ElementType* base;
        ElementType* top;
        int stackSize;
}sqStack;

void InitStack(sqStack* s);
bool StackEmpty(sqStack* s);
void Push(sqStack* s, ElementType e);
void Pop(sqStack* s, ElementType* e);
bool Priority(char operator1, char operator2);
void Output(sqStack* s);
int main()
{
        sqStack* s = (sqStack*)malloc(sizeof(sqStack*));
        InitStack(s);
        char buf[MAXBUFFER];
        char c, operator_stack;
        c = operator_stack = '0';
        double value;
        while (true) {
                int i = 0;
                while (true) {
                        scanf("%c", &c);
                        if (isdigit(c) || c == '.') {
                                buf[i++] = c;
                        }
                        else if (c == ' ' && i != 0) {
                                buf[i] = '\0';
                                value = atof(buf);
                                printf("%.2lf ", value);
                                break;
                        }
                        else {
                                break;
                        }
                }
                switch (c) {
                case '+':
                case '-':
                case '*':
                case '/':
                        while (true) {
                                if (StackEmpty(s)) {
                                        Push(s, c);
                                        break;
                                }
                                else {
                                        Pop(s, &operator_stack);
                                        if (Priority(c, operator_stack)) {
                                                Push(s, operator_stack);
                                                Push(s, c);
                                                break;
                                        }
                                        else {
                                                printf("%c ", operator_stack);
                                        }
                                }
                        }
                        break;
                case '(':
                        Push(s, c);
                        break;
                case')':
                        Pop(s, &c);
                        while (c != '(') {
                                printf("%c ", c);
                                Pop(s, &c);
                        }
                        break;
                default:
                        break;
                }
                if (c == '#') {
                        break;
                }
        }
        Output(s);
}
void InitStack(sqStack* s) {
        s->top = s->base = (ElementType*)malloc(STACK_INIT_SIZE * sizeof(double));
        if (!s->top) {
                exit(0);
        }
        s->stackSize = STACK_INIT_SIZE;
}
bool StackEmpty(sqStack* s) {
        if (s->top == s->base) {
                return true;
        }
        else {
                return false;
        }
}
void Push(sqStack* s, ElementType e) {
        if (s->top - s->base >= s->stackSize) {
                s->base = (ElementType*)realloc(s->base, s->stackSize + STACKINCREAMENT);
                if (!s->base) {
                        exit(0);
                }
                s->stackSize = s->stackSize + STACKINCREAMENT;
        }
        *(s->top) = e;
        s->top++;

}
void Pop(sqStack* s, ElementType * e) {
        if (StackEmpty(s)) {
                return;
        }
        *e = *--(s->top);
}
bool Priority(char operator_stackTop, char operator_ch) {
        if (((operator_stackTop == '*') || (operator_stackTop == '/') || (operator_stackTop == '+') || (operator_stackTop == '-'))
                && (operator_ch == '(')) {
                return true;
        }
        if (((operator_stackTop == '*') || (operator_stackTop == '/')) && ((operator_ch == '+') || (operator_ch == '-'))) {
                return true;
        }
        else {
                return false;
        }
}
void Output(sqStack* s) {
        char c;
        while (s->top != s->base) {
                Pop(s, &c);
                printf("%c ", c);
        }
}
/*
 * 样例输入: 1 + 2 - 1 * ( ( 3 + 4 ) / 5 - 6 ) + 7 #
 * 输出: 1 2 + 1 3 4 + 5 / 6 - * - 7 +
 */

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-27 06:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表