读取一段文本并输出文本中每个不同单词在文本中出现的次数

题目:编写一个程序,对一个文本文件进行分析,将不同单词的个数按大小排序,并输出该文件中每个不同单词在文本中出现的次数

例如:To be or not to be, that is the question.Whether in the mind to stuffer

应输出:to 3次,be 2次, or 1次

思路:

当前测试环境:开发工具xcode Version 10.1 (10B61),操作系统Macos 10.14.3

注意:由于stricmp不能使用,已被废弃,所以使用strcasecmp功能是一样的

代码中内存申请没有释放,排序效率低

  1 #include <stdio.h>
  2 #include <strings.h>
  3 #include <stdlib.h>
  4 #include <xlocale.h>
  5 
  6 struct myString
  7 {
  8     int count;//单词出现重复次数
  9     char *_str;
 10 };
 11 struct words
 12 {
 13     int num;    //当前存储单词个数
 14     struct myString str[1024];
 15 };
 16 void readWords(struct words *pw)
 17 {
 18     FILE *fp = fopen("book.txt","r");
 19     if(fp == NULL)
 20     {
 21         printf("文件打开出错...\n");
 22         exit(0);
 23     }
 24     pw->num=0;
 25     char ch = getc(fp);
 26     char tmp[30] = {0};
 27     int i = 0;
 28     int pre = 0;  //  前一个字符是字母
 29     int current = 0;//当前字符是字母
 30     while(ch != EOF)
 31     {
 32         if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
 33         {
 34             current = 0;
 35             //如果前一个不是字母,当前是字母
 36             if(pre == -1 && current == 0)
 37             {
 38                 i = 0;
 39                 tmp[i]=ch;
 40             }
 41             else if(pre == 0 && current == 0)//如果前一个是字母,且当前是字母
 42             {
 43                 tmp[i] = ch;
 44             }
 45            ++i;
 46         }
 47         else
 48         {
 49             current = -1;
 50             //如果前一个是字母,当前不是字母
 51             if(pre == 0 && current == -1)
 52             {
 53                 tmp[i] = '\0';
 54                 //开始加入字符并比较
 55                 int j;
 56                 for(j = 0; j < pw->num; ++j)
 57                 {
 58                     //如果已保存相同单词,则在相同单词位置+1
 59                     if(strcasecmp(pw->str[j]._str, tmp) == 0)
 60                     {
 61                         pw->str[j].count++;
 62                         break;
 63                     }
 64                 }
 65                 //如果没有匹配的单词,则在数组中加入单词,并置1;
 66                 if(j >= pw->num)
 67                 {
 68                     int len = (int)strlen(tmp) + 1;
 69                     pw->str[pw->num]._str = (char*)malloc(len*sizeof(char));
 70                     strcpy(pw->str[pw->num]._str, tmp);
 71                     pw->str[pw->num].count = 1;
 72                     pw->num++;
 73                     i = 0;
 74                    // printf("tmp...%s\n", tmp);
 75                 }
 76             }
 77         }
 78         ch = getc(fp);
 79         pre = current;
 80     }
 81 }
 82 void mySwap(struct myString *str1, struct myString *str2)
 83 {
 84     char *tmp;
 85     int t;
 86     tmp = str1->_str;
 87     str1->_str = str2->_str;
 88     str2->_str = tmp;
 89     
 90     t = str1->count;
 91     str1->count = str2->count;
 92     str2->count = t;
 93 }
 94 void sortWords(struct words *pw)
 95 {
 96     struct myString *str = pw->str;
 97     int num = pw->num;
 98     for(int i = 0; i < num; ++i)
 99     {
100         for(int j = i+1; j < num; ++j)
101         {
102             if(str[i].count < str[j].count)
103             {
104                 mySwap(&(str[i]), &(str[j]));
105             }
106         }
107     }
108 }
109 void printWords(struct words *pw)
110 {
111     struct myString *str = pw->str;
112     int num = pw->num;
113     printf("单词:\t\t出现次数:\n");
114     for(int i = 0; i < num; ++i)
115     {
116         printf("%s\t\t%d\n", str[i]._str,str[i].count);
117     }
118 }
119 int main()
120 {
121     struct words w;
122     memset(&w, 0, sizeof(w));
123     w.num = 0;
124     readWords(&w);
125     sortWords(&w);
126     printWords(&w);
127     return 0;
128 }

猜你喜欢

转载自www.cnblogs.com/chuanyang/p/10604845.html