數據結構與程序設計 C++ 基礎知識Review_第1頁
數據結構與程序設計 C++ 基礎知識Review_第2頁
數據結構與程序設計 C++ 基礎知識Review_第3頁
數據結構與程序設計 C++ 基礎知識Review_第4頁
數據結構與程序設計 C++ 基礎知識Review_第5頁
已閱讀5頁,還剩30頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、11/23/2021數據結構與程序設計1The Solution: nClasses nObjects nMethods nEg: nLife configuration;nconfiguration.initialize( );nconfiguration.update();nconfiguration.print();11/23/2021數據結構與程序設計2Life Game ImplementnSet up a Life configuration as an initial arrangement of living and dead cells.nPrint the Life con

2、figuration.nWhile the user wants to see further generations:nUpdate the configuration by applying the rules of the Life game.nPrint the current configuration.11/23/2021數據結構與程序設計3良好的編程風格良好的編程風格n類、變量及函數的命名要簡短有意義類、變量及函數的命名要簡短有意義nP12-13n在程序、函數或方法前寫上精確的功能描述。在程序、函數或方法前寫上精確的功能描述。n文檔、注釋要在編程的同時,甚至是在編程之前完成。文檔

3、、注釋要在編程的同時,甚至是在編程之前完成。nGuidelines P13-14n工程的模塊化工程的模塊化n至頂向下,模塊細分至頂向下,模塊細分n使用類來建模(使用類來建模(STL,MFC)n每個函數只做一件事情,代碼盡量不要超出一頁每個函數只做一件事情,代碼盡量不要超出一頁n精確每個函數的功能,精確每個函數的功能,Input(Const 說明)說明), Output, Inout參參數數n函數接口要簡單,盡量少用全局變量,用時要有文檔注明函數接口要簡單,盡量少用全局變量,用時要有文檔注明11/23/2021數據結構與程序設計4課堂練習課堂練習nP17 Exercises 1.3 E1(a)n

4、P17 Exercises 1.3 E411/23/2021數據結構與程序設計5C+ 基礎知識基礎知識Review11/23/2021數據結構與程序設計6C+ 基礎知識基礎知識Reviewn.h fileclass declaration and function headersn.cpp or .c fileclass or function implementation11/23/2021數據結構與程序設計7C+ 基礎知識基礎知識Reviewn#includeUsed to include header files#include for library files#include “fi

5、le.h” for local files and library files11/23/2021數據結構與程序設計8C+ 基礎知識基礎知識ReviewnI/OTo use Input or Output, you need to use:#include Standard input is cin and uses cin xCoord;Standard output is cout and uses cout “(“ xCoord “,” yCoord “)” n;int* x = new intn; /動態數組動態數組char s6;char s = “CS225”; /with a n

6、ull character at the end 011/23/2021數據結構與程序設計10C+ 基礎知識基礎知識ReviewnParameter Passing3 ways to pass a variable to a function:Pass-by-value (C and C+)Pass-by-pointer (C and C+)Pass-by-reference (C+ only)11/23/2021數據結構與程序設計11Parameter Passing11/23/2021數據結構與程序設計12Value Example Result11/23/2021數據結構與程序設計13P

7、arameter Passing11/23/2021數據結構與程序設計14Pointer Example Result11/23/2021數據結構與程序設計15Parameter Passing11/23/2021數據結構與程序設計16Reference Example Result11/23/2021數據結構與程序設計17Life Coding: The Life Class Declaration P22const int maxrow = 20, maxcol = 60; / grid dimensionsclass Life public: void initialize(); voi

8、d print(); void update();private: int gridmaxrow + 2maxcol + 2; / allows for two extra rows and columns int neighbor_count(int row, int col);11/23/2021數據結構與程序設計18Life Coding: The Main Program P8void main() / Program to play Conways game of Life. Life configuration; instructions(); configuration.init

