Don't get a program logic

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Don't get a program logic

 
0
  #1
Oct 5th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 5th, 2009
nvm about buff deceremting part it won't decerement it only after 2nd time i think but how does ungetch deletes input ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso
 
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
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 ?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
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
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso
 
0
  #6
Oct 7th, 2009
you need to post a question if you want an answer.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
0
  #7
Oct 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training
 
0
  #8
Oct 7th, 2009
anyways thanks for clarifying the function for me
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC