coding第8天1.1

來根華子冷靜下發表於2020-11-11

指向結構的指標

重新寫binsearch函式,

struct key *binsearch(char *word,struct key *tab,int n)
{
	int cond;
	struct key *low=&tab[0];
	struct key *high=&tab[n];
	struct key *mid;

	while(low<high)
	{
		mid=low+(high-low)/2;
		if((cond=strcmp(word,mid->word))<0)
		{
			high=mid;
		}
		else if(cond>0)
		{
			low=mid+1;
		}
		else
		{
			return mid;
		}
	}
	return NULL;
}

主函式

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXWORD 100

int main(void)
{
	char word[MAXWORD];
	struct key *p;

	while(getword(word,MAXWORD)!=EOF)
	{
		if(isalpha(word[0]))
		{
			if((p=binsearch(word,keytab,NKEYS))!=NULL)
			{
				p->count++;
			}
		}
	}
	for(p=keytab,p<keytab+NKEYS;p++)
	{
		if(p->count>0)
		{
			printf("%4d %s\n",p->count,p->word);
		}
	}
	return 0;
}

相關文章