zoukankan      html  css  js  c++  java
  • 完成对输入的字符串中C关键词的查找统计。

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
     
    #define MAXWORD 80
     
    #define NKEYS (sizeof(keytab) / sizeof(struct key))
     
    struct key
    {       
        char *word;
        int count;
    }        keytab[] =
    {       
        "auto", 0,
        "break", 0,
        "case", 0,
        "char", 0,
        "const", 0,
        "continue", 0,
        "default", 0,
        "do", 0,
        "double", 0,
        "else", 0,
        "enum", 0,
        "extern", 0,
        "float", 0,
        "for", 0,
        "goto", 0,
        "if", 0,
        "int", 0,
        "long", 0,
        "register", 0,
        "return", 0,
        "short", 0,
        "singed", 0,
        "sizeof", 0,
        "static", 0,
        "struct", 0,
        "switch", 0,
        "typedef", 0,
        "union", 0,
        "unsigned", 0,
        "void", 0,
        "volatile", 0,
        "while", 0
    }       ;
     
    void getword(char *, int);
    int binsearch(char *, struct key *, int);
     
    int main(int argc, char *argv[])
    {       
        int n;
        char word[MAXWORD];
     
        printf("本程序将为您统计C语言的关键字的个数,请输入,输入end结束输入:
    ");
        do
        {       
            getword(word, MAXWORD);
            if (isalpha(word[0]))
            {       
                if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                {       
                    keytab[n].count++; //找到则对应次数+1
                }
            }
     
        }
        while (strcmp(word, "end") != 0);
     
        printf("您的输入中C语言关键字出现的次数统计如下:
    ");
        for (n = 0; n < NKEYS; n++)
        {       
            if (keytab[n].count > 0)
            {       
                printf("%-10s: %6d
    ", keytab[n].word, keytab[n].count);
            }
        }
     
        return 0;
    }       
     
    /* 折半查找:在tab[0]到tab[n-1]中查找word */
    int binsearch(char *word, struct key tab[], int n)
    {       
        int result;
        int low, high, mid;
     
        low = 0;
        high = n - 1;
        while (low <= high)
        {       
            mid = (low + high) / 2;
            if ((result = strcmp(word, tab[mid].word)) < 0)
            {       
                high = mid - 1;
            }
            else if (result > 0)
            {       
                low = mid + 1;
            }
            else
            {       
                return mid;
            }
        }
        return -1;
    }       
     
    /* getword:从输入中获取某个单词 */
    void getword(char *word, int lim)
    {       
        int c;
        void ungetch(int);
        char *w = word;
     
        while (isspace(c = getchar()))
        {       
        }
        if (c != EOF)
        {       
            *w = c;
            w++;
        }
        if (!isalpha(c))
        {       
            *w = '';
        }
        for ( ; --lim > 0; w++)
        {       
            if (!isalnum(*w = getchar()))
            {       
                //读入的某个字符不是字母,则将它退还给输入缓冲区
                ungetch(*w);
                break;
            }
        }
        *w = '';
    }    
    

    ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符、控制字符等等。既支持单字节(Byte)字符,也支持宽字符。

  • 相关阅读:
    pads layout模块复用-两个不同功能的复用模块功能
    摘抄:一个电容都能讲得如此全面实用,不分享就太可惜了!
    layout需要非常了解清楚的内容
    摘抄:一篇文章看看能不能讲透“阻抗匹配”
    python2.7/3.7安装NumPy函数库的一种方法及小心得
    3.C#基础之基本概念(完成)
    2.C#基础之词法结构(完成)
    .NET、.NET框架、ASP.NET和C#的关系(完成)
    1.C#基础之简介(完成)
    2.LINQ(新手写的!新手写的!新手写的!)(未完成)
  • 原文地址:https://www.cnblogs.com/HBU-xuhaiyang/p/12520672.html
Copyright © 2011-2022 走看看