统计C语言代码中常见关键字出现的次数

笔者用两种方法实现了C语言代码中常见关键字出现的次数的统计:数组方法和指针方法,其本质是一样的。

1、数组实现

/****************************************************/
/********** 统计C语言中各个关键字出现的次数 ***********/
/**********         结构体数组实现         ***********/
/****************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#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<Maxbuf) {
		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<Nword; i++) {
		printf("%s\t%d", keytab[i].wordstal, keytab[i].count);
	}
}
2、指针实现

/****************************************************/
/********** 统计C语言中各个关键字出现的次数 ***********/
/**********         结构体指针实现         ***********/ //程序的主要改变在 int main() 和 binsearch() 和writeword()函数
/****************************************************/ 
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#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<Maxbuf) {
		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<keytab+Nword; p++) {
		printf("%s\t%d", p->wordstal, p->count);
	}
}
希望指点,相互学习。



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值