编写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并把频率最高的10个词打印出来

    尽管我们和蔼可爱的老师周一就给我们布置了这道题目,但我们整周都有课,课程好忙,自己也就是睡前想想这个程序。周五晚上8点,我着手敲源代码,当写到文件传输时,发现忘了c中是如何读取文件的,io流不会用,脑海中一片空白,8点26分,我停止了敲键盘,看书去。周六白天班级活动,去敬老院。晚上9点,我开始写代码,到了10点26,我把整个代码都写出来了,编译有错误,调试到了10点40,程序运行成功。我在桌面上建一个.txt文档,复制进去一篇英文文章,运行程序。运行黑框显示“无法打开此文件”,我知道肯定是路径错误了,我将代码改为 if((fp=fopen("D:\\in.txt","r"))==NULL),并在D盘下粘贴了一篇.txt文档,运行还是没有结果。10点50了,学校2分钟后熄灯,我将代码保存后关机。周日早,我去万达广场,到恩波考研中心逛了一圈,了解考研的一些信息。下午,拿电脑到太和换了win7系统。晚上7点,我网上查c中如何读取文件,网上写到文档必须与main.c存放在同一个文件夹中,我问了孔祥安,并把文档粘贴到了main.c所在的文件夹下,运行成功,8点10分。

    为了便于读取与统计,我定义了单词结构体:

   struct word
{
    char w[30];
    int k; 

    struct word *next;
};


 读取文件之前,先对文件进行判断:
if((fp=fopen("in.txt","r"))==NULL)
    {
        printf("无法打开此文件!\n");
        exit(0);
    }

  使用数组统计频率出现最高的10个词,并打印出来:

   printf("频率最高的十个单词是:\n");
    for(i=0;i<10;i++)
    {
        q=Head;
        while(q!=NULL)
        {
            if(q->k>a[i])
                a[i]=q->k;
            else
                q=q->next;
        }
        q=Head;
        while(q!=NULL)
        {
            if(a[i]==q->k)
            {
                q->k=0;
                printf("出现频率:%d\t",a[i]);
                puts(q->w);
                break;
            }
            else
                q=q->next;
        }

 1 #include "stdafx.h"
 2 
 3 #include<stdio.h>
 4 #include<stdlib.h> 
 5 #include<ctype.h> 
 6 #include<string.h> 
 7                      //定义单词的结构体
 8 struct word 
 9 { 
10     char w[30]; 
11     int k; 
12     struct word *next;
13 };
14  
15 int main() 
16 { 
17     FILE *fp; 
18     int i;
19     int a[10];
20     char b;
21     struct word *Head=NULL;
22     struct word *q;
23     for(i=0;i<10;i++)     //初始化数组 
24         a[i]=0;
25   
26     if((fp=fopen("in.txt","r"))==NULL)
27     { 
28         printf("无法打开此文件!\n");
29         exit(0);
30     }
31                          //统计单词的出现频率
32     while(!feof(fp))
33     { 
34         char *p=(char*)malloc(30*sizeof(char));
35         fscanf(fp,"%s",p);
36         if(Head==NULL)
37         { 
38             struct word *temp=(struct word*)malloc(sizeof(struct word));
39             strcpy(temp->w,p);
40             temp->k=1;
41             temp->next=NULL;
42             Head=temp;
43         }
44         else
45         { 
46             struct word *pp=Head;
47             while(pp!=NULL)
48             { 
49                 if(strcmp(pp->w,p)==0)
50                 { 
51                     int count = pp->k;
52                     count++;
53                     pp->k = count;
54                     break;
55                 }
56                 pp=pp->next;
57             } 
58             if(pp==NULL)
59             { 
60                 struct word *temp = (struct word*)malloc(sizeof(struct word));
61                 strcpy(temp->w, p);
62                 temp->k=1;
63                 temp->next=Head;
64                 Head=temp;
65             }
66         }
67     }
68                                  //对统计后的单词频率进行排序并输出
69     printf("频率最高的十个单词是:\n");
70     for(i=0;i<10;i++)
71     { 
72         q=Head;
73         while(q!=NULL)
74         { 
75             if(q->k>a[i])
76                 a[i]=q->k;
77             else
78                 q=q->next;
79         } 
80         q=Head;
81         while(q!=NULL)
82         { 
83             if(a[i]==q->k)
84             { 
85                 q->k=0;
86                 printf("出现频率:%d\t",a[i]);
87                 puts(q->w);
88                 break;
89             } 
90             else 
91                 q=q->next;
92         }
93     }
94     return 0;
95 }


  之前大一时没好好学c语言,编程能力挺差的。这回写这个程序感觉好不得心应手,思路一直卡,语法使用错误。看来日后要好好加油了。

posted @ 2014-03-03 08:56  凌晨4点的洛杉矶  阅读(641)  评论(0编辑  收藏  举报