文件搜索 1.0

  1 #include <stdio.h>
  2 #include <string.h>
  3 using namespace std;
  4 
  5 struct cha
  6 {
  7     char aa[50];//存储单词(不重复)
  8     int  num;//该单词出现的次数
  9     int  Mark[100];//标记该单词出现过的行
 10 };
 11 
 12 cha CHA[1000]; 
 13 
 14 struct Mystr
 15 {
 16     char bb[50];//存储 :字符串
 17     int rol;//存储:字符串 所在行
 18 };
 19 
 20 
 21 void Standard_String(char cans[]) //标准化字符串
 22 {
 23     int i=0;
 24     while(cans[i]!='\0')
 25     {
 26         if(cans[i]>='A'&&cans[i]<='Z')//字母全小写
 27             cans[i]+='a'-'A';
 28         else if((! ((cans[i]>='A'&&cans[i]<='Z')||(cans[i]>='a'&&cans[i]<='z')))&&cans[i]!=' ')
 29             cans[i]=' ';              //符号全变为空格
 30         ++i;
 31     }
 32 
 33     char standard[501];   //存放标准字符串
 34     i=0;int j=0;
 35     while(cans[i]==' ')//去开头空格
 36         ++i;
 37     while(cans[i]!='\0')
 38     {
 39         while(cans[i] != ' ' && cans[i]!='\0')
 40         {
 41             char ctem=cans[i];
 42             standard[j++]=ctem;
 43             ++i;
 44         }
 45         standard[j++]=' ';
 46         while(cans[i] == ' '&&cans[i]!='\0')
 47             ++i;    
 48     }
 49     standard[j]='\0';
 50     strcpy(cans,standard);
 51 }
 52 
 53 
 54 
 55 int main()
 56 {
 57     char cc[500][501];  //存储 原版文本
 58     char Standard_File[500][501];  //存储标准化后的文本,用于查找字符串
 59     Mystr ans[1000];//存储字符串
 60     char tem[50];
 61     int i=0,m;
 62 
 63 
 64 
 65 
 66 
 67    /***************************读取文件*******************************/
 68     FILE* fp;
 69     if((fp=fopen("E:\\cc.txt","r"))==NULL) 
 70     {
 71         printf("cannot open file\n");
 72         return 0;
 73     }
 74     int fcount=0;//记录文本的行数
 75     while(!feof(fp))
 76     {
 77         i=0;
 78         while(!feof(fp))//100个字符为一行存入cc中
 79         {
 80             fgets(cc[fcount],500,fp);
 81             char ctem[501];
 82             strcpy(ctem,cc[fcount]);
 83             Standard_String(ctem);//字符串标准化
 84             strcpy(Standard_File[fcount++],ctem);
 85         }
 86     }
 87     fclose(fp);
 88   /***************************************************************************/
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97  /**************************扣取单词,统计单词个数*******************************/
 98 
 99     int count=0;//单词数
100     for(m=0;m<fcount;m++)
101     {
102         i=0;
103         while(cc[m][i]==' ')//去开头空格
104             ++i;
105         while(cc[m][i]!='\0')
106         {
107             ++count;
108             int j=0;
109             //printf("%d : ",count);
110             while( ( (cc[m][i]>='a'&&cc[m][i]<='z')||(cc[m][i]>='A'&&cc[m][i]<='Z')||cc[m][i]=='\'')&&cc[m][i]!='\0')
111             {
112                 char ctem=cc[m][i];
113                 if(cc[m][i]>='A'&&cc[m][i]<='Z') ctem+='a'-'A';
114                 printf("%c",ctem);
115                 ans[count].bb[j++]=ctem;
116                 ++i;
117             }
118             ans[count].bb[j]='\0';
119             ans[count].rol=m;//记录每个单词所在的行号
120             printf("\n");
121             while( (!( (cc[m][i]>='a'&&cc[m][i]<='z')||(cc[m][i]>='A'&&cc[m][i]<='Z')||cc[m][i]=='\'' ))&&cc[m][i]!='\0' )
122                  ++i;    
123         }
124     }
125     printf("一共 %d 个单词\n",count);
126 
127 /***************************************************************************/
128 
129 
130 
131 
132 
133 
134 
135 
136 
137     /****************************按照字符顺 冒泡排序*********************/
138     int is=0;//是否排序完成
139     int Tct=count;
140     while(is==0)
141     {
142         is=1;
143         for(i=1;i<Tct;i++)
144         {
145             if(strcmp(ans[i].bb,ans[i+1].bb)>0)
146             {
147                 is=0;
148                 Mystr Mtem=ans[i];
149                 ans[i]=ans[i+1];
150                 ans[i+1]=Mtem;
151             }
152         }
153         --Tct;
154     }
155     /*********************************************************************************/
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 /***************************记录单词出现的次数和所在的行号**************************/
167 
168     printf("\n");
169     printf("\n");
170     int count2=-1;//不同的单词的个数
171     for(i=1;i<=count;i++)
172     {
173         if(strcmp(ans[i].bb,ans[i+1].bb)==0)
174         {
175             strcpy(CHA[++count2].aa,ans[i].bb);
176 
177             if(CHA[count2].Mark[ans[i].rol]!=1)//标记该单词出现过的行号
178                 CHA[count2].Mark[ans[i].rol]=1;
179             if(CHA[count2].Mark[ans[i+1].rol]!=1)//标记该单词出现过的行号
180                 CHA[count2].Mark[ans[i+1].rol]=1;
181 
182             strcpy(tem,ans[i].bb);
183             CHA[count2].num+=2;
184             i+=2;
185             while(strcmp(tem,ans[i].bb)==0)
186             {
187                 ++CHA[count2].num;
188                 if(CHA[count2].Mark[ans[i].rol]!=1)//标记该单词出现过的行号
189                     CHA[count2].Mark[ans[i].rol]=1;
190                 ++i;
191             }
192             --i;
193         }
194         else if (strcmp(ans[i].bb,ans[i+1].bb)!=0)
195         {
196             strcpy(CHA[++count2].aa,ans[i].bb);
197 
198             if(CHA[count2].Mark[ans[i].rol]!=1)//标记该单词出现过的行号
199                 CHA[count2].Mark[ans[i].rol]=1;
200             CHA[count2].num++;
201         }
202     } 
203 
204 /*********************************************************************************/
205 
206 
207 
208 
209 
210 /******************************按照次数排序*************************************/
211 
212     printf("\n");
213     printf("\n");
214     is=0;//是否排序完成
215     Tct=count2;
216     while(is==0)
217     {
218         is=1;
219         for(i=0;i<Tct;i++)
220         {
221             if(CHA[i].num<CHA[i+1].num)
222             {
223                 is=0;
224                 cha ctem=CHA[i];
225                 CHA[i]=CHA[i+1];
226                 CHA[i+1]=ctem;
227             }
228         }
229         --Tct;
230     }
231     if(count2!=0)
232     {
233         printf("先按次数再按字符序的排序如下:\n\n");
234         for(i=0;i<=count2;i++)
235             printf("第%d个: 出现次数为:%d次    %s\n",i+1,CHA[i].num,CHA[i].aa);
236     }
237 
238     /*********************************************************************************/
239 
240 
241 
242 
243 
244 
245 
246 /******************************输出 次数 频率*************************************/
247 
248     printf("\n");
249     printf("\n");
250     for(i=0;i<=count2;i++)
251         printf("字符:%s  次数为:%d 频率:%0.3lf\n",CHA[i].aa,CHA[i].num,(double)CHA[i].num/count);
252     printf("\n");
253     printf("\n");
254 /*********************************************************************************/
255 
256 
257 
258 
259 
260 
261 
262 
263 /***********************************查找***************************************/
264 
265 
266     printf("查找单词请输入‘1’,查找字符串请输入‘2’:");
267     int mark1;
268     while(scanf("%d",&mark1)!=EOF)
269     {
270         int find=0;
271      /***********************查找单词************************/
272         if(mark1==1) 
273         {
274             char word[50];
275             printf("请输入要查找的单词:\n");
276             scanf("%s",word);
277             printf("\n");
278 
279             for(i=0;i<=count2;i++)
280             {
281                 if(strcmp(word,CHA[i].aa)==0)
282                 {
283                     find=1;
284                     for(m=0;m<fcount;m++)
285                     {
286                         if(CHA[i].Mark[m]==1)
287                             printf("找到第%d行:%s\n",m+1,cc[m]);
288                     }
289 
290                     break;
291                 }
292             }
293 
294             if(find==0) printf("该单词不存在!!\n");
295         }
296       /***************************查找字符串************************/
297         else
298         {
299             char word2[50];
300             printf("请输入要查找的字符串:\n");
301             getchar();
302             gets(word2);
303             Standard_String(word2);//字符串标准化
304             int len2 = strlen(word2);
305             printf("\n");
306             for(m=0;m<fcount;m++)
307             {
308                 int len1=strlen(Standard_File[m]);
309 
310                 if(len1<len2) continue;
311 
312                 for(i=0;i+len2<=len1;i++)
313                 {
314                     while((i+len2<=len1)&&word2[0]!=Standard_File[m][i])
315                         ++i;
316 
317                     if(i+len2>len1) continue;
318 
319                     char ctem[50];
320                     strncpy(ctem,Standard_File[m]+i,len2);
321                     ctem[len2]='\0';
322                     if(strcmp(ctem,word2)==0)
323                     {
324                         printf("找到第%d行:%s\n",m+1,cc[m]);
325                         break;
326                     }
327                 }
328             }
329         }
330         printf("\n查找单词请输入‘1’,查找字符串请输入‘2’:");
331     }
332 
333  /*********************************************************************************/
334 
335     return 1;
336 }

 

posted @ 2015-01-17 10:22  小爷  阅读(336)  评论(0编辑  收藏  举报