#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MAXN = 2000000;
const int MAXW = 20;
bool isletter(char c);
struct Word
{
char word[MAXW];
int n;
}word[MAXN];
Word filted_word[MAXN];
bool cmp1(Word x, Word y);
bool cmp2(Word x, Word y);
char passage;
int main()
{
FILE* fpr = fopen("input.txt", "r");
FILE* fpw = fopen("output.txt", "w");
int i;
if (fpr)
{
int flag = 0;
int i1 = 0, i2 = 0;
passage = fgetc(fpr);
for (; feof(fpr)!= 1;)
{
while (isletter(passage)&& feof(fpr) != 1)
{
word[i1].word[i2] = (passage>='A'&&passage<='Z')?(passage+('a'-'A')):(passage);
i2++;
passage = fgetc(fpr);
if (isletter(passage) == 0)
{
i1++;
i2 = 0;
}
}
while (isletter(passage) == 0&& feof(fpr) != 1)
{
passage = fgetc(fpr);
}
}
printf("i1=%d\n", i1);
int n1 = i1, n2 = 0;
sort(word, word + n1-1, cmp1);
int ct_filted_word = 0;
for (i = 0; i <= n1 - 1; )
{
int ct = 1;
while (i <= n1 - 1 && strcmp(word[i].word, word[i + 1].word) == 0)
{
ct++; i++;
}
if (strcmp(word[i].word, word[i + 1].word) != 0)
{
strcpy(filted_word[ct_filted_word].word, word[i].word);
filted_word[ct_filted_word].n = ct;
ct = 1;
i++;
ct_filted_word++;
}
}
n2 = ct_filted_word;
sort(filted_word, filted_word + n2, cmp2);
if (fpw)
{
for (i = 0; i <= n2 - 1; i++)
fprintf(fpw, "单词:%s 词频%d\n", filted_word[i].word, filted_word[i].n);
return 0;
}
else
{
printf("写文件读取失败\n");
}
}
else
{
printf("读文件打开失败\n");
return 0;
}
}
bool isletter(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return 1;
else
return 0;
}
bool cmp1(Word x,Word y)
{
if(strcmp(x.word, y.word)==0) return 0;
return ((strcmp(x.word, y.word) < 0) ? (1) : (0));
}
bool cmp2(Word x, Word y)
{
return (x.n > y.n) ? (1) : (0);
}