hey i m here supposed to extend calculator of K&R to support getline i made it but i dunno everytime i keep getting 0 poped not the numbers itself i debuged it i couldn't find where the bug is so hopefully someone will tell me what i m doing wrong here

#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <ctype.h>
#include <math.h>
#include <string.h>
#define ClearStack(n) n=0 //which makes just the external variable count var point to first member then the elements will get overwritten
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
#define MAXVAL 100 /* maximum depth of val stack */
#define BUFSIZE 100
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
int getline(char *s) {
    int c;
    //we first get line then stop at first new line encounter or at first operand then we reparse the shit
    for(int count=0;(c=getchar())!=EOF;count++) {
         s[count]=c;
        if(c=='\n') {
            s[count]='\0';//we forgot to end the string so thats y shit happened
            return true;
        }
    }
    //it means loop ended by EOF which means we need to return false
    return false; //means EOF is reached mate :D we should now quit the loop
}
void push(double f) {
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
}
double pop(void) {
    if (sp > 0)
        return val[sp--];
    else {
        printf("error: stack empty\n");
        return 0.0;
    }
}
int GetNext(char *s,int *num) {
    double x;
    x=atof(s + *num);//here i didn't do push in main function since it alrdy pushes here anyways but working with ptrs
    if(x>0) //then we catched a number let's push it now
        push(x);
    while(*s) {
        if(isdigit(*s)) {//stop at first digit encountered so we read that with GetNext Operator
            while(isdigit(*s)) {  //which will end at first space assuming we rely on user entering spaces after numbers
                (*num)++,s++;//so it start atofing at next stop we made :P
            }
            ++(*num);
            return 'D';//which will lead to a default case
        }
        if(strchr("+*/%-",*s)) {
            ++(*num);//so it starts at line after the sign
            return *s;
        }
        s++;(*num)++;
    }
    return false;//end of string is reached should print the result
}
int main(void)
{
    int type;
    int i=0,count=0;
    double pop2,res;
    char s[MAXOP];
    while(getline(s)) {//should quit if it's other value other than 0
        while((type=GetNext(s+count,&count))) {//will return false once new line is reached or we reached end of the string (....)
            printf("line 74 yo mate s till now is %s and count=%d\n",s+count,count);//this debug shows nothing wrong !!!
            switch(type) {//we used here continue to continue getting new shit untill user enter new line which will quit the while loop anyways
                case '+':
                    push(pop() + pop());
                    continue;
                case '*':
                    push(pop() * pop());
                    continue;
                case '-':
                    pop2=pop();//which will get last variable
                    push(pop() - pop2);
                    continue;
                case '/':
                    pop2=pop();
                    if(pop2) //if not zero division
                        push(pop() / pop2);
                    else
                        printf("Error Zero Division: ");
                    continue;
                default:
                        continue;/*default case which means a number we will keep adding shit untill there is an operand*/
            }
            //we will quit when we encounter '\0' anyways
        }
        printf("line 101 result of expression %s is : %d\n",s,pop());//i think which will pop last result
    }
    return 0;
}

note i used some printf and getchars to debug the source so i don't still couldnt catch the error

Recommended Answers

All 6 Replies

I think you have to rearrange this into polish notation. For example, if I enter "100 + 2" when the + operator is reached your program attempts to pop two values off the stack, then in fact there is only one value (100).

yes but polish notation of K&R is 2 4 + not 2 + 4

thats y i implemented it according

You would have to take the input "2 + 4" and rearrange it in reverse polish notation format. google for it and you will find examples.

yes i will add that but i don't want to rewrite the hole thing so i may never know what went wrong here so i won't rlly improve so that's y i want to know what i m doing wrong here so i will be able to avoid that next time i m coding something new also i m having a bit hardtime with the forum it's lagging bad

i fixed it problem was in the increment line 68 coz i used to postfix increment it and also there was a bug

#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <ctype.h>
#include <math.h>
#include <string.h>
#define ClearStack(n) n=0 //which makes just the external variable count var point to first member then the elements will get overwritten
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
#define MAXVAL 100 /* maximum depth of val stack */
#define BUFSIZE 100
int sp = 0; /* next free stack position */
int val[MAXVAL]; /* value stack */
int getop(char []);
void push(int);
int pop(void);
/* reverse Polish calculator */
int getline(char *s) {
    int c;
    //we first get line then stop at first new line encounter or at first operand then we reparse the shit
    for(int count=0;(c=getchar())!=EOF;count++) {
         s[count]=c;
        if(c=='\n') {
            s[count]='\0';//we forgot to end the string so thats y shit happened
            return true;
        }
    }
    //it means loop ended by EOF which means we need to return false
    return false; //means EOF is reached mate :D we should now quit the loop
}
void push(int f) {
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
}
int pop(void) {
    int temp=sp;
    if (sp > 0) {
        //printf("pop() function val[--sp] line 39 %d and pop again is %d\n",val[--temp],val[temp-2]);
        //getchar();
        return val[--sp];
    }
    else {
        printf("error: stack empty\n");
        return 0;
    }
}
int GetNext(char *s,int *num) {//problem was here coz i was doing alrdy in main function s + count which then here added again :P
    int x;
    x=atoi(s);//get atoi according to num
    if(x>0) {
        push(x);
        //printf("okay we pushed = %d sp = %d\n",x,sp);//okay here right result so what's wrong !!!!
        //getchar();
    }
    while(*s) {
        if(isdigit(*s)) {//stop at first digit encountered so we read that with GetNext Operator
            while(isdigit(*s)) { //which will end at first space assuming we rely on user entering spaces after numbers
                (*num)++,s++;//so it start atofing at next stop we made :P
            }
            //printf("num is %d\n and string + num is %s",*num,s+*num);
            //getchar();
            return 'D';//which will lead to a default case
        }
        if(strchr("+*/%-",*s)) {
            //printf("we gonna return now %c",*s);
            //getchar();
            (*num)++;//problem was here so next time it's gonna be used it will get in that num
            return *s;
        }
        s++;(*num)++;
    }
    return false;//end of string is reached should print the result
}
int main(void)
{
    int type;
    int i=0,count=0;
    int pop2,pop1;
    char s[MAXOP];
    while(getline(s)) {//should quit if it's other value other than 0
        while((type=GetNext(s+count,&count))) {//will return false once new line is reached or we reached end of the string (....)
            switch(type) {//we used here continue to continue getting new shit untill user enter new line which will quit the while loop anyways
                case '+':
                    push( pop() + pop() );
                    //printf("mate sp is now %d\n",sp);
                    continue;
                case '*':
                    push( pop() * pop() );
                    continue;
                case '-':
                    pop2=pop();//which will get last variable
                    push( pop() - pop2 );
                    continue;
                case '/':
                    pop2=pop();
                    if(pop2) //if not zero division
                        push(pop() / pop2);
                    else
                        printf("Error Zero Division: ");
                    continue;
                default:
                        continue;/*default case which means a number we will keep adding shit untill there is an operand*/
            }
            //we will quit when we encounter '\0' anyways
        }
        printf("line 101 result of expression %s is : %d\n",s,pop());//i think which will pop last result
        count=0;
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.