SSH:git@git.coding.net:shenbaishan/cipintongji_.git
再修正SSH:git@git.coding.net:shenbaishan/cipintongji_.git
需求分析:
1. 小文件输入——从控制台由用户输入到文件中,再对文件进行统计;
2.支持命令行输入英文作品的文件名;
3.支持命令行输入存储有英文作品文件的目录名,批量统计;
4.从控制台读入英文单篇作品,重定向输入流。
部分代码分析:
See if the transformed @string repeat,than add the frequency,otherwise create class @String to contain the new @string
1 void If_Repet(char *WordP) 2 { 3 vector<String>::iterator iter; 4 for (iter = Sort_Word.begin(); iter != Sort_Word.end(); iter++) 5 if (!strcmp(iter->Word_Point, WordP)) 6 { 7 Total++; 8 iter->Frequency++; 9 return; 10 } 11 Sort_Word.push_back(String(WordP)); 12 }
read each letter and transform them into @string
1 void Catch_Letter(char *File_P) 2 { 3 char *Wor_Po = (char *)malloc(4); 4 FILE *Article = fopen(File_P, "r"); 5 char *Letter_Point; 6 while (!feof(Article)) 7 { 8 fscanf(Article, "%s", Wor_Po); 9 Letter_Point = Wor_Po; 10 while (*Letter_Point++) 11 { 12 if (*Letter_Point == ',' || *Letter_Point == '.') 13 *Letter_Point = 0; 14 } 15 If_Repet(Wor_Po); 16 } 17 }
after this function,all unique word should belong to an unique class @String
1 void Stat_Alone(char *Master_P) 2 { 3 Catch_Letter(Master_P); 4 sort(Sort_Word.begin(), Sort_Word.end()); 5 reverse(Sort_Word.begin() ,Sort_Word.end()); 6 cout << "total :" << Total << endl << endl; 7 8 int count = 20; 9 for (vector<String>::iterator iter = Sort_Word.begin(); iter != Sort_Word.end(); iter++)/ 10 { 11 printf("Word : %-15s Frequency :%d ", iter->Word_Point, iter->Frequency); 12 if (!count--) break; 13 } 14 printf("......."); 15 Total = 0; 16 Sort_Word.clear(); 17 }
distinguish which funtion according to the @cin from @console and read each file form the folder, and statist that file alone
1 void main(int argv ,char **argc) 2 { 3 time_t start = clock(); 4 5 if (strcmp(argc[1], "folder")) Stat_Alone(argc[1]); 6 else 7 { 8 char *Folder = (char *)malloc(40*sizeof(int)); 9 FILE *Folder_P = fopen("folder.txt" ,"r"); 10 while (!feof(Folder_P)) 11 { 12 if (feof(Folder_P)) break; 13 14 fscanf(Folder_P, "%s", Folder); 15 Stat_Alone(strcat(Folder, "\article.txt")); 16 } 17 } 18 19 printf("Use time: %.0f ms" ,double(clock() - start)*1000/CLOCKS_PER_SEC); 20 getchar(); 21 }
单元测试:
功能1
功能2
功能3
功能4
实现了功能,但是巨慢,但还好没崩溃