c语言程序设计题(急!)

2025-03-05 16:38:18
推荐回答(2个)
回答1:

英文输入完成后,用CTRL+Z输入结束标志。

//---------------------------------------------------------------------------

#include
#include
#include
#define STR_MAX (80)/*单个单词的最大长度*/
typedef struct {
char word[STR_MAX];
int count;
} node;
int ref(node * const ws,const int n,const char * const word)
{
int i;
for (i = 0; i if (strcmp(ws[i].word,word)==0) {
ws[i].count++;
return 1;
}
}
return 0;
}
node *input(int * const n)
{
node *ws=NULL;
char word[STR_MAX];
*n=0;

while (scanf("%*[^a-zA-Z]%[a-zA-Z]%*c",word)!=EOF)
{
if (!ws) {
ws=malloc(sizeof(node));
ws[*n].count=1;
strcpy(ws[(*n)++].word,word);
}
else if (!ref(ws,*n,word)){
ws=realloc(ws,sizeof(node)*(*n+1));
strcpy(ws[(*n)].word,word);
ws[(*n)++].count=1;
}
}
return ws;
}
int comp(const void *a,const void *b)
{
return (*(node *)b).count-(*(node *)a).count;
}
int main(void)
{
node *words=NULL;
int i,n=0;
words=input(&n);
qsort(words,n,sizeof(node),comp);
for (i = 0; i printf("%s(%d)\n",words[i].word,words[i].count);
}
return 0;
}
//---------------------------------------------------------------------------

回答2:

有问题,关键是:你信吗