这是我尝试编写一个程序,可以将infix中的任何表达式转换为postfix格式。我将-1分配给顶部以指示堆栈为空。按下时,顶部递增;弹出时,顶部递减。但是,当我键入a+b时,输出仅给我ab而没有+运算符,而当我键入(a+b)时,它表示分段错误。我认为我的堆栈有问题,但是无法弄清楚出了什么问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define SIZE 30

typedef struct Stack
{
    int top;
    int capacity;
    char* storage;

} stack;

int isEmpty(stack* a);
char topelement(stack* a);
char pop(stack* a);
void push(stack* a,char b);
bool isOperand(char a);
int Precedence(char a);
stack* NewStack(char* a);
void InfixPostfix(char* a);

int main(void)
{
    char expression[SIZE];
    printf("Please enter an expression:");
    scanf("%s",expression);
    InfixPostfix(expression);
    printf("\n");

}

int isEmpty(stack* a)
{
    if(a->top==-1)
    {
        return 1;
    }

    else
    return 0;
}

char topelement(stack* a)
{
    return a->storage[a->top];
}

char pop(stack* a)
{
    if(isEmpty(a)==1)
    {
        printf("Stack is Empty\n");
        return '$';
    }

    else
    return a->storage[a->top];
    --(a->top);


}

void push(stack* a,char b)
{
    ++(a->top);
    a->storage[a->top]=b;
}

bool isOperand(char a)
{
    if ( (a >= 'a' && a<= 'z') ||(a>='A' && a<='Z'))
    {
        return 1;
    }

    else
    return 0;

}

int Precedence(char a)
{
    if(a=='+' || a=='-')
    {
        return 1;
    }

    if(a=='*' || a=='/')
    {
        return 2;
    }

    if(a=='^')
    {
        return 3;
    }

    else
    return -1;
}

stack* NewStack(char* a)
{
    stack* b= malloc(sizeof(stack));

    if(b!=NULL)
    {
        b->top=-1;
        b->storage=malloc((strlen(a))*sizeof(char));
        return b;
    }
    else
    return NULL;

}

void InfixPostfix(char* a)
{
    int i; int j=-1;

    stack* b=NewStack(a);

    if(b!=NULL)
    {

        for(i=0;  i<strlen(a) ;i++)
        {
            if(isOperand(a[i]))
            {
                a[++j]=a[i];
            }

            if(a[i]=='(')
            {
                push(b, a[i]);
            }

            if(a[i]==')')
            {
                while(isEmpty(b)==0 && topelement(b)!= '(')
                {
                    a[++j]= pop(b);
                }


            }

            else
            {
                while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
                {
                    a[++j]=pop(b);
                    push(b,a[i]);
                }
            }


        }

        while(isEmpty(b)==0)
        {
            a[++j]=pop(b);
        }

        a[++j]='\0';

        printf("%s",a);

    }


}

最佳答案

除了已经建议的malloc更正之外,还有其他一些要解决的问题。


pop()

    return a->storage[a->top];
    --(a->top);


没有到达最后的代码行;将其更改为return a->storage[a->top--];
InfixPostfix()中,在elseif(a[i]=='(')之前缺少if(a[i]==')')
InfixPostfix()中,循环之后

                while(isEmpty(b)==0 && topelement(b)!= '(')
                {
                    a[++j]= pop(b);
                }


缺少pop(b);-元素'('也必须从堆栈中删除。
InfixPostfix()中,必须从循环中删除push(b,a[i]);

                while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
                {
                    a[++j]=pop(b);
                    push(b,a[i]);
                }


并放置在该循环之后-a[i]中的运算符仅需放入堆栈一次。

关于c - Postfix程序的缀未按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47605776/

10-11 22:34
查看更多