指向结构的指针
重新写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;
}