中缀到postfix转换 [英] Infix to postfix conversion

查看:57
本文介绍了中缀到postfix转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于调试:评估器堆栈在内存中成功保留

但是根本没有推送值!不知道出了什么问题?为什么值不被推入堆栈?知道我在另一个程序中使用了我的堆栈实现并且运行良好!!



我尝试了什么:



 void infixTopostfix(char infix [],char postfix []){
int i,j = 0;
Stack * evaluator = initialize();
for(i = 0; infix [i]; ++ i){
if(isdigit(infix [i])){
postfix [j ++] = infix [i];}
else {
while(!isempty(evaluator)&& precedence(infix [i])< = precedence((char)top(evaluationator)))
{postfix [j ++ ] = pop(评估者);}
push(evaluateator,(int)infix [i]);

}
}
postfix [j ++] ='\'';
printf(%s,postfix);
}

 



my堆栈实现

 #include< stdlib.h> 
#include< stdio.h>
#define SIZE 100
typedef struct
{
int top;
int * items;
}堆栈;
Stack * initialize()
{
Stack * s = malloc(sizeof(Stack));
s-> top = 0;
s-> items =(int *)malloc(SIZE * sizeof(int));
返回s;
}
int isfull(Stack * s)
{
return s-> top> = SIZE?1:0;
}
int isempty(Stack * s)
{
return s-> top == 0?1:0;
}

void push(Stack * s,int value)
{
if(!isfull(s))
s-> items [ S->顶++] =值;
else printf(堆栈已满);
}
int pop(Stack * s)
{
if(!isempty(s))
return s-> items [ - s->最佳];
else printf(stack is empty);
返回404;
}
int top(Stack * s){
if(isempty(s))返回0;
return(s-> items [s-> top]);
}

解决方案

如果您使用过调试器,则表明函数top无法正常工作。 s-> top通常指向当前顶部的一个条目 。因此,最后一行应为:



返回s->项目[s-> top-1];



这可能不是您代码中唯一的错误。它只是伸出来了。使用调试器单步执行整个代码,并观察它是否做得正确。


引用:

但是根本没有推送值!不知道出了什么问题?为什么值不被推入堆栈?



这是不可能的!

如果你制作了这些代码,你必须知道并理解它应该如何工作因为它是编写代码的先决条件,而调试器只显示正在执行的代码。

理解代码未执行预期的位置应该不复杂。 />
,差异在哪里就是bug。



调试器是一个工具,对于任何工具都是如此,你需要学习如何使用它。

调试器不知道应该做什么代码,它从不显示你的错误,它只显示你的代码实际上在做什么,你必须比较它的预期做。当它出现意外行为时,你发现了一个错误。



显示一些代码很不错,但对于我们可以运行的一段自治代码来说更好。


on debugging : evaluator stack is successfully reserved in memory
but values are not pushed at all! don't know what is wrong ? why aren't the values pushed into stack ? knowing that I used my stack implementation in another program and it worked well !!

What I have tried:

void infixTopostfix(char infix[], char postfix[]){
 int i,j=0;
Stack* evaluator = initialize();
 for(i=0; infix[i] ; ++i){
    if(isdigit(infix[i])) {
            postfix[j++] = infix[i];}
    else {
            while(!isempty(evaluator) && precedence(infix[i]) <= precedence((char)top(evaluator)))
                {postfix[j++] = pop(evaluator);}
                 push(evaluator,(int)infix[i]);

        }
                    }
                postfix[j++] = '\0';
                printf("%s",postfix);
 }


my stack implementation

#include<stdlib.h>
#include<stdio.h>
#define SIZE 100
typedef struct
{
    int top;
    int *items;
} Stack;
Stack* initialize()
{
    Stack *s=malloc(sizeof(Stack));
    s->top=0;
    s->items=(int*) malloc(SIZE*sizeof(int));
    return s;
}
int isfull(Stack *s)
{
    return s->top >= SIZE ?1:0 ;
}
int isempty(Stack *s)
{
    return s->top==0?1:0;
}

void push(Stack *s,int value)
{
    if(!isfull(s))
        s->items[s->top++]=value;
    else printf("stack is full");
}
int pop(Stack *s)
{
    if(!isempty(s))
        return s->items[--s->top];
    else printf("stack is empty");
    return 404;
}
int top(Stack* s){
if(isempty(s)) return 0;
return (s->items[s->top]);
}

解决方案

If you had used the debugger you had seen that the function top is not working correctly. s->top normally points one entry above the current top. Hence the last line should read:

return s->items[s->top-1];

And that might be not the only bug in your code. It just stuck out. Use your debugger to single-step through your entire code and watch whether it's doing the right thing.


Quote:

but values are not pushed at all! don't know what is wrong ? why aren't the values pushed into stack ?


This is just impossible!
If you made that code, you must know and understand how it should work because it is a prerequisite to write the code, and the debugger only show you what is doing the code.
It shouldn't be complicated to understand where the code is not doing what is expected.
and that difference is where is the bug.

The debugger is a tool and like for any tool, you need to learn how to use it.
The debugger don't know what is supposed to do the code, it never show you bugs, it only show you what your code is really doing and you have to compare with what it is expected to do. When it behave unexpectedly, you just found a bug.

It is nice to show some code, but it is better to an autonomous piece of code that we can run.


这篇关于中缀到postfix转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