【问题标题】:Im trying to convert expression infix to postfix,why i'm getting output null insted ab-?我试图将表达式中缀转换为后缀,为什么我得到输出 null insted ab-?
【发布时间】:2021-06-28 05:30:38
【问题描述】:
#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' : format string '%s' requires an argument of type 'char *',但可变参数 1 的类型为 'int' ,来自最后一个printf()。实际上 charchar 被提升为 int 用于可变参数函数。尝试将%s 更改为%c 以匹配类型。

标签: c null infix-notation


【解决方案1】:

你的格式字符串需要一个字符串,但你给它一个字符:

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 中,您调用了 3 次 malloc(),但不调用 free(),因此您的程序会泄漏内存。

  2. 你想要strlen(Infix) + 1,而不是strlen(Infix + 1),它的计算结果为strlen(Infix) - 1

  3. 考虑使用 for() 循环来缩小变量 i 的范围,而不是 while (Infix[i] != '\0')。避免否定并使用 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++];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多