• 第六章 结构


    6.4 指向结构的指针

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define MAXWORD 100
    //int binsearch(char *, struct key *, int);
    #define NKEYS (sizeof keytab / sizeof(keytab[0]))
    #define BUFSIZE 100
    int getword(char *, int);
    char buf[BUFSIZE];
    int bufp = 0;
    
    struct key {
        char *word;
        int count;
    } keytab[] = {
        "auto", 0,
        "break", 0,
        "case", 0,
        "char", 0,
        "const", 0,
        "continue", 0,
        "default", 0,
        /* ... */
        "unsigned", 0,
        "void", 0,
        "volatile", 0,
        "while", 0
    };
    /* count C keywords */
    main()
    {
        int n;
        char word[MAXWORD];
        while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                keytab[n].count++;
            for (n = 0; n < NKEYS; n++)
                if (keytab[n].count > 0)
                    printf("%4d %s
    ", keytab[n].count, keytab[n].word);
    
        return 0;
    }
    
    /* binsearch: find word in tab[0]...tab[n-1] */
    
    int binsearch(char *word, struct key tab[], int n)
    {
        int cond;
        int low, high, mid;
        low = 0;
        high = n - 1;
        while (low <= high) {
            mid = (low+high) / 2;
            if ((cond = strcmp(word, tab[mid].word)) < 0)
                high = mid - 1;
            else if (cond > 0)
                low = mid + 1;
            else
                return mid;
        }
        return -1;
    }
    
    /* getword: get next word or character from input */
    
    int getword(char *word, int lim)
    {
        int c, getch(void);
        void ungetch(int);
        char *w = word;
        while (isspace(c = getch()))
            ;
        if (c != EOF)
            *w++ = c;
        if (!isalpha(c)) {
            *w = '';
            return c;
        }
        for ( ; --lim > 0; w++)
            if (!isalnum(*w = getch())) {
                ungetch(*w);
                break;
        }
        *w = '';
        return word[0];
    }
    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
    ");
        else
            buf[bufp++] = c;
    }
    终端输出显示:
    da
    gfa
    ray
    allen
    auto
    void
    los
    static
    register
    break
    ls
    case
    void
    auto
    break
    continue
       2 auto
       2 break
       1 case
       1 continue
       2 void
  • 相关阅读:
    弹出框
    my.conf 配置编码为utf-8
    解决git pull 命令失效,不能从远程服务器上拉取代码问题
    git config --global core.autocrlf false
    python数据库操作常用功能使用详解(创建表/插入数据/获取数据)
    MySQL缺失mysql_config文件
    物联网操作系统系列文章之-软件平台的力量
    50% 的财富 500 强企业使用 Windows Azure
    Mobile Service更新和 Notification Hub 对Android的支持
    Windows Azure 社区新闻综述(#68 版)
  • 原文地址:https://www.cnblogs.com/try-again/p/5028928.html
Copyright © 2020-2023  润新知