Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
 }
C++


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]);
}
Posted
Updated 10-Mar-18 0:47am

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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900