#include <stdio.h>
#include <string.h>
enum { MAX_SIZE = 20, MAX_WORDS = 200 };
struct MyTable {
char strs[MAX_WORDS][MAX_SIZE];
int occur[MAX_WORDS];
int cur_pos;
};
void init( struct MyTable* ptable );
void destroy( struct MyTable* ptable );
void input( struct MyTable* ptable );
void output_all( const struct MyTable* ptable );
void output_max( const struct MyTable* ptable );
int main(void) {
struct MyTable table;
init( &table );
input( &table );
output_all( &table );
output_max( &table );
destroy( &table );
return 0;
}
void init( struct MyTable* ptable ) {
ptable->cur_pos = 0;
}
void destroy( struct MyTable* ptable ) {}
void input( struct MyTable* ptable ){
char tmp[MAX_SIZE];
puts("请输入短文:");
while( scanf("%s", tmp) != EOF ) {
int i;
for( i=0;i<ptable->cur_pos;++i )
if( strcmp( tmp, ptable->strs[i]) ==0 ) {
++(ptable->occur[i]);
break;//break for
}
if( i==ptable->cur_pos ) {//not occur before
strcpy( ptable->strs[i], tmp );
ptable->occur[i] = 1;
++(ptable->cur_pos);
}
}
}
void output_all( const struct MyTable* ptable ) {
int i;
for( i=0;i<ptable->cur_pos;++i )
printf("单词 %s 出现了%d次\n",ptable->strs[i], ptable->occur[i]);
}
void output_max( const struct MyTable* ptable ) {
if( ptable->occur > 0 ) {
int i, max_index, max_occur;
for( i=0, max_index =0, max_occur = -1;i<ptable->cur_pos;++i )
if( ptable->occur[i] > max_occur ) {
max_occur = ptable->occur[i];
max_index = i;
}
printf("单词 %s 出现次数最多,出现了%d次\n",ptable->strs[max_index], ptable->occur[max_index]);
}
}
|