fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define STACK_MAX (100)
  4.  
  5. // prototype
  6. void res();
  7. void push(int w);
  8. int pop();
  9.  
  10. // global
  11. int st[STACK_MAX];
  12. int pt = 0;
  13. char str[10];
  14. int cnt[256] = {};
  15.  
  16. // process
  17. int main()
  18. {
  19. FILE *fp;
  20. int i;
  21.  
  22. fp = fopen("c161-102.c.in", "r");
  23. printf("input = ");
  24. while ((fscanf(fp, "%s", str)) != EOF)
  25. res();
  26. printf("\nresult = %d\n", pop());
  27. for ( i = 0; i < 256; ++i)
  28. if (cnt[i])
  29. printf("%c %d\n", i, cnt[i]);
  30.  
  31. return 0;
  32. }
  33.  
  34.  
  35. void res()
  36. {
  37. int a, b;
  38. char c;
  39.  
  40. printf("%s ", str);
  41. c = str[0];
  42.  
  43. switch (c) {
  44. case '+':
  45. cnt[c]++;
  46. a = pop();
  47. b = pop();
  48. push(b + a);
  49. break;
  50. case '-':
  51. cnt[c]++;
  52. a = pop();
  53. b = pop();
  54. push(b - a);
  55. break;
  56. case '*':
  57. cnt[c]++;
  58. a = pop();
  59. b = pop();
  60. push(b * a);
  61. break;
  62. case '/':
  63. cnt[c]++;
  64. a = pop();
  65. b = pop();
  66. if (a == 0)
  67. break;
  68. push(b / a);
  69. break;
  70. default:
  71. push(atoi(str ));
  72. break;
  73. }
  74. }
  75.  
  76. void push(int w)
  77. {
  78. st[pt++] = w;
  79. }
  80.  
  81. int pop()
  82. {
  83. return st[--pt];
  84. }
  85.  
Runtime error #stdin #stdout 0.02s 1808KB
stdin
1 2 + 3 4 * +
11 22 - 3 /
+
stdout
Standard output is empty