<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    呆呆向前沖的blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      78 隨筆 :: 43 文章 :: 5 評論 :: 74 Trackbacks

    mmd 感覺腦子不夠用了。還是用笨方法:一步一步寫注釋。

    #include <stdio.h>
    #include <stdlib.h> /* for atof() */
    #include <ctype.h>

    #define MAXOP 100       /* max size of operand and operator */
    #define NUMBER '0'      /* signal that a number was found  */
    #define MAXVAL 100      /* maxmium depth of value stack */
    #define BUFSIZE 100

    int getop(char []);
    void push(double);
    double pop(void);
    int getch(void);
    void ungetch(int);

    int sp = 0;             /* next free stack position */
    double val[MAXVAL];     /* value stack  */
    char buf[BUFSIZE];      /* buffer for ungetch */
    int bufp = 0;           /* next free position in buf */

    /* reverse Polish Calculator */

    main()
    {
            int type;
            double op2;
            char s[MAXOP];

            while ((type = getop(s)) != EOF) {
                    switch (type) {
                            case NUMBER:
                                    push(atof(s));
                                    break;
                            case '+':
                                    push(pop() + pop());
                                    break;
                            case '*':
                                    push(pop() * pop());
                                    break;
                            case '-':
                                    op2 = pop();
                                    push(pop() - op2);
                                    break;
                            case '/':
                                    op2 = pop();
                                    if (op2 != 0.0)
                                            push(pop() / op2);
                                    else
                                            printf("error: zero divisor\n");
                                    break;
                            case '\n':
                                    printf("\t%.8g\n",pop());
                                    break;
                            default:
                                    printf("error: unknown command %s\n",s);
                                    break;
                    }
            }
            return 0;
    }
    /* push: push f onto value stack  */
    void push(double f)
    {
            if (sp < MAXVAL)
                    val[sp++] = f;
            else
                    printf("error: stack full,can't push %g\n",f);
    }
    /* pop: pop and return top value from stack */
    double pop(void)
    {
            if (sp > 0)
                    return val[--sp];
            else {
                    printf("error: stack empty\n");
                    return 0.0;
            }
    }
    /* getop: get next character or numeric oprand */
    int getop(char s[])
    {  
      //開始試圖讀取一個操作數或一個操作符
            int i,c;
      //忽略空格:直到讀到非空字符
            while ((s[0] = c = getch()) == ' ' || c == '\t')
                    ;
      //末尾加字符串結束標識
            s[1] = '\0';
            //if (!(s[0] == '-' && bufp == 1))
            //        if (!isdigit(c) && c != '.')
            //                return c;
      //如果讀到的不是數字并且不是小數點,表示讀到了一個操作符
            if (!isdigit(c) && c != '.')
                    return c;       /* not a number */
            i = 0;
      //如果讀到了數字,繼續讀取直到讀到非數字字符
            if (isdigit(c)) /* collect integer part */
                    while (isdigit(s[++i] = c = getch()))
                            ;
      //如果上面讀取到的非數字字符是小數點,繼續讀取直到讀取到非數字字符
            if (c == '.')   /* collect fraction part */
                    while (isdigit(s[++i] = c = getch()))
                            ;
      //在s結尾前一位加結束標識
            s[i] = '\0';
      //如果未讀到末尾,把最后讀到的那個字符放到輸入緩沖字符數組buf中
            if (c != EOF)
                    ungetch(c);
            return NUMBER;
    }

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

    void ungetch(int c)     /* push character back on input */
    {
            if (bufp >= BUFSIZE)
                    printf("ungetch: too many characters\n");
            else
                    buf[bufp++] = c;
    }

    posted on 2005-01-25 23:19 呆呆向前沖的blog 閱讀(227) 評論(0)  編輯  收藏 所屬分類: 愛好:網絡編程

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 少妇性饥渴无码A区免费| 亚洲xxxxxx| 亚洲色大成网站WWW久久九九| 免费A级毛片无码A| 国产乱色精品成人免费视频| 日本黄页网站免费| 国产男女猛烈无遮挡免费视频| 波多野结衣中文一区二区免费| 国产在线19禁免费观看| 四虎影院永久免费观看| 亚洲国产成人久久综合一区77| 亚洲美女在线国产| 国产精品亚洲一区二区三区在线| 日韩亚洲一区二区三区| 亚洲bt加勒比一区二区| 亚洲国产中文在线二区三区免| 亚洲 暴爽 AV人人爽日日碰| 日本系列1页亚洲系列| 一级看片免费视频| 国产又黄又爽又大的免费视频| 久久国产乱子伦精品免费看| 中文字幕免费视频一| 毛片a级毛片免费播放下载| 日本免费的一级v一片| 亚洲国产一区视频| 亚洲国产精品一区第二页| 久久亚洲春色中文字幕久久久| 亚洲成年人免费网站| 亚洲人片在线观看天堂无码| 美女被免费网站在线视频免费 | 免费a级毛片无码av| 国产亚洲精品自在线观看| 亚洲国产人成在线观看69网站 | 亚洲VA中文字幕无码毛片 | 国产在线a不卡免费视频| 亚洲综合区小说区激情区| 亚洲国产精品一区| 亚洲人成人伊人成综合网无码| 一二三四在线观看免费中文在线观看 | 国产麻豆免费观看91| 国产亚洲精品拍拍拍拍拍|