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

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

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

    呆呆向前沖的blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      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[])
    {  
      //開始試圖讀取一個操作數(shù)或一個操作符
            int i,c;
      //忽略空格:直到讀到非空字符
            while ((s[0] = c = getch()) == ' ' || c == '\t')
                    ;
      //末尾加字符串結(jié)束標(biāo)識
            s[1] = '\0';
            //if (!(s[0] == '-' && bufp == 1))
            //        if (!isdigit(c) && c != '.')
            //                return c;
      //如果讀到的不是數(shù)字并且不是小數(shù)點(diǎn),表示讀到了一個操作符
            if (!isdigit(c) && c != '.')
                    return c;       /* not a number */
            i = 0;
      //如果讀到了數(shù)字,繼續(xù)讀取直到讀到非數(shù)字字符
            if (isdigit(c)) /* collect integer part */
                    while (isdigit(s[++i] = c = getch()))
                            ;
      //如果上面讀取到的非數(shù)字字符是小數(shù)點(diǎn),繼續(xù)讀取直到讀取到非數(shù)字字符
            if (c == '.')   /* collect fraction part */
                    while (isdigit(s[++i] = c = getch()))
                            ;
      //在s結(jié)尾前一位加結(jié)束標(biāo)識
            s[i] = '\0';
      //如果未讀到末尾,把最后讀到的那個字符放到輸入緩沖字符數(shù)組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 閱讀(232) 評論(0)  編輯  收藏 所屬分類: 愛好:網(wǎng)絡(luò)編程

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 深夜福利在线免费观看| 国产一区二区三区免费观看在线| 亚洲成AⅤ人影院在线观看| 国产99久久久国产精免费| 亚洲一区二区中文| 永久免费av无码网站大全| 中国一级全黄的免费观看| 亚洲人成人网毛片在线播放| 亚洲国产精品尤物YW在线观看 | 和老外3p爽粗大免费视频| 亚洲一区二区三区日本久久九| 黄网址在线永久免费观看 | 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲Av永久无码精品黑人| 亚洲乱码一区二区三区在线观看| 国产在线jyzzjyzz免费麻豆 | 美女视频黄a视频全免费网站色| 亚洲国产综合专区电影在线 | 亚洲精品无码鲁网中文电影| 最近最新的免费中文字幕| 中国一级特黄的片子免费| 久久精品亚洲日本波多野结衣| 亚洲AV中文无码字幕色三| 国产精品国产自线拍免费软件| 免费无码又爽又刺激高潮视频| 看亚洲a级一级毛片| 亚洲专区一路线二| 亚洲成色WWW久久网站| 国产jizzjizz免费看jizz| 国产精品怡红院永久免费| 成人性生交大片免费看好| 免费看内射乌克兰女| 亚洲精品第一综合99久久| 香蕉蕉亚亚洲aav综合| 亚洲男人第一无码aⅴ网站| 毛片免费观看的视频在线| 中文字幕免费在线| 久久久久久精品免费看SSS| 一级毛片**免费看试看20分钟 | 男人天堂免费视频| 中美日韩在线网免费毛片视频|