实验09 群体类与群体数据的组织资料.docx
《实验09 群体类与群体数据的组织资料.docx》由会员分享,可在线阅读,更多相关《实验09 群体类与群体数据的组织资料.docx(32页珍藏版)》请在冰豆网上搜索。
实验09群体类与群体数据的组织资料
谭毓银《程序设计》课内实验C++2014
数学与应用数学曾悦03
4
166
学时)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;
}
1
9_2函数模板的示例。
#include
usingnamespacestd;
//定义函数模板template
voidoutputArray(constT*array,intcount){
for(inti=0;icout<cout<}
intmain(){//主函数constintA_COUNT=8,B_COUNT=8,C_COUNT=20;
int数组inta[A_COUNT]={1,2,3,4,5,6,7,8};//定义数组doubleb[B_COUNT]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};//定义doublechar//定义数组charc[C_COUNT]=Welcometoseeyou!
;
cout<<调用函数模板//outputArray(a,A_COUNT);
cout<<outputArray(b,B_COUNT);//调用函数模板cout<<//调用函数模板outputArray(c,C_COUNT);
return0;
}
2
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<<exit
(1);//使程序完全退出,返回到操作系统。
}
itemreturnitem;//返回中存放的数据}
template//存入数据函数的实现voidStore:
:
putElem(constT&x){
中已存入数值,表示置为将haveValue=true;//haveValuetrueitemitem
将x值存入//item=x;
}
intmain(){
Stores1,s2;
s1.putElem(3);
s2.putElem(-7);
cout<3Studentg={1000,23};
Stores3;s3.putElem(g);
cout<Stored;
cout<cout<过程中导致程序终在执行函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&a);
//拷贝构造函数
~Array();
//析构函数使数组对象可以整体赋重载Array&operator=(constArray&rhs);//?
值C++Array孜屝//T&operator[](inti);重载,使对象可以起到普通数组的作用版本运算符的?
?
孜屝constconstT&operator[](inti)const;
普通数组类型的转换,使T*重载到operatorT*();
//C++Array对象可以起到的作用4
版本类型转换操作符的constoperatorconstT*()const;//到T*取数组的大小intgetSize()const;//修改数组的大小voidresize(intsz);//};
构造函数//template
Array:
:
Array(intsz){
,应当非负//sz为数组大小(元素个数)assert(sz>=0);
size
将元素个数赋值给变量size=sz;//
类型的元素空间个Tlist=newT[size];//动态分配size}
析构函数//template
Array:
:
~Array(){
delete[]list;
}
拷贝构造函数//template
Array:
:
Array(constArray&a){
x取得数组大小,并赋值给当前对象的成员//从对象size=a.size;
//为对象申请内存并进行出错检查nlist=newT[size];//动态分配个T类型的元素空间复制数组元素到本对象从对象X//for(inti=0;ilist[i]=a.list[i];
}
rhs赋值给本对象。
实现对象之间的整体赋值//重载?
运算符,将对象template
Array&Array:
:
operator=(constArray&rhs){
if(&rhs!
=this){
不同,则删除数组原有内存,然后重新分配如果本对象中数组大小与rhs//
if(size!
=rhs.size){
//删除数组原有内存delete[]list;
设置本对象的数组大小size=rhs.size;
//个元素的内存重新分配list=newT[size];//n}
X从对象//复制数组元素到本对象for(inti=0;i5
list[i]=rhs.list[i];
}
//返回当前对象的引用return*this;
}
//重载下标运算符,实现与普通数组一样通过下标访问元素,并且具有越界检查功能template
T&Array:
:
operator[](intn){
//检查下标是否越界assert(n>=0&&n的数组元素//返回下标为nreturnlist[n];
}
template
constT&Array:
:
operator[](intn)const{
检查下标是否越界assert(n>=0&&n//}
类型的指针,类的对象名转换为T//重载指针转换运算符,将Array//指向当前对象中的私有数组。
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];
//
6
n中较小的一个赋值给与sizeintn=(szsz:
size;//将sz个元素复制到新数组中将原有数组中前n//for(inti=0;inewList[i]=list[i];
//删除原数组delete[]list;
list指向新数组list=newList;
//使size
size=sz;//更新}
//ARRAY_H#endif
//9_4.cpp
#include
#include
#includeArray.h
usingnamespacestd;
intmain(){
个元素。
用来存放质数的数组,初始状态有10Arraya(10);//
intcount=0;
intn;
cout<=2asupperlimitforprimenumbers:
;
cin>>n;
for(inti=2;i<=n;i++){
i是否能被比它小的质数整除//检查boolisPrime=true;
for(intj=0;j不是质数整除,说明i//if(i%a[j]==0){若i被a[j]isPrime=false;
break;
}
写入质数表中i//把if(isPrime){
//如果质数表满了,将其空间加倍if(count==a.getSize())
a.resize(count*2);
a[count++]=i;
}
}
for(inti=0;icout<7
return0;
}
9_5链表类应用案//Node.h
#ifndefNODE_H
#defineNODE_H
/类模板的定template
classNode{
private:
/指向后继结点的指Node*next;
public:
/数据Tdata;
/Node(constT&data,Node*next=0);构造函voidinsertAfter(Node*p);/在本结点之后插入一个同类结p
/删除本结点的后继结点,并返回其地Node*deleteAfter();
/Node*nextNode();
获取后继结点的地
获取后继结点的地constNode*nextNode()const;/};
类的实现部分////构造函数,初始化数据和指针成员template
Node:
:
Node(constT&data,Node*next/*=0*/):
data(data),next(next){}
返回后继结点的指针//template
Node*Node:
:
nextNode(){
returnnext;
}
//返回后继结点的指针8
template
constNode*Node:
:
nextNode()const{
returnnext;
}
在当前结点之后插入一个结点p//template
voidNode:
:
insertAfter(Node*p){
//p结点指针域指向当前结点的后继结点p->next=next;
当前结点的指针域指向//next=p;p
}
删除当前结点的后继结点,并返回其地址//template
Node*Node:
:
deleteAfter(){
Node*tempPtr=next;//将欲删除的结点地址存储到tempPtr中if(next==0)//如果当前结点没有后继结点,则返回空指针return0;
的后继结点//使当前结点的指针域指向tempPtrnext=tempPtr->next;
返回被删除的结点的地址//returntempPtr;
}
#endif//NODE_H
//LinkedList.h
#ifndefLINKEDLIST_H
#defineLINKEDLIST_H
#includeNode.h
template
classLinkedList{
private:
//数据成员:
表头和表尾指针Node*front,*rear;
//由插入和删除操作记录表当前遍历位置的指针,Node*prevPtr,*currPtr;//更新//intsize;表中的元素个数//intposition;reset当前元素在表中的位置序号。
由函数使用
//函数成员:
ptrNext
//生成新结点,数据域为,指针域为itemNode*newNode(constT&item,Node*ptrNext=NULL);
释放结点//
9
voidfreeNode(Node*p);
。
L拷贝到当前表(假设当前表为空)//将链表调用被拷贝构造函数、operator=//voidcopy(constLinkedList&L);
public:
构造函数LinkedList();//
//拷贝构造函数LinkedList(constLinkedList&L);
析构函数//~LinkedList();
重载赋值运算符LinkedList&operator=(constLinkedList&L);//
//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();
};
//LINKEDLIST_H#endif
//9_7.cpp
#include
#includeLinkedList.h
usingnamespacestd;
intmain(){
LinkedListlist;
10
个整数依次向表头插入输入10//for(inti=0;i<10;i++){
intitem;
cin>>item;
list.insertFront(item);
}
//输出链表cout<;
list.reset();
//输出各结点数据,直到链表尾while(!
list.endOfList()){
;cout<//使游标指向下一个结点list.next();
}
cout<输入需要删除的整数//intkey;
cout<;
cin>>key;
查找并删除结点//
list.reset();
while(!
list.endOfList()){
if(list.data()==key)
list.deleteCurrent();
list.next();
}
输出链表//cout<;
list.reset();
输出各结点数据,直到链表尾//
while(!
list.endOfList()){
;cout<//使游标指向下一个结点list.next();
}
cout<return0;
}
11
9_6栈的应用(一个简单的整数计算器)
//Stack.h
#ifndefSTACK_H
#defineSTACK_H
#include
//模板的定义,SIZE为栈的大小
template
classStack{
private:
Tlist[SIZE];//数组,用于存放栈的元素
inttop;//栈顶位置(数组下标)
public:
构造函数,初始化栈Stack();//item压入栈voidpush(constT&item);//将元素//将栈顶元素弹出栈Tpop();
//将栈清空voidclear();
访问栈顶元素constT&peek()const;//测试是否栈满boolisEmpty()const;//测试是否栈空boolisFull()const;//};
//模板的实现template
-1构造函数,栈顶初始化为//Stack:
:
Stack():
top(-1){}
template
item//voidStack:
:
push(constT&item){将元素压入栈//assert(!
isFull());如果栈满了,则报错将新元素压入栈顶list[++top]=item;//
}
template
TStack:
:
pop(){