test4 结对项目
[必做 1] 基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。
结对对象:袁晨晖 博客地址:http://www.cnblogs.com/ychhhh/
github地址:https://github.com/yuanchenhui/zuoye
双方贡献 1:1
结对照片:
1 #include<iostream> 2 #include<fstream> 3 #include<cstring> 4 #include<string> 5 using namespace std; 6 7 struct Word{ //定义结构体 8 int Count;//计数器 9 Word() : Str(""), Count(0) {} 10 string Str; //字符串 11 char *p; 12 }; 13 14 15 void exchange(Word &word) //函数,用于交换单词(排序单词) 16 { 17 string tStr = word.Str; 18 int tCount = word.Count; 19 word.Str = Str; 20 word.Count = Count; 21 Str =tStr; 22 Count = tCount; 23 } 24 25 Words test[100]; 26 27 void lwr(char x[]) //大写转小写 28 { int k = 0; 29 while (x[k] != '\0') 30 { 31 if (x[k] >= 'A'&&x[k] <= 'Z') 32 x[k] = x[k] + 32; 33 k++; 34 } 35 } 36 37 int identify(char a[]) //判断是否符合单词的定义 38 { int m=(strlen(a)>=4)?1:0; 39 int n=(a[0]>='a'&&a[0]<='z')?1:0; 40 if(!m||!n) 41 return 0; 42 else 43 while(a) 44 { for(int i=1;;i++) 45 { 46 if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9')) 47 return 0; 48 else 49 return 1; 50 } 51 } 52 } 53 54 void SortWordDown(Word * words, int size) //以单词出现频率降序排列单词,words 单词数组,size 单词数量 55 { 56 for(int i=0;i<size;i++) 57 { 58 for(int j=0;j <size-1;j++) 59 { 60 if(words[j].Count<words[j+1].Count) 61 { 62 words[j].exchange(words[j+1]); 63 } 64 } 65 } 66 } 67 68 int counting(char b[],int num) //对出现次数计数 69 { for(int j=0;j<num;j++) 70 { 71 72 if(!strcmp(b,test[j].p)) 73 test[j].count++; 74 else 75 return 0; 76 } 77 } 78 79 80 int main( void ) 81 { 82 char result[500]; 83 84 char *ptr; 85 ifstream file( "E://A_Tale_of_Two_Cities.txt" ); /* 读取 */ 86 if ( !file ) 87 { 88 cout << "不能打开文件"; 89 } 90 while ( !file.eof() ) 91 { 92 file.getline( result, 500 ); 93 } 94 file.close(); 95 int j = 0; /* 大写转小写 */ 96 while ( result[j] != '/0' && result[j + 1] != '/0' ) 97 { 98 if ( result[j] >= 'A' && result[j] <= 'Z' ) 99 { 100 result[j] = result[j] - 'A' + 'a'; 101 j++; 102 } 103 } 104 cout << result; 105 char *sep = " "; 106 107 int i = 0; 108 ptr = strtok( result, " " ); /* 利用strtok函数来分割result字符串中的单词 */ 109 while ( ptr != NULL ) 110 { 111 if ( isword( p ) != false ) 112 { 113 if ( judge( p, n ) != false ) 114 { 115 w[n].s = *p; /* 赋值给数组 */ 116 n++; 117 } 118 } 119 ptr = strtok( NULL, " " ); 120 } 121 int t = 0; 122 ofstream outfile; /* 输出文件到result1 */ 123 outfile.open( "Result1.txt" ) 124 SortWordDown( w, count ); 125 while ( w[t].s ) /* 输出统计结果 */ 126 { 127 if ( strlen( w[t].s ) >= 4 ) 128 { 129 outfile << w[t].s << ":" << w[t].count << '\n'; 130 t++; 131 } 132 } 133 return(0); 134 }
运行结果:
:
借鉴资料:百度了各部分的主题思路,得到模板,怎么解决,问同学。
心得:
互相教对方,可以得到能力上的互补。
可以让编程环境有效地贯彻Design。
有效的减少BUG。
降低学习成本。一边编程,一边共享知识和经验,有效地在实践中进行学习。
在编程中,相互讨论,可能更快更有效地解决问题。