C實驗_1字符串及基本輸入輸出_第1頁
C實驗_1字符串及基本輸入輸出_第2頁
C實驗_1字符串及基本輸入輸出_第3頁
C實驗_1字符串及基本輸入輸出_第4頁
C實驗_1字符串及基本輸入輸出_第5頁
已閱讀5頁,還剩13頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、暨南大學(xué)本科實驗報告專用紙(附頁)暨南大學(xué)本科實驗報告專用紙課程名稱 C+ 程序設(shè)計 成績評定 實驗項目名稱 字符串及基本輸入輸出 指導(dǎo)教師 實驗項目編號 01 實驗項目類型 驗證 實驗地點 C304 學(xué)生姓名 學(xué)號 學(xué)院 系 專業(yè) 實驗時間 2010年 3月 31 日 上午 一、實驗?zāi)康?熟悉C+的標(biāo)準(zhǔn)輸入、輸出功能; 初步熟悉C+中string對象的使用;二、實驗環(huán)境P4 1.7G計算機一臺,Windows XP操作系統(tǒng),Microsoft Visual Studio 2005編程環(huán)境。三、實驗內(nèi)容及分析根據(jù)題目要求編寫程序,驗證程序的正確性。撰寫實驗報告并提交紙質(zhì)版本,程序源代碼的電子版

2、發(fā)送至教師的Email信箱。(一)帶框架的問候語1. 題意描述:練習(xí)2-4本章在框架程序在輸出上下邊框和問候語之間的空白行時,每次都只輸出一個字符。改寫這個程序,用一個單獨的輸出表達式輸出這個空白行。2. 問題分析與解決問題分析:題意是單獨輸出,即將該空白行作為一個獨立的字符串一次性輸出。空白行輸出部分的程序: 該部分說明程序均以整排輸出。3. 調(diào)試、測試運行情況及結(jié)果分析 (1) 測試數(shù)據(jù)及運行結(jié)果運行結(jié)果:(2) 結(jié)果分析與結(jié)根據(jù)結(jié)果,知程序運行正常并正確。 (二)改進的帶框架的問候語1. 題意描述:改寫第2章的程序,由用戶指定問候語與框架之間的空白符數(shù)目(上下左右共4個數(shù)目,并可能各不相

3、同)。 在問候語周圍的空白區(qū)域由用戶決定,及與其相關(guān)的4個數(shù)不由程序本身給出。2. 問題分析與解決 由1,知需在程序中給出4個由用戶提供的變量值。 通過以下程序?qū)崿F(xiàn):定義四個變量:uprows,downrows,leftpad,rightpad分別表示問候語周圍空白的數(shù)目。3. 調(diào)試、測試運行情況及結(jié)果分析(1) 實驗中碰到的問題及解決方法 無法輸入數(shù)據(jù)。原因:問候語句中出現(xiàn)空白符,可能讀取錯誤。(2) 測試數(shù)據(jù)及運行結(jié)果運行結(jié)果:問候語部分也可由用戶決定?。?) 結(jié)果分析與結(jié)論1:輸入上下左右空白數(shù)依次為2 3 2 4 ,Hello,shuang! 左留出2格空白,右留出4格空白,上面2拍空

4、白行,下面3排空白行。說明程序正確,并正確運行。2:同1。(三)字符*作圖1. 題意描述:輸出一個“*”字符集,并使它們分別構(gòu)成一個長方形,漢字“王”,漢字“田”。由用戶指定長方形的長和寬,“王”字上中下三橫分別的長度及中間一豎的高度(一豎位于三橫的中央),“田”字三橫及三豎的長度(內(nèi)部的橫、豎均位于“田”字的中央)。2. 問題分析與解決長方形:與(一)中的基本類似,只需除去問候語排??梢赃x擇整排輸出(本次選擇)或者單個字符輸出。 漢字“王”與“田”,筆畫單一,可選擇整排與單個字符混合輸出。主函數(shù) int main() ;三個子函數(shù):主函數(shù)通過一個switch 語句對三個子函數(shù)進行調(diào)用。相關(guān)程

5、序: 長方形:采用整排輸出,定義兩個字符串blanks 和 border。漢字“王”:取王的主要比劃并給予變量 up , mid ,down分別表示上中下三橫的長度。然后用Max 函數(shù)取up ,down 的較大值作為整個圖形框的長;最后通過循環(huán)實現(xiàn)。循環(huán)中再使用 if else 進行判斷輸出。循環(huán)體結(jié)構(gòu)基本如上圖所示。漢字“田”:基本思想類似于長方形的畫法。定義變量length 用來給定所畫田的圖框的長度,給一個固定的字符(*)串用來畫田字的上中下三畫。然后通過循環(huán)與判斷實現(xiàn)。3. 調(diào)試、測試運行情況及結(jié)果分析(1) 實驗中碰到的問題及解決方法 在編寫漢字“王”程序時,循環(huán)體進入死循環(huán)。改寫過

