Advertisement
Guest User

RPC ex 4.3 K&R

a guest
Jun 2nd, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <mylib.c>
  2. #include <math.h>
  3.  
  4. #define MAXOP 100
  5. #define NUMBER '0'
  6.  
  7. #define BUFSIZE 100
  8.  
  9. char buf[BUFSIZE];
  10. int bufp = 0;
  11.  
  12. int sp = 0; /* next free stack position */
  13.  
  14. /* get a (possibly pushed back) character, */
  15. int
  16. getch(void)
  17. {
  18. return (bufp > 0) ? buf[--bufp] : getchar();
  19. }
  20.  
  21. /* push character back to input */
  22. void ungetch(int c)
  23. {
  24. if(bufp >= BUFSIZE)
  25. printf("ungetch: to many characters\n");
  26. else
  27. buf[bufp++] = c;
  28. }
  29.  
  30. // PUSH & POP
  31.  
  32. #define MAXVAL 100 /* maximum depth of val stack */
  33. double val[MAXVAL]; /* value stack */
  34.  
  35. /* push on top of the stack*/
  36.  
  37. void
  38. push(double f)
  39. {
  40. if(sp < MAXVAL)
  41. val[sp++] = f;
  42. else
  43. printf("error: stack if full, can't push %g\n", f);
  44. }
  45.  
  46. /* extract end return a value on top of the stack*/
  47. double
  48. pop(void)
  49. {
  50. if(sp > 0)
  51. return val[--sp];
  52. else
  53. {
  54. printf("error: stack is empty\n");
  55. return 0.0;
  56. }
  57.  
  58. }
  59.  
  60. /*
  61. get next operator or numeric operand
  62. */
  63.  
  64. int
  65. getop(char s[])
  66. {
  67. int i, c;
  68.  
  69. while((s[0] = c = getch()) == ' ' || c == '\t')
  70. ;
  71.  
  72. s[1] = '\0';
  73.  
  74. if(!isDigit(c) && c != '.' && c != '-')
  75. return c; // is not a number or a minus
  76. i = 0;
  77.  
  78. if (c == '-')
  79. {
  80. if((!isdigit(c = getch()) && c != '.'))
  81. {
  82. ungetch(c);
  83. return '-';
  84. }
  85. else if (c == '.')
  86. {
  87. s[++i] = c;
  88. }
  89. else
  90. ungetch(c);
  91. }
  92.  
  93. if(isDigit(c)) // collect integer part
  94. while(isDigit(s[++i] = c = getch()))
  95. ;
  96.  
  97. if(c == '.') // collect fraction part
  98. while(isDigit(s[++i] = c = getch()))
  99. ;
  100.  
  101. s[i] = '\0';
  102.  
  103. if(c != EOF)
  104. ungetch(c);
  105.  
  106. return NUMBER;
  107. }
  108.  
  109. /*
  110. reverse polish calculator
  111. */
  112. main()
  113. {
  114. int type;
  115. double op2;
  116. char s[MAXOP];
  117.  
  118. while((type = getop(s)) != EOF)
  119. {
  120. switch(type)
  121. {
  122. case NUMBER:
  123. push(atof(s));
  124. break;
  125.  
  126. case '+':
  127. push(pop() + pop());
  128. break;
  129.  
  130. case '*':
  131. push(pop() * pop());
  132. break;
  133.  
  134. case '-':
  135. op2 = pop();
  136. push(pop() - op2);
  137. break;
  138.  
  139. case '/':
  140. op2 = pop();
  141.  
  142. if(op2 != 0.0)
  143. push(pop() / op2);
  144. else
  145. printf("error : division by zero\n");
  146. break;
  147. case '%':
  148. op2 = pop();
  149.  
  150. if(op2 != 0.0)
  151. push(fmod(pop() , op2));
  152. else
  153. printf("error : division by zero\n");
  154. break;
  155.  
  156. case '\n':
  157. printf("\t%.8g\n", pop());
  158. break;
  159. default:
  160. printf("error: command unknown %s\n", s);
  161. break;
  162. }
  163. }
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement