簡體   English   中英

我試圖將表達式中綴轉換為后綴,為什么我得到 output null insted ab-?

[英]Im trying to convert expression infix to postfix,why i'm getting output null insted ab-?

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

struct stack {
    int size;
    int top;
    char *arr;
}; 
    
int isEmpty(struct stack *ptr) {
    if (ptr->top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
} 
    
int isFull(struct stack *ptr) {
    if (ptr->top == ptr->size - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
    
int push(struct stack *ptr, int val) {
    if (isFull(ptr))
    {
        return -1;
    }
    else
    {
        ptr->top++;
        ptr->arr[ptr->top] = val;
    }

    return 1; 
}

int pop(struct stack *ptr) {
    if (isEmpty(ptr))
    {
        return -1;
    }
    else
    {
        ptr->top--;
        int val = ptr->arr[ptr->top];
        return val;
    } 
}

int stackTop(struct stack *ptr) {
    return ptr->arr[ptr->top];
}

int precedence(char ch) {
    if (ch == '/' || ch == '*')
    {
        return 3;
    }
    else if (ch == '+' || ch == '-')
    {
        return 2;
    }
    else
    {
        return 0;
    }
}

int isOperand(char ch) {
    if (ch == '+' || ch == '-' || ch == '/' || ch == '*')
    {
        return 1;
    }
    else
    {
        return 0;
    } 
}

char IntoPostFix(char *Infix) {
    struct stack *s = (struct stack *)malloc(sizeof(struct stack));
    s->top = -1;
    s->size = 100;
    s->arr = (char *)malloc(s->size * sizeof(char));
    char *postfix = (char *)malloc(strlen(Infix + 1) * sizeof(char));
    int i = 0; //value at intfix;
    int j = 0; //store into post fix
    while (Infix[i] != '\0')
    {
        if (!isOperand(Infix[i]))
        {
            postfix[j] = Infix[i];
            i++;
            j++;
        }
        else
        {
            if (precedence(Infix[i]) > precedence(stackTop(s)))
            {
                push(s, Infix[i]);
                i++;
            }
            else
            {
                postfix[j] = pop(s);
                j++;
            }
        }
    }
    while (!isEmpty(s))
    {
        postfix[j] = pop(s);
        j++;
    }
    postfix[j] = '\0';
    return postfix[j]; 
} 

int main() {
    char *Infix = "a-b";
    printf("PostFix is : %s\n ", IntoPostFix(Infix));
    return 0;
}

您格式化字符串需要一個字符串,但您給它一個字符:

printf("PostFix is : %s\n ", IntoPostFix(Infix)); 

更改簽名並返回后綴而不是 '\0':

char *IntoPostFix(char *Infix) {
...
   return postfix;
}

它現在打印“ab\n”,也就是說,我們正確處理了'a','b',並且'-'被壓入堆棧。 pop(s) return 0 你在返回值之前遞減 top :

ptr->top--;
int val = ptr->arr[ptr->top];
return val;

而是這樣做:

return ptr->arr[ptr->top--];

瞧:

PostFix is : ab-

獎金回合:

  1. 在 IntoPostFix 中,您調用malloc() 3 次並且不調用free() ,因此您的程序會泄漏 memory。

  2. 而不是strlen(Infix + 1)評估為strlen(Infix) - 1你想要strlen(Infix) + 1

  3. 而不是while (Infix[i] != '\0')考慮使用 for() 循環來減少變量 i 的 scope。 避免否定並使用 continue 來減少分支噪音。 如果你使用這種壓縮換行方式,你會在屏幕上一次獲得大約兩倍的代碼:

for(int i = 0; Infix[i]; ) {
    if(isOperand(Infix[i])) {
        if(precedence(Infix[i]) > precedence(stackTop(s))) {
            push(s, Infix[i++]);
        } else {
            postfix[j++] = pop(s);
        }
        continue;
     }
     postfix[j++] = Infix[i++];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM