版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、7/18/2022数据结构与程序设计1The Solution: Classes Objects Methods Eg: Life configuration;configuration.initialize( );configuration.update();configuration.print();7/18/2022数据结构与程序设计2Life Game ImplementSet up a Life configuration as an initial arrangement of living and dead cells.Print the Life configuration.W
2、hile the user wants to see further generations:Update the configuration by applying the rules of the Life game.Print the current configuration.7/18/2022数据结构与程序设计3良好的编程风格类、变量及函数的命名要简短有意义P12-13在程序、函数或方法前写上精确的功能描述。文档、注释要在编程的同时,甚至是在编程之前完成。Guidelines P13-14工程的模块化至顶向下,模块细分使用类来建模(STL,MFC)每个函数只做一件事情,代码尽量不要超
3、出一页精确每个函数的功能,Input(Const 说明), Output, Inout参数函数接口要简单,尽量少用全局变量,用时要有文档注明7/18/2022数据结构与程序设计4课堂练习P17 Exercises 1.3 E1(a)P17 Exercises 1.3 E47/18/2022数据结构与程序设计5C+ 基础知识Review7/18/2022数据结构与程序设计6C+ 基础知识Review.h fileclass declaration and function headers.cpp or .c fileclass or function implementation7/18/202
4、2数据结构与程序设计7C+ 基础知识Review#includeUsed to include header files#include for library files#include “file.h” for local files and library files7/18/2022数据结构与程序设计8C+ 基础知识ReviewI/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
5、 “(“ xCoord “,” yCoord “)” n;int* x = new intn; /动态数组char s6;char s = “CS225”; /with a null character at the end 07/18/2022数据结构与程序设计10C+ 基础知识ReviewParameter 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)7/18/2022数据结构与程序设计1
6、1Parameter Passing7/18/2022数据结构与程序设计12Value Example Result7/18/2022数据结构与程序设计13Parameter Passing7/18/2022数据结构与程序设计14Pointer Example Result7/18/2022数据结构与程序设计15Parameter Passing7/18/2022数据结构与程序设计16Reference Example Result7/18/2022数据结构与程序设计17Life Coding: The Life Class Declaration P22const int maxrow =
7、20, maxcol = 60; / grid dimensionsclass Life public: void initialize(); void print(); void update();private: int gridmaxrow + 2maxcol + 2; / allows for two extra rows and columns int neighbor_count(int row, int col);7/18/2022数据结构与程序设计18Life Coding: The Main Program P8void main() / Program to play Co
8、nways game of Life. Life configuration; instructions(); configuration.initialize(); configuration.print(); cout Continue viewing new generations? endl; while (user_says_yes() configuration.update(); configuration.print(); cout Continue viewing new generations? endl; 7/18/2022数据结构与程序设计19分析: Life Meth
9、odsint Life : neighbor count(int row, int col)Pre: The Life object contains 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
10、Life object contains the next generation of configuration.7/18/2022数据结构与程序设计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 : pr
11、int( )Pre: The Life object contains a configuration.Post: The configuration is written for the user.7/18/2022数据结构与程序设计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 th
12、e coordinates for living cells. endl; cout Terminate the list with 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; 7/18/2022数据结构与程序设计22Life Codi
13、ng: The Life:neighbor_count Method P23int Life:neighbor_count(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 ne
14、ighbor. return count;7/18/2022数据结构与程序设计23Life Coding: The 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, c
15、ol) case 2: new_gridrowcol = gridrowcol; / Status stays the 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;7/18/2022数据结构与程序设计24Life C
16、oding: The Life:print Method P26void Life:print() int row, 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;7/18/2022数据结构与程序设计25Life Coding: The utility function instru
17、ctions P25void instructions() cout Welcome to Conways game of Life. endl; cout This game uses a grid of size maxrow by maxcol in which endl; cout each cell can either be occupied by an organism or not. endl; cout The occupied cells change from generation to generation endl; cout according to the num
18、ber of neighboring cells which are alive. endl;7/18/2022数据结构与程序设计26Life Coding: The utility function user_says_yes P27bool user_says_yes() int c; bool initial_response = true; do / Loop until an appropriate input is received. if (initial_response) cout “ (y,n)? ” flush; /清除缓冲区 else cout Respond with
19、 either y or n: c; /会自动过滤n, t, 等效于char c;do / Ignore white space. c = cin.get(); while (c = n | c = | c = t);7/18/2022数据结构与程序设计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 appropri
20、ate input is received. cout Respond with either y or n: c; return (c = y | c = Y);7/18/2022数据结构与程序设计29VC Life_Game 实现目录Life_Game下例程参考P23 图7/18/2022数据结构与程序设计30上机作业用VC实现上述Life_Game 7/18/2022数据结构与程序设计31程序调试的一些方法可能的程序分支都思考一遍在主程序的关键点添加重要变量输出语句或设置断点在函数的调用前后添加重要变量输出语句或设置断点7/18/2022数据结构与程序设计32软件测试测试只能证明BUG的
21、存在,永远不能证明程序的正确性。测试的主要方法黑盒法 P30(a) Easy values(b) Typical, realistic values(c) Extreme values(d) Illegal values白盒法 P31:Trace all the paths through theprogram.7/18/2022数据结构与程序设计33本章小结(points and pitfalls)1. To improve your program, review the logic. Dont optimizecode based on a poor algorithm.2. Never
22、 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 any function be morethan a page long.5. Be sure your algorithm is correct before starting to code.6. Verify the intricate(复杂的) parts of your algorit
23、hm.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 before youstart to program.10. In case of difficulty, divide a problem into pieces and think ofeach part separately.7/18/2022数据结构与程序设计34本章小结(points an
24、d 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. Include error checking at the beginning of
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026杭州新初一英语抢跑计划:音标巩固、词汇扩容与阅读启蒙方案
- 2026天河事业编面试题及答案
- 2026通信类职称面试题及答案
- 2026脱口秀的面试题及答案
- 2026网红商品面试题目及答案
- 2026往年医疗面试题及答案
- 2026温暖工程面试题及答案
- 2026文字设计师面试题及答案
- 2026物流普工面试题及答案
- 2026先慧电气工程师面试题及答案
- 2026河南郑州市郑盐盐业集团有限公司社会招聘7人笔试参考题库及答案详解
- 2026年辽宁锦州海通实业有限公司计划招录28人备考题库及参考答案详解一套
- 2026年黑龙江、吉林、辽宁、内蒙古高考生物试卷(含答案及解析)
- 2026江粮集团科技创新与品控检验中心校园招聘1人备考题库及参考答案详解
- 慢性肾脏病合并心脏病的管理
- 2026统编版小学三年级道德与法治下册期末复习综合测试卷及答案(共三套)
- 海绵城市建设试点项目水土保持方案
- 2026年广东省惠州市初二学业水平地理生物会考试题题库(答案+解析)
- 2026中共深圳市龙岗区委政法委员会招聘聘员4人备考题库(广东)附答案详解ab卷
- 2026年贵州铜仁市初二地生会考真题试卷+解析及答案
- 学校运动猝死案例研究报告
评论
0/150
提交评论