單詞統(tǒng)計(jì)程序C++_第1頁(yè)
單詞統(tǒng)計(jì)程序C++_第2頁(yè)
單詞統(tǒng)計(jì)程序C++_第3頁(yè)
單詞統(tǒng)計(jì)程序C++_第4頁(yè)
單詞統(tǒng)計(jì)程序C++_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、單詞統(tǒng)計(jì)問題描述文字研究人員需要統(tǒng)計(jì)某篇英文小說中某些特定單詞的出現(xiàn)次數(shù)和位置,試寫出一個(gè)實(shí)現(xiàn)這一目標(biāo)的文字統(tǒng)計(jì)系統(tǒng)。這稱為“文學(xué)研究助手”。要求算法輸入:文本文件和詞集。算法輸出:?jiǎn)卧~出現(xiàn)的次數(shù),出現(xiàn)位置所在行的行號(hào)(同一行出現(xiàn)兩次的只輸出一個(gè)行號(hào))。算法要點(diǎn):(1)文本串非空且以文件形式存放。(2)單詞定義:用字母組成的字符序列,中間不含空格,不區(qū)分大小寫。(3)待統(tǒng)計(jì)的單詞不跨行出現(xiàn),它或者從行首開始,或者前置一個(gè)空格。(4)數(shù)據(jù)結(jié)構(gòu)采用二維鏈表,單詞結(jié)點(diǎn)鏈接成一個(gè)鏈表,每個(gè)單詞的行號(hào)組成一個(gè)鏈表,單詞結(jié)點(diǎn)作為行號(hào)鏈表的頭結(jié)點(diǎn)。需求分析用戶需求:用戶可以通過該程序查詢和統(tǒng)計(jì)一篇英文文章中

2、某些特定單詞出現(xiàn)次數(shù)和位置。功能需求:用戶可以輸入單詞來查詢單詞出現(xiàn)次數(shù)和位置; 程序可以正確顯示查詢結(jié)果; 用戶可以選擇是否在一次輸出后繼續(xù)查詢; 在一次查詢中的結(jié)果記錄到一個(gè)二維鏈表中。概要設(shè)計(jì) 為達(dá)到設(shè)計(jì)要求,本程序采用二維鏈表存儲(chǔ)單詞結(jié)點(diǎn)和相關(guān)的位置信息。抽象數(shù)據(jù)類型:struct nodeint col; /行坐標(biāo)int row; /所在行的列坐標(biāo)node* next; /指向下一個(gè)坐標(biāo)結(jié)點(diǎn)的指針; /單詞坐標(biāo)坐點(diǎn)類型struct nodechar words20; /單詞數(shù)組node* ptr; /指向單詞坐標(biāo)結(jié)點(diǎn)的指針node* next; /指向下一個(gè)單詞結(jié)點(diǎn)的指針int nu

3、m; /單詞所含字符個(gè)數(shù); /單詞結(jié)點(diǎn)class tlinkpublic:tlink() head = null; /構(gòu)造函數(shù)tlink() /析構(gòu)函數(shù)while( head != null )node* temp;temp = head;head = head - next;delete temp;void insert( char* item );/前條件:參數(shù)item為一個(gè)字符數(shù)組。/后條件:item所包含的單詞被插入到鏈表。void calcute(char *szfile,int size);/前條件:szfile以正確保存了文本字符,size為文本字符長(zhǎng)度。/后條件:統(tǒng)計(jì)鏈表每一個(gè)

4、插入的單詞的個(gè)數(shù)及所在行、列坐標(biāo)。node* gethead();/前條件:鏈表已初始化/后條件:返回鏈表頭指針private:node* head;char a_to_a( char alp );/前條件:alp為一個(gè)正確的英文字母/后條件:如果alp是大寫字母,則轉(zhuǎn)化為小寫字母,否則不變。void showwindow();/后條件:顯示統(tǒng)計(jì)結(jié)果。void show_text();/前條件:在正確的路徑上存在一個(gè)英文文本。/后條件:讀入英文文本到字符數(shù)組并顯示在屏幕上。void input();/后條件:讀入用戶輸入的字符并插入到單詞鏈表。數(shù)據(jù)結(jié)構(gòu)圖解: 將從文件流讀入的文章字符存到szf

5、ile字符數(shù)組中,以空格計(jì)數(shù)行單詞個(gè)數(shù),以換行符記錄文章列數(shù),將輸入后插入到鏈表中的單詞與字符數(shù)組中的單詞比較,遇到相等的則將當(dāng)前的行列數(shù)插入到鏈表的位置結(jié)點(diǎn)中,并且單詞個(gè)數(shù)加1。本程序允許用戶選擇是否重復(fù)進(jìn)行,并且對(duì)于在一次操作中重復(fù)輸入的單詞,在鏈表中不進(jìn)行重復(fù)插入。功能模塊: 模塊調(diào)用: 程序流程圖:主要模塊偽碼概要設(shè)計(jì):插入函數(shù)(參數(shù):item【】數(shù)組): 新建單詞結(jié)點(diǎn);while( itemi != 0 ) 復(fù)制各字符到單詞結(jié)點(diǎn)的單詞數(shù)組中; 記錄單詞字母?jìng)€(gè)數(shù); 插入單詞字母?jìng)€(gè)數(shù)到鏈表相應(yīng)域;temp - wordsi = 0;查找并比較鏈表中是否已有要插入的單詞; 如果有 不進(jìn)行插

