数据结构c++课设报告文档格式.docx
- 文档编号:21104371
- 上传时间:2023-01-27
- 格式:DOCX
- 页数:42
- 大小:93.06KB
数据结构c++课设报告文档格式.docx
《数据结构c++课设报告文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构c++课设报告文档格式.docx(42页珍藏版)》请在冰豆网上搜索。
c)//重载流插入
ostr<
'
('
c.real<
"
"
c.imaginary<
)'
endl;
returnostr;
friendcompoperator+(constcomp&
lhs,constcomp&
rhs)//重载+运算
compc1;
c1.real=lhs.real+rhs.real;
c1.imaginary=lhs.imaginary+rhs.imaginary;
returnc1;
}
friendcompoperator-(constcomp&
rhs)//重载—运算
c1.real=lhs.real-rhs.real;
c1.imaginary=lhs.imaginary-rhs.imaginary;
private:
intreal,imaginary;
};
intmain()
{
compc,d,e;
cin>
c>
d;
e=c+d;
cout<
两复数的和为:
e<
e=c-d;
两复数的差为:
}
题目2:
类模板的定义与实现1
根据栈数据结构的特点建立自己的栈类模板myStack,其中要包含栈的基本操作,最后测试之。
//mystack类
list>
cstdio>
iterator>
template<
typenameT>
classmystack
mystack();
//初始化操作,创建一个空栈
boolempty()const;
//判断栈是否为空
voidclearstack();
//清空栈
intsize();
//求栈长度
voidpush(constT&
item);
//在栈顶加入一个元素
voidpop();
//删除栈顶的一个元素
intgettop();
//获取栈顶元素
list<
T>
stacklist;
mystack<
:
mystack()
{}
boolmystack<
empty()const
returnstacklist.size()==0;
intmystack<
size()
returnstacklist.size();
template<
voidmystack<
clearstack()
stacklist.clear();
voidmystack<
push(constT&
item)
stacklist.push_back(item);
pop()
stacklist.pop_back();
T>
gettop()
returnstacklist.back();
mystack<
int>
ceshi;
inta=ceshi.empty();
//检测是否为空
isemptyornot?
endl<
a<
intnum;
enternumberintothestack:
;
while
(1)
{cin>
num;
ceshi.push(num);
charc=getchar();
if(c=='
\n'
)
break;
intb=ceshi.empty();
b<
intc=ceshi.size();
//获取栈顶长度
thelengthofthestack:
c<
outputtheelem:
for(inti=c;
i>
0;
i--)//测试pop函数
{cout<
ceshi.gettop()<
"
ceshi.pop();
intd=ceshi.empty();
//检测经pop后栈是否为空
d<
clearthestack..."
ceshi.clearstack();
//将栈清空
return0;
题目3:
类模板的定义与实现2
根据单向链表的特点,建立一个自己的链表类模板myList,其中要包含表链式存储的基本操作,最后测试之。
#include<
string>
classT>
classslistNode
public:
slistNode(){next=NULL;
Tdata;
//value
slistNode*next;
//pointtonextpos
classmyslist
private:
intlistlen;
slistNode<
*node;
//临时节点
*lastnode;
//headnode
*headnode;
//lastnode
myslist();
intlen();
//thelengthofthelist
voidaddback(Tx);
//addelemtothebackofthelist
voidDispList();
//outputthelist
boolisEmpty();
//
*find(Tx);
//查找值为x的节点,返回节点的地址
voidDelete(Tx);
//deletex`spos
voidinsert(Tx,slistNode<
*p);
//在p节点后插入值为x的节点
voidinsertnode(Tx);
//在链表的头部插入节点
myslist<
myslist()//conductor
{node=NULL;
lastnode=NULL;
headnode=NULL;
listlen=0;
inlineintmyslist<
len()//list长度计算
{if(listlen==0)
listisempty"
else
returnlistlen;
voidmyslist<
addback(Tx)//push_back
{node=newslistNode<
();
//申请一个新的节点
node->
data=x;
//新节点赋值为x
if(lastnode==NULL)//没有尾节点则链表为空,node既为头结点,又是尾节点
{headnode=node;
lastnode=node;
else//如果链表非空
{lastnode->
next=node;
//node既为尾节点的下一个节点
//node变成了尾节点,把尾节点赋值为node
listlen++;
//元素个数+1
DispList()//打印list
{node=headnode;
//用临时节点指向头结点
while(node!
=NULL)//遍历链表并输出
node->
data<
ends;
node=node->
next;
boolmyslist<
isEmpty()//判断list是否为空
returnlistlen==0;
slistNode<
*myslist<
find(Tx)//查找函数
=NULL&
&
data!
=x)//遍历链表,遇到值相同的节点跳出
returnnode;
//返回找到的节点的地址,如果没有找到则返回NULL
Delete(Tx)//删除list中的元素
*temp=headnode;
//申请一个临时节点指向头节点
if(temp==NULL)return;
//如果头节点为空,则该链表无元素,直接返回
if(temp->
data==x)
{headnode=temp->
//把头节点指向头节点的下一个节点
next==NULL)lastnode=NULL;
delete(temp);
//删除头节点
listlen--;
return;
while(temp->
next!
temp->
next->
=x)//在list中查找x
temp=temp->
next==NULL)return;
//如果没有找到则返回
next==lastnode)//如果找到的时候尾节点
{lastnode=temp;
//把尾节点指向他的上一个节点
delete(temp->
next);
//删除尾节点
temp->
next=NULL;
else//如果不是尾节点
{node=temp->
//用临时节点node指向要删除的节点
next=node->
delete(node);
//删除节点
node=NULL;
insert(Tx,slistNode<
*p)
{if(p==NULL)return;
node=newslistNode<
//申请一个新的空间
next=p->
p->
if(node->
next==NULL)//如果node为尾节点
insertnode(Tx)
next=headnode;
headnode=node;
{inttemp1,temp2;
charm;
myslist<
v;
//输入函数测试
pleaseenterelem:
cin>
temp1;
v.addback(temp1);
while(m=cin.get()!
='
v.addback(temp1);
//判断是否为空函数测试
if(v.isEmpty())
listisempty?
ture"
false"
//list长度函数测试
thelengthofthelistis:
v.len()<
display:
v.DispList();
//往list表头插入元素
push_front:
v.insertnode(temp1);
v.DispList();
//插入函数测试
entertheposyouwanttofollow:
entertheelemyouwanttoinsert:
temp2;
//查找函数测试
v.insert(temp2,v.find(temp1));
//删除函数测试
entertheelemyouwanttodelet:
v.Delete(temp1);
return0;
题目4:
迭代器的使用
用迭代器访问向量vector和表list的元素。
假设已知数据存在一个数组中,分别建立一个向量vector和一个list,然后用迭代器访问向量和表,分别求它们的最大值另外使用string类建立字符串,用迭代器访问每个字符。
vector>
{intarr[5]={1,-1,3,4,0};
list1;
vector<
vector1;
for(inti=0;
i<
5;
i++)
{vector1.push_back(arr[i]);
//将数组中的元素输入到向量中
list1.push_back(arr[i]);
//将数组中的元素输入到表中
//创建一个链表迭代器进行访问
intmax1=arr[0];
iteratoriter1;
for(iter1=list1.begin();
iter1!
=list1.end();
iter1++)
*iter1<
'
if(*iter1>
=max1)
max1=*iter1;
}cout<
themaxis:
max1<
//创建一个向量迭代器进行访问
intmax2=arr[0];
iteratoriter2=vector1.begin();
for(;
iter2!
=vector1.end();
iter2++)
*iter2<
if(*iter2>
max2=*iter2;
max2<
//string类创建字符串并访问
stringstr("
Helloworld!
);
for(string:
iteratorit=str.begin();
it!
=str.end();
it++)
cout<
*it;
endl;
题目5:
函数对象的使用
具体要求:
定义一个比较的函数对象类或模板,然后再定义一个插入排序函数或函数模板,写一个main测试它们。
代码实现:
classlessThan
booloperator()(constT&
x,constT&
y)const
returnx<
=y;
//à
à
?
£°
classlessThan2
booloperator()(constT&
returnx>
=y;
typenameT,typenameCompare>
voidinsertionSort(vector<
v,Comparecomp)
inttemp=1,i;
iteratorita;
iteratorit;
while(temp)
{temp=0;
for(ita=v.begin();
ita+1!
=v.end();
ita++)
{if(!
comp(*ita,*(ita+1)))
{i=*ita;
*ita=*(ita+1);
*(ita+1)=i;
temp=1;
}}}}
inta;
charc;
a;
v.push_back(a);
while(c=getchar()!
insertionSort(v,lessThan<
());
it=v.begin();
*it;
it++;
it!
it++)
cout<
insertionSort(v,lessThan2<
cout<
题目6:
各种排序算法比较
对各种内部排序算法如插入排序,冒泡排序,选择排序,快速排序,基数排序,堆排序、二路归并排序等进行实时计算比较,针对同样的数据计算每种方法耗时多少秒。
测试数据可以用随机产生的100000个1到999之间的整数。
#include"
d_random.h"
d_sort.h"
iomanip>
time.h>
returnx<
{randomNumberarr;
intnumber,num[100000];
rand1,rand2,rand3,rand4,rand5,rand6;
100000;
{number=arr.random()%1000;
num[i]=number;
rand1.push_back(number);
rand2.push_back(number);
rand3.push_back(number);
rand4.push_back(number);
rand5.push_back(number);
rand6.push_back(number);
doublestarttime,endtime;
//快速排序
starttime=clock();
qui
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 c+ 报告