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