6、多次終究實現(xiàn)了。(2) 測試數(shù)據(jù)及運行結(jié)果 運行結(jié)果:(3) 結(jié)果分析與結(jié)論 運行結(jié)果正確。(四)字符串替換1. 題意描述:提示用戶從鍵盤輸入一句話(其中可能包含空格,以EOF標(biāo)記作為結(jié)束,即用戶輸入Ctrl+Z作為輸入結(jié)束),將其中的大寫字母換成對應(yīng)的小寫字母,小寫字母換成對應(yīng)的大寫字母。為避免不雅字眼,還需要將其中的“bullshit”(不論大小寫)替換為“bush”(全部小寫)。最后將替換前后的兩句話分別顯示在屏幕上。 要求: l 使用string對象和應(yīng)用于string對象的函數(shù);l 在輸出替換前后的文字時需去除原輸入語句中多余的空白符(即多于一個的連續(xù)的空白符)。2. 問題分析與解決

7、 首先要解決大、小寫相互轉(zhuǎn)換,可用string 對象的函數(shù)函數(shù)解決。由于要將bullshit 改為bush 則將輸入的字符串分解為一個個詞與 bullshit進行比較判斷,并進行單個輸出。 大小寫互換:“bullshit” 改寫:3. 調(diào)試、測試運行情況及結(jié)果分析(1) 實驗中碰到的問題及解決方法 先前編過另一個程序,出現(xiàn)語法錯誤。因為變量類型不同,無法將字符賦給串,而程序接下來的語句都是針對string類型進行的。后來,找到了新的辦法(本題接下來所用方法),完成實驗。(2) 測試數(shù)據(jù)及運行結(jié)果(3) 結(jié)果分析與結(jié)論 由圖可看出,字母的大小寫均已成功進行轉(zhuǎn)換,而 BullSHIT 也成功換成了

8、 bush ,達到實驗要求。實驗成功,程序正確。四、實驗小結(jié)總的來說,本次試驗比較成功,但仍需繼續(xù)努力!整個過程由自己獨立完成,編程時,一般思路清晰,能夠找到解決辦法。在現(xiàn)實想法改為機器實現(xiàn)時,語言的運用還掌握不夠,解決辦法也并不是最簡單有效地,加上對調(diào)試不熟練,耗時比較多。在試驗1-3中運用了switch語句,由于C語言基礎(chǔ)不太好,上網(wǎng)查了C+中switch的用法,鞏固了下。五、附錄將程序的源代碼用小五號字體打印出來附錄在實驗報告的最后。(一)練習(xí)2-4源程序:#include<iostream>#include<string>/say what standard_l

9、ibrary names we useusing std:cin; using std:endl;using std:cout; using std:string;int main()/ask for the person's namecout<<"Please enter your first name: "/read the name;string name;cin>>name;/build the message that we intend to writeconst string greeting = "Hello, &q

10、uot;+name+ "!"/creat the line of spacesstring blanks=" "string border="*" for(int q=0;q!=greeting.size()+2;+q)blanks=blanks+" "border=border+"*"blanks=blanks+"*" border=border+"*"/the number of blanks surrounding the greetingconst

11、 int pad=1;/the number of rows and columns to writeconst int rows=pad*2+3;const string:size_type cols=greeting.size()+pad*2+2;/write a blank line to separate the output from the inputcout<<endl;/write rows rows of output/invariant:we have written r rows so farfor(int r=0;r!=rows;+r)string:size

12、_type c=0;/invariant:we have written c characters so far in the current rowwhile(c!=cols)/is it time to write the greeting ?if(r=pad+1)cout<<"* "<<greeting<<" *"c=cols;else/are we on the border?if(r=0|r=rows-1)cout<<border; c=cols;else cout<<blanks;c

13、=cols; cout<<endl;return 0;(二)改變問候語周圍空白數(shù)源程序:#include<iostream>#include<string>/say what standard_library names we useusing std:cin; using std:endl;using std:cout; using std:string;int main()/ask for the person's namecout<<"Please enter your greetingwords: "/read

14、 the name;string greetingwords;cin>>greetingwords;/ask for the spaces' numbers of the surroundingscout<<"Please enter the uprows,downrows,leftpad,rightpad: "int uprows,downrows,leftpad,rightpad;cin>>uprows;cin>>downrows;cin>>leftpad;cin>>rightpad;/bu

15、ild the message that we intend to writeconst string greeting =greetingwords;/creat the line of border and the line of spacesstring border="*"string blanks="*" for(int q=0;q!=greeting.size()+ leftpad+ rightpad;+q)blanks=blanks+" "border=border+"*"blanks=blanks+

16、"*" border=border+"*"/creat the greeting linestring greet="*"for(int p=0;p!=leftpad;+p)greet=greet+" " greet=greet+greetingwords; for(int p=0;p!=rightpad;+p)greet=greet+" "greet=greet+"*"/the number of rows and columns to writeconst int row

