以前写的统计文章里单词出现的次数,原来想借此分析单词的频率,好重点记忆,结果出现的全是冠词连词啥的,很失败的构思。
#include
#include
#include
typedef struct MYDATA{
char sword[30];
int ncount;
struct MYDATA* pnext;
}MYDATA;
int main(void)
{
FILE* fps;
FILE* fpt;
FILE* fpd;
char ctemp;
char stemp[50];
MYDATA* phead = NULL;
MYDATA* pcurrent = NULL;
MYDATA* ptemp = NULL;
int nexist = 0;
int nmax = 0;
int i;
if((fps = fopen("test.txt", "r")) == NULL){
printf("打开指定文件失败,");
system("pause");
return 0;
}
if((fpt = fopen("temp.txt", "wt+")) == NULL){
printf("创建临时文件失败,");
system("pause");
return 0;
}
while(!feof(fps)){
ctemp = fgetc(fps);
if(ctemp >= 97 && ctemp <= 122 || ctemp == 39){
fputc(ctemp, fpt);
}
else{
if(ctemp >= 65 && ctemp <= 90){
fputc(ctemp + 32, fpt);
}
else{
fputc('\n', fpt);
}
}
}
fclose(fps);
rewind(fpt);
while(!feof(fpt)){
fscanf(fpt, "%s", stemp);
if(phead == NULL){
phead = (MYDATA*)malloc(sizeof(MYDATA));
strcpy(phead->sword, stemp);
phead->ncount = 1;
phead->pnext = NULL;
pcurrent = phead;
if(nmax <= phead->ncount)
nmax = phead->ncount;
}
else{
ptemp = phead;
nexist = 0;
while(ptemp != NULL){
if(strcmp(ptemp->sword, stemp) == 0){
ptemp->ncount++;
nexist = 1;
if(nmax <= ptemp->ncount)
nmax = ptemp->ncount;
}
ptemp = ptemp->pnext;
}
if(nexist == 0){
ptemp = (MYDATA*)malloc(sizeof(MYDATA));
strcpy(ptemp->sword, stemp);
ptemp->ncount = 1;
ptemp->pnext = NULL;
pcurrent->pnext = ptemp;
pcurrent = ptemp;
if(nmax <= ptemp->ncount)
nmax = ptemp->ncount;
}
}
}
fclose(fpt);
remove("temp.txt");
if((fpd = fopen("data.txt", "w")) == NULL){
printf("创建数据文件失败,");
system("pause");
return 0;
}
for(i = nmax; i > 0; i--){
ptemp = phead;
while(ptemp != NULL){
if(ptemp->ncount == i){
fprintf(fpd, "%-20s%d\n", ptemp->sword, ptemp->ncount);
}
ptemp = ptemp->pnext;
}
}
fclose(fpd);
printf("程序运行结束!");
system("pause");
return 0;
}