实验09 群体类与群体数据的组织资料.docx
- 文档编号:27046405
- 上传时间:2023-06-26
- 格式:DOCX
- 页数:28
- 大小:77.40KB
实验09 群体类与群体数据的组织资料.docx
《实验09 群体类与群体数据的组织资料.docx》由会员分享,可在线阅读,更多相关《实验09 群体类与群体数据的组织资料.docx(28页珍藏版)》请在冰豆网上搜索。
实验09群体类与群体数据的组织资料
实验09群体类与群体数据的组织(4学时)
(第9章群体类与群体数据的组织)
一、实验目的
(1)掌握函数模板与类模板。
(2)了解线性群体与群体数据的组织。
二、实验任务
9_1求绝对值的函数模板及其应用
#include
usingnamespacestd;
template
Tfun(Tx){
returnx<0?
-x:
x;
}
intmain(){
intn=-5;
doubled=-5.5;
cout< cout< return0; } 9_2函数模板的示例。 #include usingnamespacestd; template voidoutputArray(constT*array,intcount){ for(inti=0;i cout< cout< } intmain(){//主函数 constintA_COUNT=8,B_COUNT=8,C_COUNT=20; inta[A_COUNT]={1,2,3,4,5,6,7,8};//定义int数组 doubleb[B_COUNT]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};//定义double数组 charc[C_COUNT]="Welcometoseeyou! ";//定义char数组 cout<<"aarraycontains: "< outputArray(a,A_COUNT);//调用函数模板 cout<<"barraycontains: "< outputArray(b,B_COUNT);//调用函数模板 cout<<"carraycontains: "< outputArray(c,C_COUNT);//调用函数模板 return0; } 9_3类模板应用举例。 #include #include usingnamespacestd; //结构体Student structStudent{ intid;//学号 floatgpa;//平均分 }; template classStore{//类模板: 实现对任意类型数据进行存取 private: Titem;//item用于存放任意类型的数据 boolhaveValue;//haveValue标记item是否已被存入内容 public: Store();//缺省形式(无形参)的构造函数 T&getElem();//提取数据函数 voidputElem(constT&x);//存入数据函数 }; //以下实现各成员函数。 template Store : Store(): haveValue(false){} template T&Store : getElem(){ //如试图提取未初始化的数据,则终止程序 if(! haveValue){ cout<<"Noitempresent! "< exit (1);//使程序完全退出,返回到操作系统。 } returnitem;//返回item中存放的数据 } template voidStore : putElem(constT&x){ //将haveValue置为true,表示item中已存入数值haveValue=true; item=x;//将x值存入item } intmain(){ Store s1.putElem(3); s2.putElem(-7); cout< Studentg={1000,23}; Store s3.putElem(g); cout<<"Thestudentidis"< Store cout<<"RetrievingobjectD..."; cout< //由于d未经初始化,在执行函数D.getElement()过程中导致程序终止 return0; } 9_4动态数据类模板示例 //Array.h #ifndefARRAY_H #defineARRAY_H #include //数组类模板定义 template classArray{ private: T*list;//T类型指针,用于存放动态分配的数组内存首地址 intsize;//数组大小(元素个数) public: Array(intsz=50);//构造函数 Array(constArray ~Array();//析构函数 Array T&operator[](inti);//重载"[]",使Array对象可以起到C++普通数组的作用 constT&operator[](inti)const;//"[]"运算符的const版本 operatorT*();//重载到T*类型的转换,使Array对象可以起到C++普通数组的作用 operatorconstT*()const;//到T*类型转换操作符的const版本 intgetSize()const;//取数组的大小 voidresize(intsz);//修改数组的大小 }; //构造函数 template Array : Array(intsz){ assert(sz>=0);//sz为数组大小(元素个数),应当非负 size=sz;//将元素个数赋值给变量size list=newT[size];//动态分配size个T类型的元素空间 } //析构函数 template Array : ~Array(){ delete[]list; } //拷贝构造函数 template Array : Array(constArray //从对象x取得数组大小,并赋值给当前对象的成员 size=a.size; //为对象申请内存并进行出错检查 list=newT[size];//动态分配n个T类型的元素空间 //从对象X复制数组元素到本对象 for(inti=0;i list[i]=a.list[i]; } //重载"="运算符,将对象rhs赋值给本对象。 实现对象之间的整体赋值 template Array : operator=(constArray if(&rhs! =this){ //如果本对象中数组大小与rhs不同,则删除数组原有内存,然后重新分配 if(size! =rhs.size){ delete[]list;//删除数组原有内存 size=rhs.size;//设置本对象的数组大小 list=newT[size];//重新分配n个元素的内存 } //从对象X复制数组元素到本对象 for(inti=0;i list[i]=rhs.list[i]; } return*this;//返回当前对象的引用 } //重载下标运算符,实现与普通数组一样通过下标访问元素,并且具有越界检查功能 template T&Array : operator[](intn){ assert(n>=0&&n returnlist[n];//返回下标为n的数组元素 } template constT&Array : operator[](intn)const{ assert(n>=0&&n returnlist[n];//返回下标为n的数组元素 } //重载指针转换运算符,将Array类的对象名转换为T类型的指针, //指向当前对象中的私有数组。 //因而可以象使用普通数组首地址一样使用Array类的对象名 template Array : operatorT*(){ returnlist;//返回当前对象中私有数组的首地址 } template Array : operatorconstT*()const{ returnlist;//返回当前对象中私有数组的首地址 } //取当前数组的大小 template intArray : getSize()const{ returnsize; } //将数组大小修改为sz template voidArray : resize(intsz){ assert(sz>=0);//检查sz是否非负 if(sz==size)//如果指定的大小与原有大小一样,什么也不做 return; T*newList=newT[sz];//申请新的数组内存 intn=(sz sz: size;//将sz与size中较小的一个赋值给n //将原有数组中前n个元素复制到新数组中 for(inti=0;i newList[i]=list[i]; delete[]list;//删除原数组 list=newList;//使list指向新数组 size=sz;//更新size } #endif//ARRAY_H //9_4.cpp #include #include #include"Array.h" usingnamespacestd; intmain(){ Array intcount=0; intn; cout<<"Enteravalue>=2asupperlimitforprimenumbers: "; cin>>n; for(inti=2;i<=n;i++){ //检查i是否能被比它小的质数整除 boolisPrime=true; for(intj=0;j if(i%a[j]==0){//若i被a[j]整除,说明i不是质数 isPrime=false; break; } //把i写入质数表中 if(isPrime){ //如果质数表满了,将其空间加倍 if(count==a.getSize()) a.resize(count*2); a[count++]=i; } } for(inti=0;i cout< cout< return0; } 9_5链表类应用案例 //Node.h #ifndefNODE_H #defineNODE_H //类模板的定义 template classNode{ private: Node public: Tdata;//数据域 Node(constT&data,Node voidinsertAfter(Node Node Node constNode }; //类的实现部分 //构造函数,初始化数据和指针成员 template Node : Node(constT&data,Node data(data),next(next){} //返回后继结点的指针 template Node : nextNode(){ returnnext; } //返回后继结点的指针 template constNode : nextNode()const{ returnnext; } //在当前结点之后插入一个结点p template voidNode : insertAfter(Node p->next=next;//p结点指针域指向当前结点的后继结点 next=p;//当前结点的指针域指向p } //删除当前结点的后继结点,并返回其地址 template Node : deleteAfter(){ Node if(next==0)//如果当前结点没有后继结点,则返回空指针 return0; next=tempPtr->next;//使当前结点的指针域指向tempPtr的后继结点 returntempPtr;//返回被删除的结点的地址 } #endif//NODE_H //LinkedList.h #ifndefLINKEDLIST_H #defineLINKEDLIST_H #include"Node.h" template classLinkedList{ private: //数据成员: Node Node intsize;//表中的元素个数 intposition;//当前元素在表中的位置序号。 由函数reset使用 //函数成员: //生成新结点,数据域为item,指针域为ptrNext Node //释放结点 voidfreeNode(Node //将链表L拷贝到当前表(假设当前表为空)。 //被拷贝构造函数、operator=调用 voidcopy(constLinkedList public: LinkedList();//构造函数 LinkedList(constLinkedList ~LinkedList();//析构函数 LinkedList intgetSize()const;//返回链表中元素个数 boolisEmpty()const;//链表是否为空 voidreset(intpos=0);//初始化游标的位置 voidnext();//使游标移动到下一个结点 boolendOfList()const;//游标是否到了链尾 intcurrentPosition(void)const;//返回游标当前的位置 voidinsertFront(constT&item);//在表头插入结点 voidinsertRear(constT&item);//在表尾添加结点 voidinsertAt(constT&item);//在当前结点之前插入结点 voidinsertAfter(constT&item);//在当前结点之后插入结点 TdeleteFront();//删除头结点 voiddeleteCurrent();//删除当前结点 T&data();//返回对当前结点成员数据的引用 constT&data()const;//返回对当前结点成员数据的常引用 //清空链表: 释放所有结点的内存空间。 被析构函数、operator=调用 voidclear(); }; #endif//LINKEDLIST_H //9_7.cpp #include #include"LinkedList.h" usingnamespacestd; intmain(){ LinkedList //输入10个整数依次向表头插入 for(inti=0;i<10;i++){ intitem; cin>>item; list.insertFront(item); } //输出链表 cout<<"List: "; list.reset(); //输出各结点数据,直到链表尾 while(! list.endOfList()){ cout< list.next();//使游标指向下一个结点 } cout< //输入需要删除的整数 intkey; cout<<"Pleaseentersomeintegerneededtobedeleted: "; cin>>key; //查找并删除结点 list.reset(); while(! list.endOfList()){ if(list.data()==key) list.deleteCurrent(); list.next(); } //输出链表 cout<<"List: "; list.reset(); //输出各结点数据,直到链表尾 while(! list.endOfList()){ cout< list.next();//使游标指向下一个结点 } cout< return0; } 9_6栈的应用(一个简单的整数计算器) //Stack.h #ifndefSTACK_H #defineSTACK_H #include //模板的定义,SIZE为栈的大小 template classStack{ private: Tlist[SIZE];//数组,用于存放栈的元素 inttop;//栈顶位置(数组下标) public: Stack();//构造函数,初始化栈 voidpush(constT&item);//将元素item压入栈 Tpop();//将栈顶元素弹出栈 voidclear();//将栈清空 constT&peek()const;//访问栈顶元素 boolisEmpty()const;//测试是否栈满 boolisFull()const;//测试是否栈空 }; //模板的实现 template Stack : Stack(): top(-1){}//构造函数,栈顶初始化为-1 template voidStack : push(constT&item){//将元素item压入栈 assert(! isFull());//如果栈满了,则报错 list[++top]=item;//将新元素压入栈顶 } template TStack : pop(){//将栈顶元素弹出栈 assert(! isEmpty());//如果栈为空,则报错 returnlist[top--];//返回栈顶元素,并将其弹出栈顶 } template constT&Stack : peek()const{//访问栈顶元素 assert(! isEmpty());//如果栈为空,则报错 returnlist[top];//返回栈顶元素 } template boolStack : is
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验09 群体类与群体数据的组织资料 实验 09 群体 数据 组织 资料