944,087 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 501
  • C RSS
Oct 5th, 2009
0

Don't get a program logic

Expand Post »
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
  1. #include <stdio.h>
  2. #include <stdlib.h> /* for atof() */
  3. #include <ctype.h>
  4. #define MAXOP 100 /* max size of operand or operator */
  5. #define NUMBER '0' /* signal that a number was found */
  6. #define MAXVAL 100 /* maximum depth of val stack */
  7. #define BUFSIZE 100
  8. int sp = 0; /* next free stack position */
  9. double val[MAXVAL]; /* value stack */
  10. int getop(char []);
  11. void push(double);
  12. double pop(void);
  13. /* reverse Polish calculator */
  14. int main(void)
  15. {
  16. int type;
  17. double op2;
  18. char s[MAXOP];
  19. while ((type = getop(s)) != EOF) {
  20. switch (type) {
  21. case NUMBER:
  22. push(atof(s));
  23. break;
  24. case '+':
  25. push(pop() + pop());
  26. break;
  27. case '*':
  28. push(pop() * pop());
  29. break;
  30. case '-':
  31. op2 = pop();
  32. push(pop() - op2);
  33. break;
  34. case '/':
  35. op2 = pop();
  36. if (op2 != 0.0)
  37. push(pop() / op2);
  38. else
  39. printf("error: zero divisor\n");
  40. break;
  41. case '\n':
  42. printf("\t%.8g\n", pop());
  43. break;
  44. default:
  45. printf("error: unknown command %s\n", s);
  46. break;
  47. }
  48. }
  49. return 0;
  50. }
  51. /* push: push f onto value stack */
  52. void push(double f)
  53. {
  54. if (sp < MAXVAL)
  55. val[sp++] = f;
  56.  
  57. else
  58. printf("error: stack full, can't push %g\n", f);
  59. }
  60. /* pop: pop and return top value from stack */
  61. double pop(void) {
  62.  
  63. if (sp > 0)
  64. return val[--sp];
  65. else {
  66. printf("error: stack empty\n");
  67. return 0.0;
  68. }
  69. }
  70.  
  71. int getch(void);
  72. void ungetch(int);
  73. /* getop: get next character or numeric operand */
  74. int getop(char s[])
  75. {
  76. int i, c;
  77. while ( (s[0] = c = getch() ) == ' ' || c == '\t');
  78. s[1] = '\0';
  79. if (!isdigit(c) && c != '.')
  80. return c; /* not a number */
  81. i = 0;
  82. if (isdigit(c)) /* collect integer part */
  83. while (isdigit(s[++i] = c = getch())) ;
  84. if (c == '.') /* collect fraction part */
  85. while (isdigit(s[++i] = c = getch()));
  86. s[i] = '\0';
  87. if (c != EOF)
  88. ungetch(c);
  89. return NUMBER;
  90. }
  91.  
  92.  
  93. char buf[BUFSIZE]; /* buffer for ungetch */
  94. int bufp = 0; /* next free position in buf */
  95. int getch(void) {/* get a (possibly pushed-back) character */
  96. return (bufp > 0) ? buf[--bufp] : getchar() ;
  97. }
  98. void ungetch(int c) /* push character back on input */
  99. {
  100. bufp >= BUFSIZE ? printf("ungetch: too many characters\n") : buf[bufp++] = c ;
  101. }
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
Similar Threads
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 5th, 2009
0
Re: Don't get a program logic
nvm about buff deceremting part it won't decerement it only after 2nd time i think but how does ungetch deletes input ?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 5th, 2009
0
Re: Don't get a program logic
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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Oct 6th, 2009
0
Re: Don't get a program logic
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 ?
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 6th, 2009
0
Re: Don't get a program logic
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
  1. #include <stdio.h>
  2. #include <stdlib.h> /* for atof() */
  3. #include <ctype.h>
  4. #include <math.h>
  5. #define MAXOP 100 /* max size of operand or operator */
  6. #define NUMBER '0' /* signal that a number was found */
  7. #define MAXVAL 100 /* maximum depth of val stack */
  8. #define BUFSIZE 100
  9. int sp = 0; /* next free stack position */
  10. double val[MAXVAL]; /* value stack */
  11. int getop(char []);
  12. void push(double);
  13. double pop(void);
  14. /* reverse Polish calculator */
  15. int main(void)
  16. {
  17. int type;
  18. int i=0,count=0;
  19. double op2;
  20. char s[MAXOP];
  21. while ((type = getop(s)) != EOF) {
  22. switch (type) {
  23. case NUMBER:
  24. push(atof(s));
  25. break;
  26. case '+':
  27. push(pop() + pop());
  28. break;
  29. case '*':
  30. push( pop() * pop() );
  31. break;
  32. case '-':
  33. op2 = pop();
  34. push(pop() - op2);
  35. break;
  36. case '/':
  37. op2 = pop();
  38. if (op2 != 0.0)
  39. push(fmod(pop(),op2));
  40. else
  41. printf("error: zero divisor\n");
  42. break;
  43. case '%':
  44. op2 = pop();
  45. if(op2!=0)
  46. fmod(pop(), op2);
  47. case '\n':
  48. printf("\tanswer is%.8g\n", pop());
  49. break;
  50. default:
  51. printf("error: unknown command %s\n", s);
  52. break;
  53. }
  54. }
  55. return 0;
  56. }
  57. /* push: push f onto value stack */
  58. void push(double f)
  59. {
  60.  
  61. if (sp < MAXVAL) {
  62. val[sp++] = f;
  63. printf("val[sp]=%g",val[sp]);//we gonna use for verifacation
  64. getchar();
  65. }
  66. else
  67. printf("error: stack full, can't push %g\n", f);
  68. }
  69. /* pop: pop and return top value from stack */
  70. double pop(void) {
  71.  
  72. if (sp > 0)
  73. return val[--sp];
  74. else {
  75. printf("error: stack empty\n");
  76. return 0.0;
  77. }
  78. }
  79.  
  80. int getch(void);
  81. void ungetch(int);
  82. /* getop: get next character or numeric operand */
  83. int getop(char s[])
  84. {
  85. int i, c,next;
  86. while ( (s[0] = c = getch() ) == ' ' || c == '\t');
  87. s[1] = '\0';
  88. if (!isdigit(c) && c != '.' && c!='-')
  89. return c; /* not a number */
  90. i = 0;
  91. if(c=='-') {
  92. //save the variable after it
  93. next=getch();//get next character
  94. if(isspace(next))//then its operand
  95. return c;
  96. //we didnt do else coz it will be the same since return will return from the whole function
  97. s[i]=c;//reassign c to negative
  98. c=next;//assign c to next which is a number after the -
  99. s[++i]=c;//now assign next character in the array now to the number
  100. }
  101. if (isdigit(c)) { /* collect integer part */
  102. while (isdigit(s[++i] = c = getch()));
  103. }
  104. if (c == '.') /* collect fraction part */
  105. while (isdigit(s[++i] = c = getch()));
  106. s[i] = '\0';
  107. if (c != EOF)
  108. ungetch(c);
  109. return NUMBER;
  110. }
  111.  
  112.  
  113. char buf[BUFSIZE]; /* buffer for ungetch */
  114. int bufp = 0; /* next free position in buf */
  115. int getch(void) {/* get a (possibly pushed-back) character */
  116. return (bufp > 0) ? buf[--bufp] : getchar() ;
  117. }
  118. void ungetch(int c) /* push character back on input */
  119. {
  120. bufp >= BUFSIZE ? printf("ungetch: too many characters\n") : buf[bufp++] = c ;
  121. }
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
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 7th, 2009
0
Re: Don't get a program logic
you need to post a question if you want an answer.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Oct 7th, 2009
0
Re: Don't get a program logic
i fixed problem was that i incremented sp then used it in printf which made it next time i used the value i used the incremented one which contained rubbish value
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 7th, 2009
0
Re: Don't get a program logic
anyways thanks for clarifying the function for me
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Complier Not Linking Issues
Next Thread in C Forum Timeline: Binary Expression Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC