//dict.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define WORDLEN 32
struct word{
//存储单词
char name[WORDLEN];
//存储单词个数
int num;
struct word *next;
};
//读取单词并统计出现频率
int ReadFile(struct word *head);
//对文本内单词出现的频率进行排序
int Sort(struct word *head);
//统计文本单词总量
int GetCount(struct word *head);
//查询文本内任一单词出现的频率
int Search(struct word *head, char *word);
//dict.cpp
#include"dict.h"
//读取单词并统计出现频率
int ReadFile(struct word *head){
FILE *fp;
if ((fp = fopen("dict.txt", "r")) == NULL)
{
printf("无法打开此文件!\n");
exit(0);
}
char ch, temp[30];
struct word *p;
while (!feof(fp))
{
int i = 0;
ch = fgetc(fp);
temp[0] = ' ';
while ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z') || temp[0] == ' ')
{
if (ch >= 'a'&&ch <= 'z')
{
temp[i] = ch;
i++;
}
else if (ch >= 'A'&&ch <= 'Z'){
temp[i] = tolower(ch);
i++;
}
ch = fgetc(fp);
if (feof(fp)) break;
}
temp[i] = '\0';
p = head->next;
while (p)
{
if (!_stricmp(temp, p->name))
{
p->num++; break;
}
p = p->next;
}
if (!p&&temp[0] != '\0')
{
p = (struct word *)malloc(sizeof(struct word));
strcpy(p->name, temp);
p->num = 1;
p->next = head->next;
head->next = p;
}
}
return 0;
}
//对文本内单词出现的频率进行排序(前五)
int Sort(struct word *head){
int i;
struct word *p, *tail, *next;
int temp;
if (head->next->next == NULL)
return -1;
for (p = head->next; p->next != NULL; p = p->next) /*尾指针初始化*/
;
tail = p;
while (tail != head->next->next)
{
for (p = head->next; p->next != tail; p = p->next)
{
next = p->next;
if (p->num < next->num) /*相邻节点比较,数据交换*/
{
temp = p->num;
p->num = next->num;
next->num = temp;
}
}
tail = p; /*p->next == tail,即把tail往前移动一位*/
}
head = head->next;
for (i = 0; i < 5; i++){
printf("出现频率:%d\t", head->num);
puts(head->name);
head = head->next;
}
return 0;
}
//统计文本单词总量
int GetCount(struct word *head){
struct word *q;
q = head->next;
int sum = 0;
while (q != NULL){
printf("出现频率:%d\t", q->num);
puts(q->name);
sum = sum + q->num;
q = q->next;
}
printf("本文单词总量:%d\n", sum);
return 0;
}
//查询文本内任一单词出现的频率
int Search(struct word *head, char *word){
struct word *q = head->next;
while (q != NULL){
int temp = strcmp(word, q->name);
if (temp == 0)
{
printf("出现频率:%d\t", q->num);
puts(q->name);
break;
}
q = q->next;
}
return 0;
}
int main(int argc,char *argv[]){
printf("**********************************************************************************\n");
printf("******************************请输入文本名称**************************************\n");
printf("**********************************************************************************\n");
char cFilename[WORDLEN];
scanf("%s",cFilename);
getchar();
FILE* fpOpen;
fpOpen = fopen(cFilename,"r");
if(fpOpen==NULL){
printf("无法打开此文件!\n");
exit(0);
}
struct word *head;
head = (struct word *)malloc(sizeof(struct word));
head->next = NULL;
char cWordname[32];
ReadFile(head);
next_one:
while (1){
printf("**************************************************************************************\n");
printf("**单词已经读取成功,请输入操作:******************************************************\n");
printf("**1:查询文本单词总量 2:查看出现频率前五的单词 3:查看任一单词出现的频率 4:退出**\n");
printf("**************************************************************************************\n");
int iCmd;
scanf("%d", &iCmd);
getchar();
switch (iCmd){
case 1:
GetCount(head);
goto next_one;
break;
case 2:
Sort(head);
goto next_one;
break;
case 3:
printf("请输入要查询的单词\n");
scanf("%s", cWordname);
getchar();
Search(head,cWordname);
goto next_one;
break;
case 4:
goto next_two;
break;
default:
printf("输入的数字选项有误,请重新输入\n");
goto next_one;
break;
}
}
next_two:
fclose(fpOpen);
free(head);
return 0;
}
09-14
261