6、入并銷毀新建結(jié)點(diǎn); 否則 插入新建的單詞結(jié)點(diǎn);/*/統(tǒng)計(jì)函數(shù)( 參數(shù):char *szfile, int size)while( 沒有到文本字符的結(jié)尾 )依次遍歷文本字符,遇到非字母字符時(shí)計(jì)數(shù)單詞所含字母?jìng)€(gè)數(shù);while( 沒有到鏈表尾 )if( 單詞結(jié)點(diǎn)所含單詞的字母?jìng)€(gè)數(shù)與計(jì)數(shù)相等 )忽略大小寫比較本次遍歷到的單詞和鏈表中的結(jié)點(diǎn)單詞; if(相等) 插入行列值 遇空格行單詞個(gè)數(shù)加1; 遇換行列數(shù)加1;/*/結(jié)構(gòu)顯示模塊while( 沒到鏈表尾 )輸出結(jié)點(diǎn)單詞;如果存在,則輸出單詞的所有行、列值及出現(xiàn)次數(shù); 否則輸出沒有該詞或輸入錯(cuò)誤信息;/*/輸入模塊doif( 鏈表不空 )清空鏈表;輸出提

7、示信息;接受用戶輸入; while( true )輸入的是結(jié)束字符; 退出;記錄輸入逗號(hào)前的單詞;插入到鏈表;繼續(xù)輸入;if( 鏈表空 )輸出 沒有插入任何單詞?。籩lse 統(tǒng)計(jì); 顯示結(jié)果;提示是否繼續(xù);輸入;while( 不繼續(xù) );詳細(xì)設(shè)計(jì)程序清單:/file:source.h#ifndef source_h#define source_hstruct nodeint col;int row;node* next;struct nodechar words20;node* ptr;node* next;int num;class tlinkpublic:tlink() head = nu

8、ll; tlink()while( head != null )node* temp;temp = head;head = head - next;delete temp;void insert( char* item );void calcute(char *szfile,int size);node* gethead();private:node* head;char a_to_a( char alp );void showwindow();void show_text();void input();#endif#include#include#include#include source

9、.husing namespace std;tlink link;int i=0;char szfile2000;int main()show_text();cout wordsi = itemi;+ i;temp - num = i;temp - wordsi = 0;node* ptrr = null;ptrr = link.gethead();while( ptrr != null ) if( ptrr - num = temp - num )int n;for( n = 0; n wordsn ) != a_to_a( itemn ) )break;if( n = i )flag =

10、1;break; ptrr = ptrr - next; if( flag != 1 ) temp - ptr = null; temp - next = null; node* temp = head; if( head = null ) head = temp; else while( temp - next != null ) temp = temp - next; temp - next = temp; elsedelete temp;/*/char a_to_a( char alp )if( ( alp = a ) & ( alp = z ) )alp = alp + 32;retu

