数据结构课程设计实习报告.docx
《数据结构课程设计实习报告.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计实习报告.docx(47页珍藏版)》请在冰点文库上搜索。
![数据结构课程设计实习报告.docx](https://file1.bingdoc.com/fileroot1/2023-5/24/5c66f49f-bb88-4e88-ae40-0a3283f21622/5c66f49f-bb88-4e88-ae40-0a3283f216221.gif)
数据结构课程设计实习报告
球
#include
#include
classPoint{//二维坐标点类
protected:
doublex,y,z;
public:
Point(){x=0;y=0;z=0;}//缺省构造函数
Point(doublepx,doublepy,doublepz){x=px;y=py;z=pz;}//带参数的构造函数
doubleGetx(){returnx;}//返回坐标点的x值
doubleGety(){returny;}//返回坐标点的y值
doubleGetz(){returnz;}
voidMove(doublemx,doublemy,doublemz){x=mx;y=my;z=mz;}//移动坐标点的位置
voidShow(){cout<<"x="<};
classTriangle:
publicPoint{//矩形类(Point的派生类,Point对象为矩形左下角的坐标点)
doubleradius;//矩形的宽度和高度
public:
Triangle(){x=0;y=0;z=0;radius=0;}//缺省构造函数
Triangle(doublea,doubleb,doublec,doubled):
Point(a,b,c){//带参数的构造函数
radius=d;
}
Triangle(Point&pn):
Point(pn){//带参数的构造函数,由点pn定义矩形左下角的位置
radius=1;
}
doubleVolume(){return3.14*4/3*radius*radius*radius;}//求矩形的面积
doubleArea(){return4*3.14*radius*radius;}
voidSet(doublea,doubleb,doublec){x=a;y=b;z=c;}//重置矩形的左下角坐标
intPosition(Point&pt);//求点pt相对于矩形的位置,返回-1(在矩形内),
//0(在矩形边上)和1(在矩形外)
voidJudge(Point&pt);//判断点pt相对于矩形的位置,给出提示
voidShow();//输出矩形的各属性值和面积
};
intTriangle:
:
Position(Point&pt){//求点pt相对于矩形的位置
if(sqrt((pt.Getx()-x)*(pt.Getx()-x)+(pt.Gety()-y)*(pt.Gety()-y)+(pt.Getz()-z)*(pt.Getz()-z))>radius)return1;//在矩形外
elseif(sqrt((pt.Getx()-x)*(pt.Getx()-x)+(pt.Gety()-y)*(pt.Gety()-y)+(pt.Getz()-z)*(pt.Getz()-z))elsereturn0;//在矩形边上
}
voidTriangle:
:
Judge(Point&pt){
pt.Show();
Show();
switch(Position(pt)){
case0:
cout<<"点在圆边上"<case-1:
cout<<"点在圆内"<case1:
cout<<"点在圆外"<}
}
voidTriangle:
:
Show(){//输出矩形的各属性值和面积
cout<<"rx="<cout<<"radius="<cout<<"area="<
cout<<"volume"<}
voidmain(){
Trianglert1(0,0,0,8),rt2,rt3(rt1);//定义Rectangle对象
Pointp1(0,0,0),p2(1,2,3),p3(5,7,9);//定义Point对象
cout<<"描述rt1如下:
"<rt1.Show();//输出矩形rt1的属性值
cout<<"描述rt2如下:
"<rt2.Show();//输出矩形rt2的属性值
rt1.Set(3,5,7);//重置矩形rt1左下角的位置
rt2=rt1;//rt2复制矩形rt1
cout<<"rt2复制圆rt1并描述"<rt2.Show();
cout<<"点p1和圆rt3并描述:
"<rt3.Judge(p1);//判断点p1相对于矩形rt3的位置
rt3.Set(9,7,5);//重置矩形rt3左下角的位置
cout<<"点p2和圆rt3并描述:
"<rt3.Judge(p2);//判断点p2相对于矩形rt3的位置
cout<<"由p3定义的圆rt4并描述:
"<Trianglert4(p3);//由p3定义的矩形rt4
rt4.Show();
}
单链表
#ifndefLINKNODE_H
#defineLINKNODE_H
#include
#include"Linknode.h"
#include
template//结点类
structLinkNode
{Tdata;
LinkNode*link;
LinkNode(LinkNode*ptr=NULL)
{link=ptr;}
LinkNode(constT&item,LinkNode*ptr=NULL)
{data=item;link=ptr;}
};
template
classList//单链表类
{protected:
LinkNode*first;
public:
List(){first=newLinkNode;}
~List(){makeEmpty();}
voidmakeEmpty();
LinkNode*getHead()const{returnfirst;}
voidinverse();
voidinput(TendTag);
voidoutput();
voidUnion(List&LA,List&LB);
voidjishu(List&L);
voidoushu(List&L);
};
template//置空
voidList:
:
makeEmpty()
{LinkNode*q;
while(first->link!
=NULL)
{q=first->link;
first->link=q->link;
deleteq;
}
}
template//输出
voidList:
:
output()
{LinkNode*current=first->link;
while(current!
=NULL)
{cout<data<<"";
current=current->link;
}
cout<}
template//后插法建立
voidList:
:
input(TendTag){
LinkNode*newNode,*last;
Tval;
makeEmpty();
cin>>val;last=first;
while(val!
=endTag){
newNode=newLinkNode(val);
if(newNode==NULL){cerr<<"存储分配错误!
"<(1);}
last->link=newNode;
last=newNode;
cin>>val;
}
last->link=NULL;
}
template//自身逆序
voidList:
:
inverse(){
if(first==NULL)return;
LinkNode*p=first->link,*pr=NULL,*r;
while(p!
=NULL){
r=p->link;
p->link=pr;
pr=p;
p=r;
}
first->link=pr;
}
template//合并
voidList:
:
Union(List&LA,List&LB)
{
LinkNode*p1=LA.getHead();
LinkNode*p2=LB.getHead();
LinkNode*last;
last=first;
p1=p1->link;p2=p2->link;
while(p1!
=NULL&&p2!
=NULL)
{
last->link=p1;
p1=p1->link;
last=last->link;
last->link=p2;
p2=p2->link;
last=last->link;
}
while(p1!
=NULL)
{
last->link=p1;
p1=p1->link;
last=last->link;
}
while(p2!
=NULL)
{
last->link=p2;
p2=p2->link;
last=last->link;
}
last=NULL;
}
template//拆分出奇数
voidList:
:
jishu(List&L)
{
Tvalue;
LinkNode*p1=L.getHead();
LinkNode*newNode,*last;
last=first;
p1=p1->link;
while(p1!
=NULL)
{
value=p1->data;
p1=p1->link;
if(value%2!
=0)
{last->link=newNode=newLinkNode(value);
last=last->link;}
}
last=first;
}
template//拆分出偶数
voidList:
:
oushu(List&L)
{
Tvalue;
LinkNode*p1=L.getHead();
LinkNode*newNode,*last;
last=first;
p1=p1->link;
while(p1!
=NULL)
{
value=p1->data;
p1=p1->link;
if(value%2==0)
{last->link=newNode=newLinkNode(value);
last=last->link;}
}
last=first;
}
#endif
voidmain()
{
Listlist1,list2,list3,list4,list5;
cout<<"输入链表1,并以‘0’为结束符:
"<list1.input(0);
cout<<"输入链表2,并以‘0’为结束符:
"<list2.input(0);
list3.Union(list1,list2);
cout<<"输出合并后的链表3:
"<list3.output();
list3.inverse();
cout<<"输出逆序后的链表3:
"<list3.output();
list4.jishu(list3);
cout<<"输入拆分后的奇数链表4:
"<list4.output();
list5.oushu(list3);
cout<<"输入拆分后的偶数链表5:
"<list5.output();
}
中缀转后缀
#ifndefSEQSTACK_H
#defineSEQSTACK_H
#include
#include
constintstackIncreament=20;
template
classSeqStack
{//顺序栈类定义
private:
T*elements;
inttop;
intmaxSize;
voidoverflowProcess();
public:
SeqStack(intsz=50);
~SeqStack(){delete[]elements;}
voidPush(constT&x);
boolPop(T&x);
boolgetTop(T&x);
boolIsEmpty()const
{return(top==-1)?
true:
false;}
boolIsFull()const
{return(top==maxSize-1)?
true:
false;}
intgetSize()const{returntop+1;}
voidMakeEmpty(){top=-1;};
voidpostfix(charx);
boolisdigit(Tx)const;
intisp(charx);
inticp(charx);
};
template
SeqStack:
:
SeqStack(intsz)//构造函数
{
top=-1;
maxSize=sz;
elements=newT[maxSize];
assert(elements!
=NULL);
}
template
voidSeqStack:
:
overflowProcess()
//私有函数:
当栈满则执行扩充栈存储空间处理
{
T*newArray=newT[maxSize+stackIncreament];
if(newArray==NULL)
{cerr<<"存储分配失败!
"<(1);}
for(inti=0;i<=top;i++)
newArray[i]=elements[i];
maxSize=maxSize+stackIncreament;
delete[]elements;
elements=newArray;
}
template//进栈
voidSeqStack:
:
Push(constT&x)
{
if(IsFull()==true)
overflowProcess();
elements[++top]=x;
}
template//退栈
boolSeqStack:
:
Pop(T&x)
{
if(IsEmpty()==true)
returnfalse;
x=elements[top--];
returntrue;
}
template//取栈顶
boolSeqStack:
:
getTop(T&x)
{
if(IsEmpty()==true)
returnfalse;
x=elements[top];
returntrue;
}
template//转换函数
voidSeqStack:
:
postfix(charx)
{
charch='#',ch1,op;
Push(ch);cin.get(ch);
while(IsEmpty()==false)
{
if(isdigit(ch))
{
cout<cin.get(ch);
}
else
{
getTop(ch1);
if(isp(ch1){
Push(ch);
cin.get(ch);
}
elseif(isp(ch1)>icp(ch))
{
Pop(op);
cout<}
else
{
Pop(op);
if(op=='(')
cin.get(ch);
}
}
}
}
template//判断函数
boolSeqStack:
:
isdigit(Tx)const
{
if(x>='A'&&x<='z')
returntrue;
elsereturnfalse;
}
template//栈内优先规定
intSeqStack:
:
isp(charx)
{
inttemp;
switch(x)
{case'#':
temp=0;break;
case'(':
temp=1;break;
case'*':
case'/':
case'%':
temp=5;break;
case'+':
case'-':
temp=3;break;
case')':
temp=6;break;
}
returntemp;
}
template//栈外优先规定
intSeqStack:
:
icp(charx)
{
inttemp;
switch(x)
{case'#':
temp=0;break;
case'(':
temp=6;break;
case'*':
case'/':
case'%':
temp=4;break;
case'+':
case'-':
temp=2;break;
case')':
temp=1;break;
}
returntemp;
}
#endif
voidmain()
{
SeqStacks;
cout<<"请输入中缀表达式,并以‘#’为结束符:
"<s.postfix('#');
cout<}
通讯本
#include
#include
#include
#include
#include
#include
#include
#include"mybook.h"
voidmain()
{
Phonebookphon1;
structitemnewitem;
charsearchname[30];
//chardailname[30];
intselect,find;
structitem*p;
intisthere;
while
(1)
{
system("cls");
isthere=0;
cout<cout<cout<**"<cout<cout<**"<cout<cout<cout<cout<cout<cout<cout<cout<cout<";
cin>>select;cout<switch(select)
{
case1:
phon1.additem(newitem);
break;
case2:
cout<请选择(A.亲人B.朋友C.同学)";
charsel;
cout<>sel;
phon1[sel];
break;
case3:
phon1.showname();break;
case4:
cout<";
cin>>searchname;
p=phon1.head;
while(p!
=NULL)
{
if(strcmp(p->name,searchname)==0)
{
isthere=1;
break;
}
p=p->next;
}
if(isthere==0)
{
cout<