9、ialize(); configuration.print(); cout Continue viewing new generations? endl; while (user_says_yes() configuration.update(); configuration.print(); cout Continue viewing new generations? endl; 11/23/2021數據結構與程序設計19分析分析: Life Methodsint Life : neighbor count(int row, int col)Pre: The Life object cont

10、ains a configuration, and the coordinatesrow and col define a cell inside its hedge.Post: The number of living neighbors of the specied cell isreturned.void Life : update( )Pre: The Life object contains a configuration.Post: The Life object contains the next generation of configuration.11/23/2021數據結

11、構與程序設計20分析分析: Life Methodsvoid instructions( )Pre: None.Post: Instructions for using the Life program are printed.void Life : initialize( )Pre: None.Post: The Life object contains a configuration specified by the user.void Life : print( )Pre: The Life object contains a configuration.Post: The config

12、uration is written for the user.11/23/2021數據結構與程序設計21Life Coding: The Life:initialize Method P23void Life:initialize() int row, col; for (row = 0; row = maxrow+1; row+) for (col = 0; col = maxcol+1; col+) gridrowcol = 0; cout List the coordinates for living cells. endl; cout Terminate the list with

13、the special pair -1 -1 row col; while (row != -1 | col != -1) if (row = 1 & row = 1 & col = maxcol) gridrowcol = 1; else cout Column col is out of range. endl; else cout Row row is out of range. row col; 11/23/2021數據結構與程序設計22Life Coding: The Life:neighbor_count Method P23int Life:neighbor_co

14、unt(int row, int col) int i, j; int count = 0; for (i = row - 1; i = row + 1; i+) for (j = col - 1; j = col + 1; j+) count += gridij; / Increase the count if neighbor is alive. count -= gridrowcol; / Reduce count, since cell is not its own neighbor. return count;11/23/2021數據結構與程序設計23Life Coding: The

15、 Life:update Method P24void Life:update() int row, col; int new_gridmaxrow + 2maxcol + 2; /Prevent next generation from affecting this generation for (row = 1; row = maxrow; row+) for (col = 1; col = maxcol; col+) switch (neighbor_count(row, col) case 2: new_gridrowcol = gridrowcol; / Status stays t

16、he same. break; case 3: new_gridrowcol = 1; / Cell is now alive. break; default: new_gridrowcol = 0; / Cell is now dead. for (row = 1; row = maxrow; row+) for (col = 1; col = maxcol; col+) gridrowcol = new_gridrowcol;11/23/2021數據結構與程序設計24Life Coding: The Life:print Method P26void Life:print() int ro

17、w, col; cout nThe current Life configuration is: endl; for (row = 1; row = maxrow; row+) for (col = 1; col = maxcol; col+) if (gridrowcol = 1) cout *; else cout ; cout endl; cout endl;11/23/2021數據結構與程序設計25Life Coding: The utility function instructions P25nvoid instructions()nn cout Welcome to Conway

18、s game of Life. endl;n cout This game uses a grid of size n maxrow by maxcol in which endl;n cout each cell can either be occupied by an organism or not. endl;n cout The occupied cells change from generation to generation endl;n cout according to the number of neighboring cells which are alive. endl

19、;n11/23/2021數據結構與程序設計26Life Coding: The utility function user_says_yes P27nbool user_says_yes()nn int c;n bool initial_response = true;n do / Loop until an appropriate input is received.n if (initial_response)n cout “ (y,n)? ” flush; /清除緩沖區清除緩沖區n elsen cout Respond with either y or n: c; /會自動過濾會自動過濾

20、n, t, 等效于等效于char c;do / Ignore white space. c = cin.get(); while (c = n | c = | c = t);11/23/2021數據結構與程序設計28Life Coding: The utility function user_says_yes P27bool user_says_yes() /改進代碼改進代碼 char c; cout “ (y,n)? ” c; while (c != y & c != Y & c != n & c != N) / Loop until an appropriate i

21、nput is received. cout Respond with either y or n: c; return (c = y | c = Y);11/23/2021數據結構與程序設計29VC Life_Game 實現實現n目錄目錄Life_Game下例程下例程n參考參考P23 圖圖11/23/2021數據結構與程序設計30上機作業上機作業n用用VC實現上述實現上述Life_Game 11/23/2021數據結構與程序設計31程序調試的一些方法程序調試的一些方法n可能的程序分支都思考一遍可能的程序分支都思考一遍n在主程序的關鍵點添加重要變量輸出語句或設置斷點在主程序的關鍵點添加重要變量

22、輸出語句或設置斷點n在函數的調用前后添加重要變量輸出語句或設置斷點在函數的調用前后添加重要變量輸出語句或設置斷點11/23/2021數據結構與程序設計32軟件測試軟件測試n測試只能證明測試只能證明BUG的存在,永遠不能證明程序的存在,永遠不能證明程序的正確性。的正確性。n測試的主要方法測試的主要方法n黑盒法黑盒法 P30n(a) Easy valuesn(b) Typical, realistic valuesn(c) Extreme valuesn(d) Illegal valuesn白盒法白盒法 P31:Trace all the paths through thenprogram.11/

23、23/2021數據結構與程序設計33本章小結(本章小結(points and pitfalls)1. To improve your program, review the logic. Dont optimizecode based on a poor algorithm.2. Never optimize a program until it is correct and working.3. Dont optimize code unless it is absolutely necessary.4. Keep your functions short; rarely should an

24、y function be morethan a page long.5. Be sure your algorithm is correct before starting to code.6. Verify the intricate(復雜的)(復雜的) parts of your algorithm.7. Keep your logic simple.8. Be sure you understand your problem before you decide howto solve it.9. Be sure you understand the algorithmic method

25、 before youstart to program.10. In case of difficulty, divide a problem into pieces and think ofeach part separately.11/23/2021數據結構與程序設計34本章小結(本章小結(points and pitfalls)11. The nouns that arise in describing a problem suggest usefulclasses for its solution; the verbs suggest useful functions.12. Include careful documentation (as presented in Section 1.3.2)with each function as you write it.13. Be careful to write down precise preconditions and postconditionsfor every function.14. In

溫馨提示

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

評論

0/150

提交評論