11、rn alp;/*/void tlink:calcute(char *szfile, int size)/cout calcute is called! endl;int i = 0; /記錄已搜索過的字符數(shù)-1intcol = 1;/列標(biāo)int row = 0;/行標(biāo)int count;/記錄空格數(shù)-1node* ptrr = null;while( i = a & szfilei = a & szfilei num = j )int n;for( n = 0; n wordsn ) != a_to_a( szfilei - j + n ) )break;if( n = j )node* t

12、emp;temp = new node;temp - col = col;temp - row = row;temp - next = null;node* temp = ptrr - ptr; if( ptrr - ptr = null )ptrr - ptr = temp;else while( temp - next != null ) temp = temp - next; temp - next = temp;/插入行數(shù)ptrr = ptrr - next;if( szfilei = | szfilei = n )count = -1; while( szfilei = ) + i;

13、 /設(shè)置列數(shù) + row;/行的單詞個(gè)數(shù)加 + count;/單詞之間空格-1 row = row - count; if( szfilei = n ) + col; /列遇到換行累加 + i;row = 0;/單詞的行個(gè)數(shù)清零 else+ i;cout wordsk != 0; + k )cout wordsk;cout ptr = null )cout 沒有該詞,或輸入不正確! ptr != null )cout (;cout ptr - col ;cout ,;cout ptr - row ;cout );cout ptr = curptr - ptr - next;word_num +

14、;cout endl;cout 該單詞共出現(xiàn) word_num 次! next;/*/void show_text()ifstream fin;fin.open(d:english.txt);if (fin.fail()coutinput file opening failed.n;exit(1);char next;fin.get(next);while (! fin.eof() szfilei = next;+ i; fin.get(next);szfilei = 0;for( int k = 0; k i; + k )cout szfilek;cout *total number : i

15、 endl;cout * endl;/*/void input()char item40; /暫存數(shù)組char in; /接受輸入字符char ans; /判斷是否重新開始doif( link.gethead() != null )link.tlink();cout 請(qǐng)輸入要統(tǒng)計(jì)的單詞,單詞之間用逗號(hào)隔開(輸入鍵結(jié)束,本程序忽略空格): in;int flag = 1;while( true )if( in = )break;int m = 0;while( in != , ) itemm = in;+ m;cin in;if( in = ) flag = 0; break;itemm = 0

16、;link.insert( item );if( flag = 0 ) break;cin in;if( link.gethead() = null )cout 沒有插入任何單詞! endl;else link.calcute( szfile, i ); showwindow();cout ans;while( ( ans != n ) & ( ans != n ) );運(yùn)行結(jié)果 結(jié)果分析輸入要查找的單詞之后,單詞插入鏈表,停止輸入后,程序開始在文本字符中查找鏈表中的單詞。程序從文本數(shù)組順次掃描,并在掃描到空格時(shí)記錄一個(gè)單詞的掃描結(jié)束,并記錄單詞所含字母?jìng)€(gè)數(shù),然后查找鏈表,如有和該單詞字母?jìng)€(gè)數(shù)

17、相同的記錄則進(jìn)行比較,否則繼續(xù)查找下一個(gè)直到鏈表尾。此后程序繼續(xù)掃描文本字符數(shù)組的下一個(gè)單詞并和鏈表中單詞進(jìn)行相同的比較過程,直到字符數(shù)組掃描完畢。要想在輸出結(jié)果后繼續(xù),則選擇y或y繼續(xù),否則輸入n或n退出。最后輸出結(jié)果。本次運(yùn)行中第一次操作輸入is,are,這是正確的輸入,則輸出正確的結(jié)果;第二次選擇y繼續(xù),輸入the,這也是正確的輸入,則輸出正確結(jié)果;第三次選擇y繼續(xù),輸入microsoft,22%,microsoft,一、三個(gè)輸入正確,但是同樣的輸入,則按照程序設(shè)計(jì),只輸出一次結(jié)果,第二次輸入錯(cuò)誤,則程序報(bào)告輸入有誤。選擇n,程序按預(yù)期結(jié)束。收獲與體會(huì)通過本次的課程設(shè)計(jì)實(shí)驗(yàn),是我對(duì)數(shù)據(jù)結(jié)

18、構(gòu)的設(shè)計(jì)編程有了更好的理解,進(jìn)一步學(xué)習(xí)了把學(xué)到的知識(shí)運(yùn)用到實(shí)際的一些問題解決中去。鞏固了和提高了編程的能力,積累了一些編程技巧和經(jīng)驗(yàn)。程序設(shè)計(jì)是選擇一種好的結(jié)構(gòu),然后設(shè)計(jì)一個(gè)好的算法。數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)的好壞會(huì)直接影響到程序算法設(shè)計(jì)的可實(shí)現(xiàn)性及難易程度。在課程設(shè)計(jì)的過程中使我更深刻認(rèn)識(shí)到結(jié)構(gòu)設(shè)計(jì)的重要性,有時(shí)在結(jié)構(gòu)中加一個(gè)記錄一個(gè)數(shù)字的變量也許會(huì)使程序代碼更明朗和易于設(shè)計(jì)。程序設(shè)計(jì)是一項(xiàng)看似枯燥卻會(huì)有無(wú)窮樂趣的事情,我們?cè)谧鼍幊淘O(shè)計(jì)時(shí),需要極其的認(rèn)真、求實(shí)、耐心和信心。一點(diǎn)的錯(cuò)誤都不會(huì)使我們得到預(yù)期的結(jié)果,所以,我要繼續(xù)認(rèn)真學(xué)習(xí)和練習(xí),多動(dòng)手動(dòng)腦,循序漸進(jìn)的提高自己的程序設(shè)計(jì)能力。附:讀入的文本:0

19、1 microsoft windows is a complex operating system. it offers02 so many featuresand does so much that its impossible for 03 any one person to fully understandthe entire system. this 04 complexity also makes it difficult for someone to decide05 where to start concentrating the learning effort. well, i

20、 06 always like to start at the lowest level by gaining a solid 07 understanding of the systems basic building blocks. once 08 you understand the basics, its easy to incrementally 09 add any higher-level aspects of the system to your knowledge. 10 so this book focuses on windows basic building blocks and11 the fundamental concepts that you must know when architecting 12 and implementing software targeting the windows operating system. 13 in short, this book teaches the reader about various windows features 14 and how to access them via the c and c+ programming languages.15 altho

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論