老师布置的作业c语言
输入一段短文,数里面的单词,然后把每个单词出现的次数输出来。。。找出出现次数最多的那个单词和其出现的次数。。。
急。。。急。。。急。。。
咋没人理我啊 #include <stdio.h>
#include <string.h>
enum { MAX_SIZE = 20, MAX_WORDS = 200 };
struct MyTable {
char strs;
int occur;
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;
puts("请输入短文:");
while( scanf("%s", tmp) != EOF ) {
int i;
for( i=0;i<ptable->cur_pos;++i )
if( strcmp( tmp, ptable->strs) ==0 ) {
++(ptable->occur);
break;//break for
}
if( i==ptable->cur_pos ) {//not occur before
strcpy( ptable->strs, tmp );
ptable->occur = 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, ptable->occur);
}
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 > max_occur ) {
max_occur = ptable->occur;
max_index = i;
}
printf("单词 %s 出现次数最多,出现了%d次\n",ptable->strs, ptable->occur);
}
} 数里面的单词 这个很难吗? 课后习题在哪,怎么没找到?
页:
[1]