C编程(统计关键词

来源:百度知道 编辑:UC知道 时间:2024/06/23 05:52:34
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define bufsize 100
char buf[bufsize];
int getword(char *, int );
int bufp=0;
main()
{ int n;
struct key{char *word;
int count;
}keytab[]={
"auto",0,"break",0, "case",0,"dear",0};
char word[30];
while(getword(word,30)!=EOF)
if(isalpha(word[0]))
if((n=binseach(word,keytab,4))>=0)
keytab[n].count++;
for(n=0;n<4;n++)
if(keytab[n].count>0)
printf("%d%s\n",keytab[n].count,keytab.word);
return 0;
}

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)) { /*isalpha函数识别字母*/

这样编译没有问题了,首先struct key要定义在主函数外,这样binsearch函数才可以引用,printf("%d%s\n",keytab[n].count,keytab[n].word);要这样写。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define bufsize 100
char buf[bufsize];
int getword(char *, int );
int bufp=0;
struct key{char *word;
int count;
}keytab[]={
"auto",0,"break",0, "case",0,"dear",0};
main()
{ int n;

char word[30];
while(getword(word,30)!=EOF)
if(isalpha(word[0]))
if((n=binseach(word,keytab,4))>=0)
keytab[n].count++;
for(n=0;n<4;n++)
if(keytab[n].count>0)
printf("%d%s\n",keytab[n].count,keytab[n].word);
return 0;
}

int getword(char *word, int lim)
{
int c, getch(void);<