还剩4页未读,继续阅读
文本内容:
实验XX例-实验名称
一、实验目的
1.了解线性表List的逻辑结构特性,以及这种特性在计算机内的两种存储结构:数组和链表
2.重点是线性表的基本操作在两种存储结构上的实现;其中以链表的操作为侧重点;并进一步学习面向对象的程序设计方法
3.进一步掌握面向对象的程序设计方法,提高程序设计能力
二、实验内容代码//TheLifcOfGame.cpp:定义控制台应用程序的入口点〃#includestdafx.hincludelife.h#includeiostreamusingnamespace std;voidinstructions/*Pre:None.Post:Instructions forusingtheLife programhavebeen printed.*/cout«Welcome toConways gameof Life,«endl;cout«This gameuses agrid ofsize«maxrow«by«maxcol«in which«endl;cout«each cellcan eitherbeoccupied byanorganism ornot.«endl;cout«The occupiedcells changefrom generationtogeneration«endl;cout«according tothenumberof neighboringcells whichare alive.«endl;booluser_says_yesintc;boolinitial_response=true;do{//Loop untilanappropriateinput isreceived.ifinitial_responsecout«y,n«flush;elsecout«Respond witheitheryorn:«flush;do{//Ignore whitespace.c=cin.get;}whilec==7n||c==||c==7t;initial_response=false;}whilec!=yc!=Y c!=nc!=N;return c==y*||c==Y;int main//Program toplay Conwaysgame ofLife./*Pre:Theuser suppliesan initialconfigurationof livingcells.Post:The programprints asequenceofpictures showingthechanges intheconfigurationoflivingcells accordingto therulesforhe gameofLife.Uses:The classLifeand itsmethodsinitialize,print,and update;the functionsinstructions,user_says_yes.*/{Lifeconfiguration;instructions;configuration.initializc;configuration.print;cout«Continueviewing newgenerations”«endl;whileuser_says_yes{configuration.update;configuration.print;cout«Continue viewingnewgenerations«endl;I/*水*木木*******木木*****木*木**木***下由[•层IJfphy昭而回木***木***木*******木******/#ifndef_LIFE_H#endif_LIFE_Hconst intmax row=20,maxcol=60;//grid dimensionsclassLifepublic:voidinilialize;voidprint;voidupdate;private:intgrid[max row+2][maxcol+2];//Allowtwo extrarowsand columns.intneighbor_countint row,int col;};#include stdafx.h#includeiostream#includelife.husingnamespace std;voidLife::prinl/*Pre:TheLife object contains aconfiguration.Post:Theconfiguration iswritten fortheuser.*/introw,col;cout«/nThe currentLife configurationis:«endl;forrow=1;row=maxrow;row++{forcol=1;col=maxcol;col++ifgrid[row][col]==I cout«elsecout«cout«cndl;cout«endl;inLife::neighbor_countint row,int col/*Pre:TheLife object contains aconfiguration,and thecoordinatesrowand coldefine acellinsideits hedge.Post:The numberoflivingneighbors ofthespecified cellisreturned.*/{inti,j;intcount=0;fori=row-1;i=row+1;i++forj=col-1;j=col+1;j++count+=gridfiHj];//Increasethe countifneighboris alive.count-=grid[row][col];//Acell isnot itsownneighbor.return count;void Lifc::updatcPre:TheLife objectcontains aconfiguration.Post:The Lifeobjectcontainsthenextgeneration ofconfiguration.introw,col;intnew_griJ[maxrow+2]|maxcol+2];forrow=1;row=maxrow;row++for col=1;col=maxcol;col++switch neighbor_countrow,colnew_grid[row][col]=grid[row][col];//Statusstays thesame,break;ncw_grid[row][col]=1;//Cellis nowalive.break;default:ncw_grid[rowj[col]=0;//Cell isnowdead.for row=1;row=maxrow;row++forcol=I;col=maxcol;col++grid[row][col]=new_grid[row][coll;void Life::initialize/*Pre:None.Post:The Lifeobjectcontainsaconfiguration specifiedbytheuser.*/introw.col;for row=0;row=maxrow+1;row++forcol=0;col=inaxcol+1;col++grid[rowl[col]=0;cout«List thecoordinatesforliving cells.«end;cout«Terminatethelistwith thethespecial pair-1-1«endl;cin»row»col;while row!=-1||col!=-1{ifrow=1row=maxrowifcol=1col=maxcolgrid[row][coll=I;elsecout«Column«col«is outofrange.«endl;elsecout«Row«row«isout ofrange.«cndl;cin»row»col;I
三、实验结果
四、总结(软件设计与实现过程种的经验与体会,进一步的改进设想,可再加入哪些部分?哪些部分可删除?哪些部分可合并?哪些可降低复杂度?怎样设计能提高其可复用性?多思考,多总结,才能不断提高程序设计能力)。
个人认证
优秀文档
获得点赞 0