LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 669|回复: 1

只匹配数字的程序?

[复制链接]
发表于 2004-3-12 20:59:01 | 显示全部楼层 |阅读模式
  1. /* this program forms the basis on which exercises 4-3 through 4-10 build */

  2. #include <stdio.h>
  3. #include <stdlib.h> /* for atof() - in K&R, math.h is referenced - this is an anachronism */

  4. #define MAXOP 100 /* max size of operand or operator */
  5. #define NUMBER '0' /* signal that a number was found */

  6. int getop(char []);
  7. void push(double);
  8. double pop(void);

  9. /* reverse Polish calculator */

  10. int main(void)
  11. {
  12.   int type;
  13.   double op2;
  14.   char s[MAXOP];

  15.   while((type = getop(s)) != EOF)
  16.   {
  17.     switch(type)
  18.     {
  19.       case NUMBER:
  20.         push(atof(s));
  21.         break;
  22.       case '+':
  23.         push(pop() + pop());
  24.         break;
  25.       case '*':
  26.         push(pop() * pop());
  27.         break;
  28.       case '-':
  29.         op2 = pop();
  30.         push(pop() - op2);
  31.         break;
  32.       case '/':
  33.         op2 = pop();
  34.         if(op2 != 0.0)
  35.           push(pop() / op2);
  36.         else
  37.           printf("error: zero divisor\n");
  38.         break;
  39.       case '\n':
  40.         printf("\t%.8g\n", pop());
  41.         break;
  42.       default:
  43.         printf("error: unknown command %s\n", s);
  44.         break;
  45.     }
  46.   }

  47.   return 0;
  48. }

  49. #define MAXVAL  100 /* maximum depth of val stack */

  50. int sp = 0; /* next free stack position */
  51. double val[MAXVAL]; /* value stack */

  52. /* push: push f onto value stack */
  53. void push(double f)
  54. {
  55.   if(sp < MAXVAL)
  56.     val[sp++] = f;
  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.   {
  67.     printf("error: stack empty\n");
  68.     return 0.0;
  69.   }
  70. }

  71. #include <ctype.h>

  72. int getch(void);
  73. void ungetch(int);

  74. /* getop: get next operator or numeric operand */
  75. int getop(char s[])
  76. {
  77.   int i, c;

  78.   while((s[0] = c = getch()) == ' ' || c == '\t')
  79.     ;

  80.   s[1] = '\0';
  81.   if(!isdigit(c) && c != '.')
  82.     return c; /* not a number */
  83.   i = 0;
  84.   if(isdigit(c)) /* collect integer part */
  85.     while(isdigit(s[++i] = c = getch()))
  86.       ;
  87.   if(c == '.')
  88.     while(isdigit(s[++i] = c = getch()))
  89.       ;
  90.   s[i] = '\0';
  91.   if(c != EOF)
  92.     ungetch(c);
  93.   return NUMBER;
  94. }

  95. #define BUFSIZE 100

  96. char buf[BUFSIZE]; /* buffer for ungetch */
  97. int bufp = 0; /* next free position in buf */

  98. int getch(void) /* get a (possibly pushed back) character */
  99. {
  100.   return (bufp > 0) ? buf[--bufp] : getchar();
  101. }

  102. void ungetch(int c) /* push character back on input */
  103. {
  104.   if(bufp >= BUFSIZE)
  105.     printf("ungetch: too many characters\n");
  106.   else
  107.     buf[bufp++] = c;
  108. }
复制代码

不知道这个程序是干嘛的?
发表于 2004-3-12 21:41:09 | 显示全部楼层
如果学过编译原理就清楚啦。这就是利用逆波兰表达式实现计算器。如果表达式为2+3*5,转化成逆波兰表达式输入就是3 5 * 2 +,再利用堆栈,先压入3和5,碰到*时弹出3和5计算乘积得15并压入堆栈得15,再压入2,碰到+时再弹出15和2,得出其和为17。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表