1. 程式人生 > >統計C語言關鍵字出現次數

統計C語言關鍵字出現次數

man scrip author != free string ret str 個數

統計C語言關鍵字出現次數

《C程序設計語言》K&R版本第6章結構6.3結構數組內容

/*
    Name: 統計c語言關鍵字出現次數 
    Copyright: 
    Author: lingr7
    Date: 21/10/18 10:05
    Description: 完全根據《C程序設計語言》K&R版本6.3結構數組內容編寫。在dev c++5.11中編譯運行成功
    一個項目文件.dev,內含calc.h,getch.c,getop.c,keytab.h,tongjimain.c.4個子文件 
    關鍵字結構數組在keytab.h中定義,可以自行修改該表,彈藥註意,這個數組裏順序必須是字典序升序。 
*/

/*tongjimain.c*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "calc.h"
#include "keytab.h"

#define MAXWORD 100

int getword(char *, int);
int binsearch(char *, struct key *, int);
/*統計輸入中c語言關鍵字的出現次數*/
main(){
    int n;
    char word[MAXWORD];
    printf("%s %s %s", keytab[0].word, keytab[1].word, keytab[2].word);
    while (getword(word,MAXWORD) != EOF)/*因為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\n",
                keytab[n].count, keytab[n].word);
    return 0;
} 
/*折半查找*/
/*2018年10月20日
lingr7*/
/* binsearch函數 :在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 = (high+low) / 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函數:從輸入中讀取下一個單詞或字符*/
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 = ‘\0‘;
        return c;
    }
    for ( ; --lim > 0; w++)
        if (!isalnum(*w = getch())){
            ungetch(*w);/**/ 
            break;
        }
    *w = ‘\0‘;
    return word[0];
} 
/*calc.h*/
#define NUMBER ‘0‘

/*void push(double);*/
/*double pop(void);*/

int getop(char []);

int getch(void);
void ungetch(int);
/*keytab.h*/
#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,
    "main", 0,
    "unsigned", 0,
    "void", 0,
    "volatile", 0,
    "while", 0,
};
/*getch.c*/
#include <stdio.h>
#define BUFSIZE 100

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

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;
}
/*getop.c*/
#include <stdio.h>
#include <ctype.h>
#include "calc.h"

/* getop: get next character or numeric operand */
int getop(char s[])
{
    int i, c;
    
    while ((s[0] = c = getch()) == ‘ ‘ || c == ‘\t‘)
        ;
    s[1] = ‘\0‘;
    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[i] = ‘\0‘;
    if (c != EOF)
        ungetch(c);
    return NUMBER;
}

統計C語言關鍵字出現次數