| | |
Don't get a program logic
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hello here in k&r excerise of primitive calculator i get it and all but there something i think its weird and i don't somewhat get it here the getch and ungetch functions i don't get it's routine
here first time let's say we read first time it will decerment buffp to -1 which will get undefined result how does all all come together then and in ungetch which suppose to push back character on input all it's doing is just copying the character i don't see any deletion here ! .... I hope someone clarify this for me
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /* for atof() */ #include <ctype.h> #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 main(void) { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; } /* push: push f onto value stack */ void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f); } /* pop: pop and return top value from stack */ double pop(void) { if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; } } int getch(void); void ungetch(int); /* getop: get next character or numeric operand */ int getop(char s[]) { int i, c; while ( (s[0] = c = getch() ) == ' ' || c == '\t'); s[1] = '\0'; if (!isdigit(c) && c != '.') return c; /* not a number */ i = 0; if (isdigit(c)) /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())); s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; } char buf[BUFSIZE]; /* buffer for ungetch */ int bufp = 0; /* next free position in buf */ int getch(void) {/* get a (possibly pushed-back) character */ return (bufp > 0) ? buf[--bufp] : getchar() ; } void ungetch(int c) /* push character back on input */ { bufp >= BUFSIZE ? printf("ungetch: too many characters\n") : buf[bufp++] = c ; }
0
#3 Oct 5th, 2009
when you 'getch' you're taking the next character off of the input queue for processing.
this assumes once you've processed it, you're done with it, and will (probably) eventually want to get another character off the input queue.
'ungetch' puts that input back on the input queue.
this effectively allows you to "peek" at the input queue and know what the next character is without destroying it, so that it can be used again at some other point.
this assumes once you've processed it, you're done with it, and will (probably) eventually want to get another character off the input queue.
'ungetch' puts that input back on the input queue.
this effectively allows you to "peek" at the input queue and know what the next character is without destroying it, so that it can be used again at some other point.
0
#4 Oct 6th, 2009
okay thanks i get it but there isn't the order of evulatution for
while (isdigit(s[++i] = c = getch())) ; is right to left ?
like for example after we enter at test if(c=='.') shouldnt it quit coz
isdigit(s[++i] contains nothing or coz i m doing it inside the function i m assigning that ++i to the character that will get read ?
while (isdigit(s[++i] = c = getch())) ; is right to left ?
like for example after we enter at test if(c=='.') shouldnt it quit coz
isdigit(s[++i] contains nothing or coz i m doing it inside the function i m assigning that ++i to the character that will get read ?
0
#5 Oct 6th, 2009
okay i understood it but i got other problem now its not acctually a problem but the exerise to add - i got it to work aswell but when i added couple of printf it didn't work this is weird
line 63 and 64 when i removed the printf debug there there was no problem is somehow printf altered the memory of val double? or what
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /* for atof() */ #include <ctype.h> #include <math.h> #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 main(void) { int type; int i=0,count=0; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push( pop() * pop() ); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(fmod(pop(),op2)); else printf("error: zero divisor\n"); break; case '%': op2 = pop(); if(op2!=0) fmod(pop(), op2); case '\n': printf("\tanswer is%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; } /* push: push f onto value stack */ void push(double f) { if (sp < MAXVAL) { val[sp++] = f; printf("val[sp]=%g",val[sp]);//we gonna use for verifacation getchar(); } else printf("error: stack full, can't push %g\n", f); } /* pop: pop and return top value from stack */ double pop(void) { if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; } } int getch(void); void ungetch(int); /* getop: get next character or numeric operand */ int getop(char s[]) { int i, c,next; while ( (s[0] = c = getch() ) == ' ' || c == '\t'); s[1] = '\0'; if (!isdigit(c) && c != '.' && c!='-') return c; /* not a number */ i = 0; if(c=='-') { //save the variable after it next=getch();//get next character if(isspace(next))//then its operand return c; //we didnt do else coz it will be the same since return will return from the whole function s[i]=c;//reassign c to negative c=next;//assign c to next which is a number after the - s[++i]=c;//now assign next character in the array now to the number } if (isdigit(c)) { /* collect integer part */ while (isdigit(s[++i] = c = getch())); } if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())); s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; } char buf[BUFSIZE]; /* buffer for ungetch */ int bufp = 0; /* next free position in buf */ int getch(void) {/* get a (possibly pushed-back) character */ return (bufp > 0) ? buf[--bufp] : getchar() ; } void ungetch(int c) /* push character back on input */ { bufp >= BUFSIZE ? printf("ungetch: too many characters\n") : buf[bufp++] = c ; }
![]() |
Similar Threads
- Program crashes when I use SetWindowText(); (C++)
- how to write a program for power (C)
- Dice rolling program using graphic (C)
- text program help needed (C)
- Tic-Tac-Toe (C++)
- Tic-Tac-Toe Game (Visual Basic 4 / 5 / 6)
- Help (C++)
- Please explain debugging (Java)
- Need direction on how start this program (C++)
- Need direction on how start this programm (C++)
Other Threads in the C Forum
- Previous Thread: To Implement Predictive Parsing in C
- Next Thread: regarding to C error
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






