Fork仓库的Github项目地址 https://github.com/xymoon/WordCount.git
|结对伙伴|学号|博客地址|
|:-|:-|:-|
|赵哲一|201831082223|https://www.cnblogs.com/plough-z/|
1.功能分析
打印出文件中的字符总数,有效行数,英文字母数,字母数字数,空格数,出现次数最多的10个单词。
2.编译器
VCExpress2010学习板(注意编译器,fopen,和头文件不同编译器可能不一样)
3. 计算模块接口的设计与实现过程。
1.读取文件。fopen打开存在D盘上的input.txt文件,fgetc将字符读入,并创建一个链表,链表结点分别存char,和next。
struct link { char ch0; struct link *next; }; while((ch=fgetc(fp))!=EOF) { if((ch>=32&&ch<=126)||ch=='\n') { p=(struct link *)malloc(sizeof(struct link)); p->ch0=ch; p->next=NULL; s->next=p; s=s->next; } }
2.索引链表,得出字符数,空格数,字母数字数,英文字母数,并将其中英文字母数全部转为小写字母并赋值给muchuan。
while(a) { cout<<a->ch0; if(a->ch0>31&&a->ch0<127){zifushu++;} if(a->ch0==32) konggeshu++; if((a->ch0<58&&a->ch0>46)||(a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)) zhimushuzi++; if((a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)) { yingwenzimu++; if(a->ch0<91&&a->ch0>64){c=a->ch0+32;muchuan=muchuan+c;} else{muchuan=muchuan+a->ch0;} } }
3.截取单词。创建2个队列,一个队列存char,一个队列存string。在上一个链表中索引,依次将英文字母转为小写并存到char队列中,一旦遇到space或者\n则检查char队列长度是否>=4如果是这全部出队赋值给s,并将s入队到string队列。
queue <char> q1; queue <string> q2; if((a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)||a->ch0==32||a->ch0==33||a->ch0==44||a->ch0==46||a->ch0=='\n') { if(a->ch0<91&&a->ch0>64) { q1.push((a->ch0)+32); } else if(a->ch0<123&&a->ch0>96) { q1.push(a->ch0); } else { if(q1.size()>=4) { ss=""; while(q1.size()!=0) { ss=ss+q1.front(); q1.pop(); } q2.push(ss); } else while(q1.size()!=0){q1.pop();} } } a=a->next;
4.词频记录。创建一个匹配次数函数chuxiancishu(string muchuan).建立vector(WORD)arr(0);匹配一个将其插入arr;插入时索引判断arr中是否有同样的单词,有就插入,没有就进入下一个循环。
struct WORD { string word; int num; struct WORD *next; }; int panduan(const vector<WORD>&arr,string s) { int i=1; for(i=0;i<arr.size();i++) { if(arr[i].word==s)return 0; } return 1; } int chuxiancishu(string muchuan) { int a=0,n=q2.size(); string zichuan; vector<WORD> arr(0); while(q2.size()!=0) { WORD p; zichuan=q2.front(); q2.pop(); p.num=counter(zichuan,muchuan); p.word=zichuan; if(panduan(arr,zichuan)) { arr.push_back(p); } } dayin(arr); return 0; }
5.打印前10个次数最多的词。dayin(vector
int dayin(vector<WORD>&arr) { int max,i=0,a=0,counter=0; string word; max=arr[0].num; word=arr[0].word; while(counter!=10) { for(i=0;i<arr.size();i++) { if(arr[i].num>max) { max=arr[i].num; word=arr[i].word; a=i; } } cout<<word<<"出现次数"<<max<<endl; max=0; arr[a].num=0; counter++; } return 0; }
4.代码复审过程
主要就是一些格式问题,两个人在写完后格式有所不同,综合起来 保持一致。
5.计算模块接口部分的性能改进
由于没有使用vs编译器,也就没有性能分析图
6.部分单元测试展示
string shu(link *a) { char c; string ss="",muchuan=""; while(a) { cout<<a->ch0; if(a->ch0>31&&a->ch0<127){zifushu++;} if(a->ch0==32) konggeshu++; if((a->ch0<58&&a->ch0>46)||(a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)) zhimushuzi++; if((a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)) { yingwenzimu++; if(a->ch0<91&&a->ch0>64){c=a->ch0+32;muchuan=muchuan+c;} else{muchuan=muchuan+a->ch0;} } if((a->ch0<91&&a->ch0>64)||(a->ch0<123&&a->ch0>96)||a->ch0==32||a->ch0==33||a->ch0==44||a->ch0==46||a->ch0=='\n') { if(a->ch0<91&&a->ch0>64) { q1.push((a->ch0)+32); } else if(a->ch0<123&&a->ch0>96) { q1.push(a->ch0); } else { if(q1.size()>=4) { ss=""; while(q1.size()!=0) { ss=ss+q1.front(); q1.pop(); } q2.push(ss); } else while(q1.size()!=0){q1.pop();} } } a=a->next; } return muchuan; }
7·结对过程
测试结果
目前测试结果,可以放任意英语作文,不管几篇都可以。主要访问的是“D:\input.txt”。