打印出txt中出现频率最高的十个词——软件工程个人项目C语言

  1 #include"stdio.h"
  2 #include"string.h"
  3 
  4 #define WORDNUM 65535 // 单词的最大数量
  5 #define WORDLENGTH 20 // 单词的最大长度
  6 #define OUTPUTWORDNUM 10 //输出十个单词比较多的数
  7 
  8 char differentword[WORDNUM][WORDLENGTH]={0};//判断两单词是否相同
  9 int differentCount[WORDNUM];//数量相同的单词
 10 int iIndex = 0;//统计全文总数
 11 int iWordCount =0;//统计互不不同的单词数
 12 
 13 void GetWord(FILE *fpRead);
 14 void SetWord(char word[WORDLENGTH]);
 15 void OrderList();
 16 void OutPut();
 17 
 18 void DealWithTxt()//读取文本
 19 {
 20 FILE *fp;
 21 fp= fopen("xie.txt", "r");
 22 if(!fp)
 23 {
 24    printf("Cannot read file. \n");
 25    return;
 26 }
 27 
 28 GetWord(fp);
 29 OrderList();
 30 OutPut();
 31 fclose(fp);
 32 }
 33 void GetWord(FILE *fp)//遍历全文单词
 34 {
 35 int jIndex = 0;
 36 int i = 0;
 37 char ch;
 38 char word[WORDLENGTH]={0};
 39 while((ch=fgetc(fp))!=EOF)
 40 {
 41    //putchar(ch);
 42    if ((ch >= 65 && ch <=90) ||(ch >= 97 && ch <=122))
 43    {
 44     if (jIndex < WORDLENGTH)
 45     {
 46      word[jIndex] = ch;
 47      jIndex ++;
 48     }
 49    }
 50    else
 51    {
 52     if (jIndex != 0)
 53     {
 54      SetWord(word);
 55      jIndex = 0;
 56      iIndex ++;
 57     }
 58    }
 59 } 
 60 }
 61 void SetWord(char word[WORDLENGTH])//统计相同的单词个数
 62 {
 63 int i;
 64 int iEqual = 0;
 65 if (iIndex == 0)
 66 {
 67    strcpy(differentword[0],word); 
 68    differentCount[0] = 0;
 69    iWordCount ++;
 70 }
 71 for (i = 0; i < iWordCount; i ++)
 72 {
 73    if (strcmp(differentword[i],word) == 0)
 74    {
 75     differentCount[i] ++;
 76     iEqual = 1;
 77    }
 78 }
 79 if (iEqual == 0)
 80 {
 81    strcpy(differentword[iWordCount],word);
 82    differentCount[iWordCount] ++;
 83    iWordCount ++;
 84 }
 85 for (i = 0; i <WORDLENGTH; i ++)
 86 {
 87    word[i] = '\0';
 88 }
 89 }
 90 void OrderList()//为单词排序
 91 {
 92 int iCurrent = 0;
 93 int i,j,tempValue;
 94 char wordTemp[WORDLENGTH]={0};
 95 for (i = 0; i < iWordCount; i ++)
 96 {  
 97    iCurrent = i;
 98    for (j = i + 1; j < iWordCount; j ++)
 99    {
100     if (differentCount[iCurrent] < differentCount[j])
101     {
102      iCurrent = j;
103     }
104    } 
105    if (iCurrent != i)
106    {
107     tempValue = differentCount[iCurrent];
108     differentCount[iCurrent] = differentCount[i];
109     differentCount[i] = tempValue;
110     strcpy(wordTemp,differentword[iCurrent]);
111     strcpy(differentword[iCurrent],differentword[i]); 
112     strcpy(differentword[i],wordTemp); 
113    }
114 }
115 }
116 void OutPut()//按顺序输出
117 {
118 int i;
119 
120 printf("\n\n此文本中单词数量前十的是:\n\n");
121 
122 printf("%5s %18s %18s\n\n","序号","单词","个数");
123 for (i = 0; i < iWordCount && i < OUTPUTWORDNUM; i ++)
124 {
125     printf("%5d %18s %18d\n\n",i+1, differentword[i], differentCount[i]);
126 }
127 }
128 
129 int main(int argc, char* argv[])
130 { 
131 DealWithTxt();
132 getchar();
133 }

程序结果:

编程日志及解题思路:

2014227日 星期四

1400-1600观察题目,开阔思维

2014228日 星期五

1500-19:00确定思路,思考所需要的变量、函数及它们之间的逻辑关系,写出框架

201431日 星期六

白天一整天详细写出GetWord(FILE *fpRead)遍历全文单词,void SetWord(char word[WORDLENGTH])统计相同的单词个数void OrderList()为单词排序void OutPut()按顺序输出DealWithTxt()读取文本,int main(int argccharargv[])的具体内容。晚上调试程序到深夜,它们间的逻辑关系非常复杂,经过多次思考才完成这个程序,使其没有错误,尤其是SetWord(),OrderList()这两个函数。后来运行时文本又打不开,因为这个问题耽误了得有两个小时,最后发现只是文件格式隐藏,我多写了格式。

2014年3月2日 星期日

为代码美容,使其整齐紧凑,诚心实意写博客。

心得体会:

    写程序这个过程,需要很大的耐性,因为里面的关系一不留神就会弄错,还需要重复的看。在小事情上还有很多不懂的地方,因为它绊了很久。果真是只有想不到的,没有不发生的。总之,还是经验太少,导致脑子太愚笨。以后还是要勤加练习。运行时用的文件是1.77M,有点大,运行时间太长足足有半分钟。代码效率还是一个很严重的问题,更需要练习了。

原文地址:https://www.cnblogs.com/xiefengjiao/p/3576343.html