作业4:结对项目——词频统计
2016-03-29 18:12
281 查看
1. 目标
代码复审练习结对练习
编写单元测试
2. 要求
[必做 2] 读取小文本文件A_Tale_of_Two_Cities.txt 或者 大文本文件Gone_with_the_wind.txt,统计某一指定单词在该文本文件中出现的频率。命令行格式: 提示符> Myapp.exe -f filename.txt -w word (PS:C++ 程序,Java 程序输出方式类似)
解释:
选项 -f 表示打开某一文件(filename.txt)
选项 -w 表示统计其后单词(word)在打开的文件(filename.txt)中的频率
结对对象纪杨阳的博客地址 :http://home.cnblogs.com/u/yangyangji/
编写系统的Github 链接:https://github.com/yangyangji/ex.2
双方的贡献比例:1:1
结对编程照片如下:
源代码如下:
#include<iostream> #include<fstream> #include<cstring> #include<string> using namespace std; struct Word{ //定义结构体 int Count;//计数器 Word() : Str(""), Count(0) {} string Str; //字符串 char *p; }; void exchange(Word &word) //函数,用于交换单词(排序单词) { string tStr = word.Str; int tCount = word.Count; word.Str = Str; word.Count = Count; Str = tStr; Count = tCount; } Words test[100]; void lwr(char x[]) //大写转小写 { int k = 0; while (x[k] != '\0') { if (x[k] >= 'A'&&x[k] <= 'Z') x[k] = x[k] + 32; k++; } } int identify(char a[]) //判断是否符合单词的定义 { int m=(strlen(a)>=4)?1:0; int n=(a[0]>='a'&&a[0]<='z')?1:0; if(!m||!n) return 0; else while(a) { for(int i=1;;i++) { if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9')) return 0; else return 1; } } } void SortWordDown(Word * words, int size) //以单词出现频率降序排列单词,words 单词数组,size 单词数量 { for(int i=0;i<size;i++) { for(int j=0;j <size-1;j++) { if(words[j].Count<words[j+1].Count) { words[j].exchange(words[j+1]); } } } } int counting(char b[],int num) //对出现次数计数 { for(int j=0;j<num;j++) { if(!strcmp(b,test[j].p)) test[j].count++; else return 0; } } int main() { char c[200]; ifstream fin("D:/A_Tale_of_Two_Cities.txt"); //从文档中获取字符串 for(int f=0;;f++) fin>>c[f]; fin.close(); cin.get(); lwr(c); const char *delim = ",”“.''!?"; //分割字符串 char *q; int n=0; q = strtok(c, delim); SortWordDown(words, wCount); while (q) { if (identify(q)) { strcpy(test .p,q); n++; } q=strtok(NULL,delim); } cout<<"输入统计出现频数的指定单词:\n"; char r; cin>>r; int t=0; while(!strcmp(test .p,r)) //找到指定单词 { t++; } cout<< test .p << ":" << test .count << '\n'; //输出指定单词的统计结果 return 0; }
实验结果:
实验体会:
由于进行了合作,实验效率得到了极大的提高,各自擅长的地方得到了发挥。
相关文章推荐
- android Shape全解析——再也不担心包size了
- linux下IPTABLES配置详解
- linux系统调用和库函数调用的区别
- Linux下java获取CPU、内存、磁盘IO、网络带宽使用率
- Mac下导出chrome插件
- TCP关闭连接
- 解决android方法数量超过65536
- MFC-创建进程
- iOS设置启动页面
- POJ 2236 Wireless Network(并查集)
- UIView背景图片设置一些技巧
- linux下多进程、多线程编程
- 操作系统 *** 实现信号量
- 优车车欲用Uber模式和传统驾校一起打一场漂亮仗
- DocuPrint M118 w的安装到苹果mac步骤
- 一位项目经理的博客内容
- codeforces 17 D Notepad 快速幂?
- Spark中组件Mllib的学习1之Kmeans错误解决
- 通过lrzsz轻松实现Windows/Linux之间文件的上传/下载
- 计算机网络传输层的功能