簡體   English   中英

C:堆棧轉儲錯誤,以將后綴轉換為后綴

[英]C: Stack Dump error for infix to postfix conversion

我已經編寫了以下代碼,用於將中綴表達式轉換為后綴,但顯示運行時錯誤。

錯誤如下:

輸入字符串:a + bc * d 1 [main] infix_to_postfix 7340 cygwin_exception :: open_stackdumpfile:將堆棧跟蹤信息轉儲到infix_to_postfix.exe.stackdump

以下是我的代碼:

#include<stdio.h>
#include<string.h>
#define size 20

struct stack{
    int a[size],top;
    int temp[size], tos;
}s;

// Push operation....
void push(struct stack s,int item){
    if(s.top >= size-1){
        printf("\nStack overflow..\n");
    }
    else{
        s.a[++s.top] = item;
    }
}

// Pop operation....
int pop(struct stack s){
    if(s.top == -1){
        printf("\n..Stack underflow..\n");
}
else {
    return s.a[s.top--];
}
}


// function f starts from here. f returns the precedence value of corresponding symbol.
int f(char symbol){
    if (symbol == '+' || '-')
        return 1;
    if (symbol == '*' || '/')
        return 2;
    if (symbol == '#')
        return 0;
    char c;
for(c='a'; c<='z'; c++){
    if(symbol == c)
        return 3;
}
}

int main(){
s.top = -1;
s.a[++s.top] = '#';

int i = 0;
char input[20], polish[21];
char next, temp;

printf("Input string: ");
scanf("%s", input);
strcat(input, '#');
next = input[i++];

while(next != '\0'){
    while(f(next) > f(s.a[s.top])){
        push(s, next);
        next = input[i++];
    }
    while(f(next) <= f(s.a[s.top])){
        temp = pop(s);
        strcat(polish, temp);
        printf("\nPolish: %s", polish);
    }
}


return 0;
}
strcat(polish, temp);

永遠不會初始化polish因此使用未初始化的數組會導致未定義的行為。 strcat()需要以空值結尾的字符串。

嘗試做類似的事情

char polish[21] = "";

編輯:

正如@BLUEPIXY指出的, strcat()期望使用char *不是char

暫無
暫無

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

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