您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业文档 > 南昌大学数据结构实验报告(C++)
1实验报告实验课程:数据结构学生姓名:学号:专业班级:网络工程班2014年6月30日2目录一、实验一长方形-------------------------3二、实验二循序表------------------13三、实验三链表--------------------29四、实验四循环双链表---------------513南昌大学实验报告---(1)长方形数据结构C++类学生姓名:学号:专业班级:实验类型:□验证■综合□设计□创新实验日期:2014-6-25实验成绩:【实验题目】长方形数据结构C++类【实验步骤】1.需求分析为实现长方形的输入输出和其他操作,我们进行如下需求分析:长方形相关数据的输出重载赋值运算符的定义输出长方形的面积测试数据2.概要设计为了实现上述功能,需要设定以下模块:主程序模块↓测试长方形模块↓派生长方形模块↓抽象长方形模块设置长设置宽.......各模块之间调用关系如图3.详细设计①长方形数据结构基类templatetypenameElemType4classRectangle{public:classRectangleNo{public:intno;};Rectangleoperator=(RectanglerightR);voidsetLength(ElemTypel);voidsetWidth(ElemTypez);voidsetNo(inti);ElemTypeArea(ElemType);Rectangle();Rectangle(constRectangle&otherD);virtual~Rectangle();protected:ElemTypelength;ElemTypewidth;RectangleNomyNo;};templatetypenameElemTypeRectangleElemTypeRectangleElemType::operator=(RectangleElemTyperightR){if(this!=&rightR){length=rightR.length;width=rightR.width;myNo=rightR.myNo;cout赋值后,当前的长方形为(length,width)endl;}return*this;}templatetypenameElemTypevoidRectangleElemType::setLength(ElemTypel){5length=1;}templatetypenameElemTypevoidRectangleElemType::setWidth(ElemTypez){width=z;}templatetypenameElemTypeElemTypeRectangleElemType::Area(ElemType){returnlength*width;}templatetypenameElemTypevoidRectangleElemType::setNo(inti){myNo.no=i;}templatetypenameElemTypeRectangleElemType::Rectangle(){length=width=0;cout自动调用构造函数endl;}templatetypenameElemTypeRectangleElemType::Rectangle(constRectangleElemType&otherD){length=otherD.length;width=otherD.width;myNo=otherD.myNo;cout自动调用拷贝初始化构造函数初始化为(;coutlength,width)endl;}templatetypenameElemTypeRectangleElemType::~Rectangle(){cout\n第myNo.no长方形对象(length,width)生存期结束!endl;}②循环单链表派生类templatetypenameElemTypeclassMyRectangle:publicRectangleElemType6{public:voidread(istream&in);voiddiaplay(ostream&out)const;};templatetypenameElemTypevoidMyRectangleElemType::read(istream&in){cout请输入第myNo.no个长方形对象endl;cout长方形中的长;inlength;cout长方形中的宽;inwidth;}templatetypenameElemTypeistream&operator(istream&in,MyRectangleElemType&iD){iD.read(in);returnin;}templatetypenameElemTypevoidMyRectangleElemType::diaplay(ostream&out)const{out第myNo.no个长方形对象长=length\t宽=width\t面积=length*widthendl;}templatetypenameElemTypeostream&operator(ostream&out,constMyRectangleElemType&oD){oD.diaplay(out);returnout;}③主程序模块voidmain(){MyRectanglefloatrec;intchoose;charcontinueYesNo='N';while(1){7choose=0;system(cls);coutendl;cout*****************测试长方形的操作******************endlendl;cout\t1.设置长方形的序号endl;cout\t2.设置长方形的长endl;cout\t3.设置长方形的宽endl;cout\t4.重载赋值运算符的定义endl;cout\t5.求长方形的面积endl;cout其他,结束endlendl;coutby网工121吴垂优endl;cout/////////////////////////////////////////////////////////////endl;displayCurrentObject(rec);cout/////////////////////////////////////////////////////////////endl;cout请选择你要操作的代码(-)号码:;cinchoose;if(choose0&&choose6){system(cls);displayCurrentObject(rec);}switch(choose){case1:ex2_1_1(rec,continueYesNo);break;case2:ex2_1_2(rec,continueYesNo);break;case3:ex2_1_3(rec,continueYesNo);break;case4:ex2_1_4(rec,continueYesNo);break;case5:ex_2_1_5(rec,continueYesNo);break;default:cout\n你选择了结束。endlendl;return;}if(continueYesNo=='N'||continueYesNo=='N')break;}}84.编码实现文件名包含的类或模块基类头文件长方形基类派生类头文件长方形派生类测试头文件测试模块主程序文件main.cpp主函数模块5.上机调试6.使用说明执行程序,如图所示输入数字选择执行以下不同的操作。每执行一次操作,就会显示相应的执行结果7.测试结果①设置长方形的序号9②设置长方形的长10③设置长方形的宽11⑤重载赋值运算符的定义⑥求长方形的面积1213南昌大学实验报告---(2)顺序表学生姓名:学号:专业班级:实验类型:□验证■综合□设计□创新实验日期:2014-6-25实验成绩:【实验题目】顺序表数据结构C++类【实验步骤】1.需求分析为实现顺序表的输入输出和其他操作,我们进行如下需求分析:实验中我们要完成如下操作:在第i个元素之前插入一个元素判断顺序表是否为空求顺序表中元素个数取第i个元素查找第1个与元素满足compare()关系元素的序号返回某元素的前驱返回某元素的后继删除第i个元素把一个顺序表赋值给另一个顺序表把顺序表置空随机生成顺序表输入顺序表2.概要设计为了实现上述功能,需要设定一下模块:主程序模块↓测试顺序表模块↓派生顺序表模块↓抽象顺序表模块14deleteElem()GetElem()....各模块之间调用关系如图3.详细设计①顺序表数据结构基类#defineMYHEAD_H#includemyhead.h#endif#defineLIST_MAX_SIZE100#defineLISTINCREMENT10//顺序表使用的一些常量说明////////////////////////////////////////////////////////////////////////////////////////顺序表数据结构C艹声明(基类)templatetypenameElemTypeclassSqList{public:intbin_Search(ElemTypekey);//有序顺序表折半查找voidclear();//把顺序表置空StatusdeleteElem(inti,ElemType&e);//删除第i个元素StatusgetElem(inti,ElemType&e);//取第i个元素intgetLength();//求顺序表中元素的个数intgetListSize();//取顺序表空间的大小Statusinsert(inti,ElemTypee);//在第i个元素之前插入一个元素boolisEmpty();//判断顺序表是否置空15intlocateElem(ElemTypee,Status(*compare)(ElemType,ElemType));//StatusnextElem(ElemTypee,ElemType&next_e);SqListElemTypeoperator=(SqListElemTyperightL);StatuspriorElem(ElemTypee,ElemType&prior_e);intsequentialSearch(ElemTypee);//******************************系统自动调用构造函数及析构函数声明************************************//SqList();virtual~SqList();SqList(constSqListElemType&otherL);protected:ElemType*elem;intlistSize;intn;};/////////////////////////////////////////////////////////////////////////////////////顺序表数据结构C++类的实现////////////////////////////////////////////////////////////////////////////////////折半查
三七文档所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
本文标题:南昌大学数据结构实验报告(C++)
链接地址:https://www.777doc.com/doc-5521734 .html