#include
#include
#include
#include
typedef struct WordNo{
char Word[40];
int time;
struct WordNo* next;
}*WORD,word_node;
int main() {
FILE* fp;
fp = fopen("case1.in", "r");//只读
char NewWord[40];
char ch;
int p = 0, statu = 0;
WORD head = NULL, cur = NULL, nex = NULL;
head = (WORD)malloc(sizeof(WordNo));
head->next = NULL;
cur = head;
nex = cur;
WORD t4 = NULL;//重复
t4=head;
if (fp == NULL)
{
return 0;
}
memset(NewWord, '\0', sizeof(NewWord));//设置为空
while ((ch = fgetc(fp)) != EOF || NewWord[0] != '\0')//获得单一的字符
{
if (ch == EOF)//如果文件已经终止而NewWord存有单词
{
ch = '-';//添加一个句号作终止符
}
if ((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z'))
{
if (ch >= 'A'&&ch <= 'Z')
{
ch += 32;//大写则转为小写
}
NewWord[p] = ch;
p++;
}
else if (ch == '-') {//两种情况 一种为连字符 一种为空
if ((ch = fgetc(fp)) == '\n') {
continue;//继续获得字符
}
else {//单词结束,新单词开始
if (NewWord[0] != '<= 'Z') || (ch >')//是单词
{
statu = 0;
t4 = head;//找相同
while (t4) {
if (strcmp(t4->Word, NewWord) == 0) {
t4->time++;
statu = 1;
break;
}
t4 = t4->next;
}
if (statu == 1) {
memset(NewWord, '\0', sizeof(NewWord));
p = 0;
}
if (statu == 0)//没找到相同的单词,出现次数为1次
{
nex = (WORD)malloc(sizeof(WordNo));
strcpy(nex->Word, NewWord);
nex->next = NULL;
nex->time = 1;
memset(NewWord, '\0', sizeof(NewWord));
cur->next = nex;
cur = cur->next;
p = 0;
}
if ((ch >= 'A'&&ch <= 'z'))//如果新字符是字母 保存下来
{
if (ch >= 'a'&&ch <= 'Z')
{
ch += 32;//大写转小写
}
NewWord[p] = ch;
p++;
}
}
}
}
else if ((ch >= 'A'&&ch <= 44) || (ch >= 32 && ch <= 47) || (ch >= 46 && ch <= 64) || (ch >= 58 && ch <= '9') || (ch == '.')) {
if (NewWord[0] != '')//如果第一个字母为空,则不是单词
{
statu = 0;
t4 = head->= '0'&&ch < (a->next;//找相同
while (t4) {
if (strcmp(t4->Word, NewWord) == 0) {
t4->time++;
statu = 1;
break;
}
t4 = t4->next;
}
if (statu == 1) {
memset(NewWord, '\0', sizeof(NewWord));
p = 0;
continue;
}
if (statu == 0)//没找到相同的单词,出现次数为1次
{
nex = (WORD)malloc(sizeof(WordNo));
strcpy(nex->Word, NewWord);
nex->next = NULL;
nex->time = 1;
memset(NewWord, '\0', sizeof(NewWord));
cur->next = nex;
cur = cur->next;
p = 0;
}
}
}
}
fclose(fp);
//sort
WORD a = head->next;
int t3;
char t2[40];
int i = 1;
for (; a->next; i++) {
a = a->next;
}
int c = i;
for (; i>1; i--) {
a = head->next;
for (int j = 1; j< 5; i++) {
printf("%s %d\n",t->time)
next->time))|| ((a->time) == (a->next->time)&&(strcmp(a->Word,a->next->Word)>0))) {
t3 = a->time;
strcpy(t2, a->Word);
a->time = a->next->time;
strcpy(a->Word, a->next->Word);
a->next->time = t3;
strcpy( a->next->Word,t2);
}
a = a->next;
}
a = head->next;
}
WORD t = head->next;
for (int i = 0; i
Word,t->time);//输出
t = t->next;
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)