69,373
社区成员
发帖
与我相关
我的任务
分享
apple 1
banana 2
orange 1
#define MAX_WORDS 200
#define MAX_SIZE 10
typedef struct _wordPool {
int counter;
char word[MAX_SIZE];
} wordPool;
static WordPool wordTable[MAX_WORDS];
void add(const char *wp){
}
void print() {
}
int main (void) {
}
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 200
#define MAX_SIZE 10
typedef struct _wordPool {
int counter;
char word[MAX_SIZE];
} WordPool;
static WordPool wordTable[MAX_WORDS];
static unsigned wordcount;//统计wordTable中单词数量
int findword(const char *wt)
{
int i,find=0;
for(i=0;i!=wordcount;++i)
{
if(!strcmp(wordTable[i].word,wt))
{
++find;
break;
}
}
if(find)
return i;
else
return -1;
}
void add(const char *wp)
{
int pos;
if(wordcount>=MAX_WORDS)
{
puts("wordTable is full");
return;
}
if((pos=findword(wp))>=0)
++wordTable[pos].counter;
else
{
strcpy(wordTable[wordcount].word,wp);
wordTable[wordcount].counter=1;
++wordcount;
}
}
void print()
{
int i;
for(i=0;i!=wordcount;++i)
printf("%s : %d\n",wordTable[i].word,wordTable[i].counter);
}
int main (void)
{
add("hello");
add("world");
add("hello");
print();
getchar();
return 0;
}