https://github.com/jimons/personal-projectios
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
Planning | 計劃 | 30 | 50 |
· Estimate | · 估計這個任務須要多少時間 | 30 | 30 |
Development | 開發 | 400 | 20 |
· Analysis | · 需求分析 (包括學習新技術) | 30 | 240 |
· Design Spec | · 生成設計文檔 | 20 | 50 |
· Design Review | · 設計複審 | 20 | 30 |
· Coding Standard | · 代碼規範 (爲目前的開發制定合適的規範) | 20 | 20 |
· Design | · 具體設計 | 70 | 45 |
· Coding | · 具體編碼 | 50 | 120 |
· Code Review | · 代碼複審 | 50 | 240 |
· Test | · 測試(自我測試,修改代碼,提交修改) | 40 | 200 |
Reporting | 報告 | 100 | 160 |
· Test Repor | · 測試報告 | 10 | 30 |
· Size Measurement | · 計算工做量 | 15 | 30 |
· Postmortem & Process Improvement Plan | · 過後總結, 並提出過程改進計劃 | 80 | 100 |
合計 | 965 | 1365 |
統計文件的字符數:c++
打開文件,而後依次讀入,判斷它們的ASCII碼是不是在1——127之間,若是是則累加,若是不是,則無論它繼續遍歷。git
統計文件的單詞總數:github
從新再打開一次文件,而後每次讀入一行判斷是不是單詞,若是前面4個是字母,則這行一直遍歷到不是字母和數字的其餘符號,都肯定爲分隔;一旦肯定爲分隔符後,又用substr()減去前面的字母,剩餘的字符串繼續判斷是否爲字母,不過判斷的時候須要注意到是數字+字母*4+組成的單詞仍是分隔符+字母*4+,若是前者,則不計入單詞,後者則計入。這個計算單詞數我是定爲int words_count()來調用。網絡
統計文件的有效行數:數據結構
這個我是根據逐行讀取,而後一行一行判斷,主要就是判斷是否爲空行。我是根據一行讀下來,若是下一行讀是空行,而該行的上面一行也是空行,則肯定該行爲空行,就不加,不然行數就++。函數
統計文件中各單詞的出現次數,最終只輸出頻率最高的10個。頻率相同的單詞,優先輸出字典序靠前的單詞:學習
這個我利用了set函數,而後利用哈希樹,將e1的頻數*100000(好多個)+e2.字母*27*27*27(依次減小,由於26個字母),而後就直接vector,依次排出來啦!這個一次就成功了,前面的代碼真的是改到我要吐血了!測試
應做業要求,代碼組織以下編碼
1.get_lines()
註釋:這個代碼就是從文件中讀取出來,而後++,就能夠了。
int get_lines() { fp2 = fopen("WordCount.txt","r+"); char c,ch='0'; int lines=0; while((c = fgetc(fp2)) != EOF) { if(c == '\n' && ch != '\n') lines++; ch = c; } if(ch != '\n') lines++; return lines; }
2.characters_get()
註釋:這個也是從文件夾中再讀取出來,而後直接就能夠獲得全部的字符數。
int characters_get() { int characters=0; fp1 = fopen("WordCount.txt","r+"); char c; while((c = fgetc(fp1)) != EOF) { if(c >= 0 && c <= 127) characters++; } return characters; }
3.這兩個分別是計算單詞的數目和top10,top10是直接用set函數,vector而後輸出來的。
void words_top10() int words_count()
4.下面這個就是主函數main()
註釋:按題目要求依次輸出characters、lines、words、以及字典順序的單詞。
int main() { double t = clock(); int lines,words,characters; characters=characters_get(); cout<<"characters:"<<characters<<endl; lines=get_lines(); cout<<"lines:"<<lines<<endl; words=words_count(); cout<<"words:"<<words<<endl; words_top10(); cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" >"<<endl; return 0; }
5.頭文件:
註釋:用結構體與set函數、哈希結合使用來構造字典排序的做用,仍是蠻暴力的。
#include<stdio.h> #include<iostream> #include<stdlib.h> #include<cmath> #include<vector> #include<cstring> #include<stack> #include<set> #include<string.h> #include<ctime> using namespace std; #define MAXN 15000//宏定義 FILE *fp1,*fp2; struct word{ char letter[100]; int num; int id; }; struct compare{ bool operator()(const word& e1, const word& e2)const{ if( e1.letter[0]*27*27*27*27*27*27+e1.letter[1]*27*27*27*27*27+e1.letter[2]*27*27*27*27+e1.letter[3]*27*27*27+e1.letter[4]*27*27+e1.letter[5]<=e2.letter[0]*27*27*27*27*27*27+e2.letter[1]*27*27*27*27*27+e2.letter[2]*27*27*27*27+e2.letter[3]*27*27*27+e2.letter[4]*27*27+e2.letter[5]) return true; return false; } }; struct comparenum{ bool operator()(const word& e1, const word& e2)const{ if(e1.num*100000000000000000+e2.letter[0]*27*27*27*27*27*27+e2.letter[1]*27*27*27*27*27+e2.letter[2]*27*27*27*27+e2.letter[3]*27*27*27+e2.letter[4]*27*27+e2.letter[5]>=e2.num*100000000000000000+ e1.letter[0]*27*27*27*27*27*27+e1.letter[1]*27*27*27*27*27+e1.letter[2]*27*27*27*27+e1.letter[3]*27*27*27+e1.letter[4]*27*27+e1.letter[5]) return true; return false; } }; set<word,compare>wordin; set<word,comparenum>wordnum; int idnum[10];
Dev-c++測試:
測試案例:
測試結果:
準備將代碼copy 到VS上時,首先在git clone 時候,我一直在懷疑是網絡的問題,結果每到39%的時候,我就在手機熱點與無線之間來回切換,切換一次它就失敗一次,從新來過.....結果百度才發現..它就是這麼慢。。。。。。。。。。