Search code examples
cstackprefix

Code stops working after pushing any number over 127 on Stack Top


Program in C!

For my homework I had to make a program that finds the value of arithmetic expression given in prefix, by using ADT Stack. The input is a string, for example '- + 5 12 * 7 3', and the output would be '-4'.

I solved my homework and I thought that everything worked fine. But for some reason, when I push the number on top of the stack and if the number is over or equal to 128, or lower or equal to -128, the number completely changes once pushed on the Stack. For example, if I push 129 on the stack, when I retrieve the number from the top, it has changed into -127.

The program has over 150 lines of code so I don't know how helpful would it be if I posted it here, I'm just curious if somebody has an idea why is this happening.

(Here's a brief idea of my program: input is a string where numbers and operaters are separated with blanks.

The program that evaluates the infix goes like this: start from the char at the end of the string, and go from end to start. If char is number, first find the whole number (until the blank) and then push it on stack(as int, not as char). If char is operater, remove last two numbers from stack and do the operation on them, and then push it back on stack. The last number on stack is the result.

I know this is really vague so if the whole program would help more, I will post it. All the operations are correct, I checked it, the problem specifically arises once I put them on stack. Also, I implemented string using pointers.)

EDIT: Instead of '4' for output, I changed it into '-4'. My bad!

EDIT: The code: (Also, I used int for my data type.)

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

typedef struct _element{
    int c;
    struct _element *next;
}element;

typedef element *stack;

void StMakeNull(stack *Sp){
    *Sp=NULL;
}

int StEmpty(stack S){
    if(S==NULL) return 1;
    else return 0;
}

void StPush(stack *Sp, char d){
    element *temp;
    temp=(*Sp);
    (*Sp)=(element*)malloc(sizeof(element));
    (*Sp)->c=d;
    (*Sp)->next=temp;
}

void StPop(stack *Sp){
    if(StEmpty(*Sp)) exit(202);
    else{
        element *temp;
        temp=(*Sp);
        (*Sp)=(*Sp)->next;
        free(temp);
    }
}

char StTop(stack S){
    if(StEmpty(S)) exit(202);
    return S->c;
}

int Jel_broj(char c){
    int d=c;
    if(d>=48 && d<=57)
        return 1;
    return 0;
}

int Jel_operator(char c){
    if(c=='+') return 1;
    else if(c=='-') return 2;
    else if(c=='*') return 3;
    else if(c=='/') return 4;
    else if(c=='^') return 5;
    return 0;
}

int pot(int n, int k){
    int l=1;
    while(k>0){
        l*=n;
        k--;
    }
    return l;
}

void izracunaj(char* niz, int n){
    int broj=0, pomocni, j, nn, b1, b2;
    stack S;
    StMakeNull(&S);
    while(n>=0){
        if(Jel_broj(niz[n])){
            broj=0; j=0; nn=n;
            while(Jel_broj(niz[nn])){
                j++;
                nn--;
            }
            nn=j;
            while(Jel_broj(niz[n-j+1])){
                pomocni=niz[n-j+1];
                broj=broj*10+(pomocni-'0');
                j--;
            }
            StPush(&S, broj);
            n=n-nn+1;
        }
        else if(Jel_operator(niz[n])){
            if(Jel_operator(niz[n])==1){
                b1=StTop(S);
                StPop(&S);
                b2=StTop(S);
                StPop(&S);
                StPush(&S, b1+b2);
            }
            else if(Jel_operator(niz[n])==2){
                b1=StTop(S);
                StPop(&S);
                b2=StTop(S);
                StPop(&S);
                StPush(&S, b1-b2);
            }
            else if(Jel_operator(niz[n])==3){
                b1=StTop(S);
                StPop(&S);
                b2=StTop(S);
                StPop(&S);
                StPush(&S, b1*b2);
            }
            else if(Jel_operator(niz[n])==4){
                b1=StTop(S);
                StPop(&S);
                b2=StTop(S);
                StPop(&S);
                StPush(&S, b1/b2);
            }
            else if(Jel_operator(niz[n])==5){
                b1=StTop(S);
                StPop(&S);
                b2=StTop(S);
                StPop(&S);
                StPush(&S, pot(b1,b2));
            }
        }
        n--;
    }
    printf("%d", StTop(S));
}

int main(){
    char *niz=NULL;
    int n=0;
    char c;
    while(1){
        scanf("%c", &c);
        if(c=='\n'){
            niz=(char*)realloc(niz, (++n)*sizeof(char));
            niz[n-1]='\0';
            break;
        }
        niz=(char*)realloc(niz, (++n)*sizeof(char));
        niz[n-1]=c;
    }
    izracunaj(niz,n-2);
    return 0;
}

Solution

  • You may be storing the values as an int, but they are being truncated to char values as you put them in and out of the stack.

    void StPush(stack *Sp, char d)
    char StTop(stack S)