通过栈实现的中缀表达式转换后缀表达式(不完整)

这一份代码并没有考虑括号内有乘除的情况,先做记录

#include "stdio.h"

#define max_size 100

struct z {
    char data[max_size];
    int top;
} Stack;

int pop(struct z* Stack, char* s) {
    if (Stack->top == -1)
        return 0;
    Stack->top--;
    *s = Stack->data[Stack->top];
    // printf("pop top is %d\n",Stack->top);
    // printf("pop is %c\n",Stack->data[Stack->top]);
    // printf("pop is %c\n",*s);
    return 1;
}

int push(struct z* Stack, char s) {
    if (Stack->top >= max_size)
        return 0;

    Stack->data[Stack->top] = s;
    Stack->top++;
    // printf("push %c\n",Stack->data[Stack->top]);
    return 1;
}

void my_print(struct z Stack) {
    for (int i = 0; i < Stack.top; i++)
        printf("DATA IS %c\n", Stack.data[i]);
}

void my_print_c(char c[],int len)
{
    printf("DATA is :");
    for (int i = 0; i < len; i++)
        printf("%c", c[i]);
    printf("\n");
}

int max_fu(struct z Stack) {
    for (int i = 0; i <= Stack.top-1; i++) {
        if(Stack.data[i] == '(')
            return 0 ;
        if (Stack.data[i] == '*' || Stack.data[i] == '/') {
            return 1;
        }
    }
    return 0;
}

int main() {
    Stack.top = 0;
    char a[] = "9+(3-1*3)*3+10/2";
    char b[100] = {0};
    int c = 0;
    for (int i = 0; i < sizeof(a); i++) {
        if (a[i]!= '\0') {
            if (a[i] >= '0' && a[i] <= '9') {
                b[c] = a[i];
                c++;
            } 
            else if (a[i]==')') {
                {
                    while (Stack.data[Stack.top-1] != '(') {
                        pop(&Stack, &b[c]);
                        c++;
                    }
                    pop(&Stack, &b[99]);
                    b[99]='0';
                } 
            }
            else {
                if (max_fu(Stack)==1) {
                    printf("%c\n",a[i]);
                    while (Stack.top != 0) {
                        pop(&Stack, &b[c]);
                        c++;
                    }
                    push(&Stack, a[i]);
                }
                else {
                    push(&Stack, a[i]);
                 //   printf("%c\n",a[i]);
                }
                //my_print_c(b,sizeof(b));
                // printf("%c\n",b[i]);
                //my_print(Stack);
            }
        } else {
            while (Stack.top != 0) {
                pop(&Stack, &b[c]);
                c++;
            }
           // printf("times\n");
        }
    }

    printf("%s\n", b);
    my_print(Stack);
    // printf("DATA is %s\n",b);
    return 0;
}

修改了代码结构,目前支持加减乘除的全体运算

#include "stdio.h"

#define max_size 100

struct z {
    char data[max_size];
    int top;
} Stack;

int pop(struct z* Stack, char* s) {
    if (Stack->top == -1)
        return 0;
    Stack->top--;
    *s = Stack->data[Stack->top];
    // printf("pop top is %d\n",Stack->top);
    // printf("pop is %c\n",Stack->data[Stack->top]);
    // printf("pop is %c\n",*s);
    return 1;
}

int push(struct z* Stack, char s) {
    if (Stack->top >= max_size)
        return 0;

    Stack->data[Stack->top] = s;
    Stack->top++;
    // printf("push %c\n",Stack->data[Stack->top]);
    return 1;
}

void my_print(struct z Stack) {
    for (int i = 0; i < Stack.top; i++)
        printf("DATA IS %c\n", Stack.data[i]);
}

void my_print_c(char c[], int len) {
    printf("DATA is :");
    for (int i = 0; i < len; i++)
        printf("%c", c[i]);
    printf("\n");
}

int max_fu(struct z Stack) {
    for (int i = 0; i <= Stack.top - 1; i++) {
        if (Stack.data[i] == '*' || Stack.data[i] == '/') {
            return 1;
        }
    }
    return 0;
}

int main() {
    Stack.top = 0;
    char a[] = "2*(9+6/3-5)+4";
    char b[100] = {0};
    int c = 0;
    char temp = '0';
    for (int i = 0; i < sizeof(a); i++) {
        if (a[i] != '\0') {
            if (a[i] >= '0' && a[i] <= '9') {
                b[c] = a[i];
                c++;
            } else {
                if (a[i] == '(') {
                    push(&Stack, a[i]);
                } else {
                    if (a[i] == ')') {
                        while (Stack.data[Stack.top - 1] != '(') {
                            pop(&Stack, &temp);
                            b[c] = temp;
                            c++;
                        }
                        pop(&Stack, &temp);
                    } else {
                        if (a[i] == '+' || a[i] == '-') {
                            if (max_fu(Stack)) {
                                while (Stack.data[Stack.top - 1] != '(' &&
                                       Stack.top != 0) {
                                    pop(&Stack, &temp);
                                    b[c] = temp;
                                    c++;
                                }
                            }
                            push(&Stack,a[i]);
                        } else {
                            push(&Stack, a[i]);
                        }
                    }
                }
            }

        } else {
            while (Stack.top != 0) {
                pop(&Stack, &b[c]);
                c++;
            }
        }
    }
    my_print_c(b, sizeof(b));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值