17、s=uprows+downrows+3;const string:size_type cols=greeting.size()+leftpad+rightpad+2;const string:size_type spaces=greeting.size()+leftpad+rightpad;/write a blank line to separate the output from the inputcout<<endl; /write rows rows of output/invariant:we have written r rows so farfor(int r=0;r

18、!=rows;+r)string:size_type c=0;/invariant:we have written c characters so far in the current rowwhile(c!=cols)/is it time to write the greeting ?if(r=uprows+1)cout<<greet;c=cols;else/are we on the border?if(r=0|r=rows-1)cout<<border; c=cols;elsecout<<blanks;c=cols; cout<<endl

19、;return 0;(三)畫長方形,漢字“王”、“田”源程序:#include<iostream>#include<string>int rectangle();int tian();int Max(int a, int b);int wang();/say what standard_library names we useusing std:cin; using std:endl;using std:cout; using std:string;int rectangle()/ask for the rectangle's length and widthc

20、out<<"Please enter the length and width: "int length,width;cin>>length;cin>>width;/creat the line of border and the line of spacesstring border="*"string blanks="*" for(int q=0;q!=length;+q)blanks=blanks+" "border=border+"*"blanks=b

21、lanks+"*" border=border+"*"/the number of rows and columns to writeconst int rows=width;const string:size_type cols=length+2;/write a blank line to separate the output from the inputcout<<endl;/write rows rows of output/invariant:we have written r rows so farfor(int r=0;r!=

22、rows;+r)string:size_type c=0;/invariant:we have written c characters so far in the current rowwhile(c!=cols)/are we on the border?if(r=0|r=rows-1)cout<<border; c=cols;elsecout<<blanks;c=cols; cout<<endl;return 0; int Max(int a, int b) return a>b?a:b; int wang()/ask for the chara

23、cter's length,width and heigthcout<<"Please enter the length of up,mid(should be the least) and down: "int up,mid,down;cin>>up;cin>>mid;cin>>down; /ask for the pad between two lines cout<<"Please enter a number as the pad between two lines: " int

24、 pad; cin>>pad;/the number of rows and columns to write const int length=Max(up,down);const int rows=pad*2+3;const string:size_type cols=length-1;/write a blank line to separate the output from the inputcout<<endl;int max=Max(up,down);/write rows rows of output/invariant:we have written

25、r rows so farfor(int r=0;r!=rows;+r)int c=0;/invariant:we have written c characters so far in the current rowwhile(c!=cols)if(c=max/2)cout<<"*"else if(r=0)if(c>=(max-up)/2&&c<=up+(max-up)/2)cout<<"*"elsecout<<" "else if(r=pad+1)if(c>=(

26、max-mid)/2&&c<=mid+(max-mid)/2)cout<<"*"elsecout<<" "else if(r=rows-1)if(c>=(max-down)/2&&c<=down+(max-down)/2)cout<<"*"elsecout<<" "elsecout<<" "c+;cout<<endl;return 0;int tian()/ask for t

27、he information of the character “田”.cout<<"Please enter the length: "int length;cin>>length;/the number of space lines between two * linescout<<"Please enter the pad: " int pad; cin>>pad;/the number of rows and columns to writeconst int rows=pad*2+3;const

28、string:size_type cols=length+2;/write a blank line to separate the output from the inputcout<<endl; /creat the mainlinestring line ="*"for(int i=0;i!=length-1;+i)line=line+"*"line=line+"*"/write rows rows of output/invariant:we have written r rows so farfor(int r=

29、0;r!=rows;+r)string:size_type c=0;/invariant:we have written c characters so far in the current rowwhile(c!=cols)/is it time to write the mainline?if(r=0|r=rows-1|r=pad+1)cout<<line;c=cols;elseif(c=0|c=cols-1|c=(length/2)cout<<"*"elsecout<<" " +c;cout<<end

30、l; return 0;int main()cout<<"*"<<endl;cout<<"* enjoy yourself *"<<endl;cout<<"* case0:draw a rectangle *"<<endl;cout<<"* case1:draw a “王” *"<<endl;cout<<"* case2:draw a “田” *"<<endl;cout<

31、<"* case3:exit *"<<endl;cout<<"* please select 0-2 *"<<endl;cout<<"*"<<endl;cout<<"Do you want a try? YES:1 or NO:0"cout<<endl;int c1; cin>>c1;while(c1)cout<<endl<<endl;cout<<""<

32、<endl; cout<<"Please enter the number you want to select: " int c2; cin>>c2; switch(c2) case 0:rectangle();break; case 1:wang();break; case 2:tian();break; case 3:c1=0;break; cout<<"*"<<endl;cout<<"*You'll leave.*"<<endl;cout&l

33、t;<"*BUT you really.*"<<endl;cout<<"*Did a good job!*"<<endl;cout<<"*"<<endl;cout<<endl<<endl;return 0;(四)字符串替換源程序:#include<iostream>#include<string>#include<vector>#include<cctype>/say what standard_library names we useusing std:cin; using std:endl;using std:cout; using std:string;using std:vector;vector<string> changestring(string& s)vector<string> ret;typedef string:size_type string_size;string_size i=0;/invariant:we have processed chara

溫馨提示

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

最新文檔

評論

0/150

提交評論