項目 | 內容 |
---|---|
這個做業屬於哪一個課程 | 軟件工程課程 |
這個做業的要求在哪裏 | 結對項目-最長單詞鏈 |
我在這個課程的目標是 | 掌握現代軟件工程的基本知識,與團隊一塊兒進行軟件開發。 |
這個做業在哪一個具體方面幫助我實現目標 | 與另外一位同窗一塊兒結對編程,初步體會了軟件開發。 |
Github項目地址node
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | 20 | |
·Estimate | ·估計這個任務須要多少時間 | 20 | |
Development | 開發 | 1710 | |
·Analysis | ·需求分析(包括學習新技術) | 20 | |
·Design Spec | ·生成設計文檔 | 20 | |
·Design Review | ·設計複審(和同事審覈設計文檔) | 20 | |
·Coding Standard | ·代碼規範(爲目前的開發制定合適的規範) | 20 | |
·Design | ·具體設計 | 30 | |
·Coding | ·具體編碼 | 1000 | |
·Code Review | ·代碼複審 | 300 | |
·Test | ·測試(自我測試,修改代碼,提交修改) | 300 | |
Reporting | 報告 | 230 | |
·Test Report | ·測試報告 | 200 | |
·Size Measurement | ·計算工做量 | 10 | |
·Postmortem & Process Improvement Plan | ·過後總結,並提出過程改進計劃 | 20 | |
合計 | 1960 |
把計算最長鏈的函數單獨寫成一個Core類,在Core類中提供兩個接口,int gen_chain_word
和int gen_chain_char
分別用於計算單詞最多的鏈和字母最多的鏈,其餘函數屬性爲private,隱藏在文件中,不能被其餘東西調用。其餘人在使用的時候只能經過gen_chain_word
和gne_chain_char
這兩個接口來使用。git
計算模塊包含一個Core類,其中有兩個可供外部調用的接口github
int gen_chain_word(char* words[], int len, char* result[], char head, char tail, bool enable_loop); int gen_chain_char(char* words[], int len, char* result[], char head, char tail, bool enable_loop);
類中還有其餘八個函數編程
void newnode(string word); void addtomap(node newword); void toforest(); void next1(vector<int> forward, int root); void next2(vector<int> forward, int root); int findmostwords(char head, char tail); int findlongest(char head, char tail); int listlength(int index);
各函數之間的調用流程
微信
改進計算模塊性能所花費的時間:2小時。網絡
第一個版本的程序中,不管面對什麼狀況,都會把有向圖中的大大小小的全部鏈路都分出來,花費了大量的時間。
在不約束首位字母和只約束首字母的狀況下,只按照各個根節點來生成最長的單詞鏈。
在約束尾字母的狀況下,再加入對尾字母的判斷,生成更多的鏈。app
根據分析結果看,消耗最大的函數是 listlength()函數
這個函數的功能是計算單詞鏈字母的長度。
代碼以下函數
int Core::listlength(int index) { int sum = 0; int i = 0; for (i = 0; i < forest[index].size(); i++) { sum += map[forest[index][i]].wordlen; } return sum; }
契約式設計,按照某種規定對一些數據等作出約定,若是超出約定,程序將再也不運行。
優勢oop
缺點post
在編寫的Core模塊中,一開始對傳入的參數進行合法性判斷,須要調用該接口的主體保證數據的合法性。
單元測試的代碼以下
TEST_METHOD(TestMethod8)//-h b -t t -c { Core* core = new Core(); char* result[100]; char* words[] = { "abb","bccccccccccccccccccccc","cccccccccccccccccccccf","ctttt","cbt" }; char* answer[] = { "bccccccccccccccccccccc","ctttt" }; int answerlen = 2; int resultlen = core->gen_chain_char(words, 5, result, 'b', 't', false); Assert::AreEqual(resultlen, answerlen); int i = 0; for (i = 0; i < answerlen; i++) { string stranswer = answer[i]; string strresult = result[i]; Assert::AreEqual(stranswer, strresult); } }
這是一個測試約束首字母爲'b',約束尾字母爲't'的求字母最多的單詞鏈的測試模塊,在測試模塊中words[]傳入全部的單詞,result存輸出的結果,調用core計算模塊中gen_chain_char接口,把返回到result中的結果和預先準備的answer中的答案進行比對。
在測試數據的構造上,主要是構造出有迷惑性的數據好比這一個數據要約束首位字母和字母最多,就構造了一個不符合首尾約束的字母更多的干擾的鏈,來測試程序可否計算正確。
在Core模塊的正確性單元測試中一共設計了12個測試模塊,包含了各類參數相結合的正確狀況。
此異常應對當輸入的單詞可以構成單詞環可是並無輸入-r參數的狀況。
單元測試樣例以下
TEST_METHOD(ErrorTest1)//包含單詞環可是沒有-r參數 { Core* core = new Core(); char* result[100]; char* words[] = { "apple","elephant","tea","alex","box","xob","cccccff","football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc" }; char* answer[] = { "football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc","cccccff" }; int answerlen = 3; try { int resultlen = core->gen_chain_char(words, 9, result, '\0', 'f', false); } catch (exception e) { Assert::AreEqual("單詞文本隱含單詞環", e.what()); } }
其中 football -- llllllllllllllc --- cccccff 能造成單詞環,可是 enable_loop 傳入 false,應該拋出"單詞文本隱含單詞環"異常。
計算模塊接口經過char* words[] 來傳入所有單詞,若是單詞字符串中出現不是字母的狀況,就應該拋出此異常。
單元測試樣例以下
TEST_METHOD(ErrorTest2)//輸入的單詞中有非法字符,elephant 的字母l被空格代替 { Core* core = new Core(); char* result[100]; char* words[] = { "apple","e ephant","tea","alex","box","xob","cccccff","football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc" }; char* answer[] = { "football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc","cccccff" }; int answerlen = 3; try { int resultlen = core->gen_chain_char(words, 9, result, '\0', 'f', false); } catch (exception e) { Assert::AreEqual("單詞包含非法字符", e.what()); } }
輸入的單詞中的第二個單詞"e ephant"的第二個字符不是字母,應該拋出"單詞包含非法字符"異常。
當傳入的head 和 tail 參數既不是合法字母,也不是'\0'字符時拋出異常。
單元測試樣例以下
TEST_METHOD(ErrorTest3)//首尾字母約束不合法,用 -h * { Core* core = new Core(); char* result[100]; char* words[] = { "apple","elephant","tea","alex","box","xob","cccccff","football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc" }; char* answer[] = { "football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc","cccccff" }; int answerlen = 3; try { int resultlen = core->gen_chain_char(words, 9, result, '*', '\0', false); } catch (exception e) { Assert::AreEqual("首尾字母約束不合法", e.what()); } }
在調用gen_chain_char()時,head參數輸入爲'*',既不是字母也不是'\0',不合法,應當拋出"首尾字母約束不合法"異常。
當傳入的words[]中有空字符串的時候,拋出此異常。
單元測試樣例以下
TEST_METHOD(ErrorTest5)//有單詞爲空字符串 " { Core* core = new Core(); char* result[100]; char* words[] = { "apple","","tea","alex","box","xob","cccccff","football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc" }; char* answer[] = { "football","lllllllllllllllllllllllllllllllllllllllllllllllllllllllllc","cccccff" }; int answerlen = 3; try { int resultlen = core->gen_chain_char(words, 9, result, '\0', '\0', false); } catch (exception e) { Assert::AreEqual("有單詞爲空字符串", e.what()); } }
傳入的words[]中的第二個是 "" , 爲空字符串,應當拋出"有單詞爲空字符串"異常。
(1)首先經過main函數從命令行中獲取參數的個數和具體的參數
int main(int argc, char* argv[])
(2)從第二個參數開始依次讀取argv[]中的參數,與"-h","-t","-r","-w","-c"這五個參數進行比較,進行具體處理。
以判斷」-h「參數爲例,部分代碼以下
for (i = 1; i < argc; i++) { parameter = argv[i]; if (strcmp(parameter, "-h") == 0) { if (if_head) { cout << "錯誤:-h參數重複" << endl; exit(0); } if_head = true; i++; parameter = argv[i]; if (strlen(parameter) == 1 && isalpha(parameter[0])) { head_alpha = parameter[0]; } else { cout << "錯誤:-h後沒有字母" << endl; exit(0); } }
在main函數讀入參數以後,直接進行命令行參數的處理,判斷參數都正確以後,根據"-w" 和 "-c" 參數來分別調用Coe模塊中的兩個接口進行計算
if (if_word) { resultlen = core->gen_chain_word(words, wordslen, result, head_alpha, tail_alpha, if_roun); } else if (if_char) { resultlen = core->gen_chain_char(words, wordslen, result, head_alpha, tail_alpha, if_roun); }
我和結對同窗基本上靠網絡交流,經過微信來聯繫,我主要負責程序開發,結對同窗主要對程序進行測試,而後把問題反饋給我。
優勢
缺點
代碼風格不一樣時,須要花時間理解。
對電腦系統的操做不熟悉
比較靦腆,不太愛說話。
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | 1940 | 1410 |
·Estimate | ·估計這個任務須要多少時間 | 1940 | 1410 |
Development | 開發 | 1710 | 1210 |
·Analysis | ·需求分析(包括學習新技術) | 20 | 180 |
·Design Spec | ·生成設計文檔 | 20 | 20 |
·Design Review | ·設計複審(和同事審覈設計文檔) | 20 | 10 |
·Coding Standard | ·代碼規範(爲目前的開發制定合適的規範) | 20 | 10 |
·Design | ·具體設計 | 30 | 30 |
·Coding | ·具體編碼 | 1000 | 600 |
·Code Review | ·代碼複審 | 300 | 180 |
·Test | ·測試(自我測試,修改代碼,提交修改) | 300 | 180 |
Reporting | 報告 | 230 | 220 |
·Test Report | ·測試報告 | 200 | 200 |
·Size Measurement | ·計算工做量 | 10 | 10 |
·Postmortem & Process Improvement Plan | ·過後總結,並提出過程改進計劃 | 20 | 10 |
合計 | 1960 | 1410 |