笔者用两种方法实现了C语言代码中常见关键字出现的次数的统计:数组方法和指针方法,其本质是一样的。
1、数组实现
/****************************************************/
/********** 统计C语言中各个关键字出现的次数 ***********/
/********** 结构体数组实现 ***********/
/****************************************************/
#include
#include
#include
#define Maxwords 100 /* 存放关键字数组的最大长度 */
#define Nword 8
struct key {
char *wordstal; /* 存放字符串 */
int count; /* 存放字符串的个数 */
} keytab[Nword] = { /* 初始化一个结构体数组 (由结构体构成的数组)N 关键词的个数 引用keytab[i].word*/
{"auto", 0},
{"break", 0},
{"case", 0},
{"char", 0},
{"const", 0},
{"continue", 0},
{"default", 0},
/* ··· */
{"while", 0}
};
int getword(char *word, int n);
int binsearch(char *word, struct key *keytab, int n);
void writeword( struct key *keytab);
int main()
{
int type;
int n;
char word[Maxwords];
while( (type = getword(word, Maxwords)) != EOF ) {
if(isalpha(type))
if( (n = binsearch(word, keytab, Nword)) >= 0) {
keytab[n].count++;
}
}
writeword(keytab);
return 0;
}
#define Maxbuf 10
static int buf[Maxbuf];
int sp = 0;
void ungetch(int c)
{
if(sp
buf[sp++] = c;
}
else
printf("Error");
}
int getch(void)
{
return (sp>0) ? buf[--sp] : getchar();
}
*** 将文本中的word放入word[]中 同时返回 类型 ***/
int getword(char *word, int n)
{
int c;
while( isspace(c = getch()) )
;
if( (*word++ = c) == EOF ) /*跟书上有点不一样P119*/
return c;
if( !isalpha(c) ) {
word[0] = '\0';
return c;
}
while( isalnum(*word++ = c =getch()) && --n)
;
ungetch(c);
*word = '\0';
return word[0];
}
**** 比较两个字符串的大小 *****/
int strcmp(char *s, char *t)
{
while( *s == *t ) {
if(*s == '\0')
return 0;
s++;
t++;
}
return *s - *t;
}
**** 用折半查找的方法 *****/
int binsearch(char *word, struct key *keytab, int n)
{
int low = 0, high = n-1, mid;
while(low <= high) {
mid = (low + high)/2;
if(strcmp(word, keytab[mid].wordstal) > 0)
low = mid + 1;
else if(strcmp(word, keytab[mid].wordstal) < 0)
high = mid - 1;
else
return mid;
}
return -1; /*没有相等的字符*/
}
void writeword( struct key *keytab )
{
int i = 0;
for(i=0; i
printf("%s\t%d", keytab[i].wordstal, keytab[i].count);
}
}2、指针实现
/****************************************************/
/********** 统计C语言中各个关键字出现的次数 ***********/
/********** 结构体指针实现 ***********/ //程序的主要改变在 int main() 和 binsearch() 和writeword()函数
/****************************************************/
#include
#include
#include
#define Maxwords 100 /* 存放关键字数组的最大长度 */
#define Nword 8
struct key {
char *wordstal; /* 存放字符串 */
int count; /* 存放字符串的个数 */
} keytab[Nword] = { /* 初始化一个结构体数组 (由结构体构成的数组)N 关键词的个数 引用keytab[i].word*/
{"auto", 0},
{"break", 0},
{"case", 0},
{"char", 0},
{"const", 0},
{"continue", 0},
{"default", 0},
/* ··· */
{"while", 0}
};
int getword(char *word, int n);
struct key *binsearch(char *word, struct key *keytab, int n); //申明binsearch()函数,它返回一个指向 struct key 类型 的指针
//与返回 指向int 或char 类型的指针一样
void writeword( struct key *keytab);
int main()
{
int type;
struct key *p;
char word[Maxwords];
while( (type = getword(word, Maxwords)) != EOF ) {
if(isalpha(type))
if( (p = binsearch(word, keytab, Nword)) != NULL) {
p->count++;
}
}
writeword(keytab);
return 0;
}
#define Maxbuf 10
static int buf[Maxbuf];
int sp = 0;
void ungetch(int c)
{
if(sp
buf[sp++] = c;
}
else
printf("Error");
}
int getch(void)
{
return (sp>0) ? buf[--sp] : getchar();
}
/*** 将文本中的word放入word[]中 同时返回 类型 ***/
int getword(char *word, int n)
{
int c;
while( isspace(c = getch()) )
;
if( (*word++ = c) == EOF ) /*跟书上有点不一样P119*/
return c;
if( !isalpha(c) ) {
word[0] = '\0';
return c;
}
while( isalnum(*word++ = c =getch()) && --n)
;
ungetch(c);
*word = '\0';
return word[0];
}
/**** 比较两个字符串的大小 *****/
int strcmp(char *s, char *t)
{
while( *s == *t ) {
if(*s == '\0')
return 0;
s++;
t++;
}
return *s - *t;
}
/**** 用折半查找的方法 *****/
struct key *binsearch(char *word, struct key *keytab, int n)
{
struct key *low = &keytab[0];
struct key *high = &keytab[n]; //是 n 不是 n-1
struct key *mid;
while(low <= high) {
mid = low + (high - low)/2;
if(strcmp(word, mid->wordstal) > 0)
low = mid + 1;
else if(strcmp(word, mid->wordstal) < 0)
high = mid - 1;
else
return mid;
}
return NULL; /*没有相等的字符*/
}
void writeword( struct key *keytab )
{
struct key *p;
for(p=keytab; p
printf("%s\t%d", p->wordstal, p->count);
}
}希望指点,相互学习。