1、数据结构课程设计实习报告球#include #include class Point /二维坐标点类protected: double x,y,z;public: Point()x=0; y=0;z=0; /缺省构造函数 Point(double px,double py,double pz)x=px;y=py;z=pz;/带参数的构造函数 double Getx()return x; /返回坐标点的x值 double Gety()return y; /返回坐标点的y值 double Getz()return z; void Move(double mx,double my,double mz
2、)x=mx;y=my;z=mz; /移动坐标点的位置 void Show()coutx=x y=y z=zradius) return 1; /在矩形外 else if(sqrt(pt.Getx()-x)*(pt.Getx()-x)+(pt.Gety()-y)*(pt.Gety()-y)+(pt.Getz()-z)*(pt.Getz()-z)radius) return -1; /在矩形内 else return 0; /在矩形边上void Triangle:Judge(Point &pt) pt.Show(); Show(); switch(Position(pt) case 0:cout点
3、在圆边上endlendl;break; case -1:cout点在圆内endlendl;break; case 1:cout点在圆外endlendl;break; void Triangle:Show() /输出矩形的各属性值和面积 coutrx=x ry=y rz=z ; coutradius=radiust; coutarea=Area() ; coutvolumeVolume()endl;void main() Triangle rt1(0,0,0,8),rt2,rt3(rt1);/定义Rectangle对象 Point p1(0,0,0),p2(1,2,3),p3(5,7,9); /
4、定义Point对象 cout描述rt1如下:endl; rt1.Show(); /输出矩形rt1的属性值 cout描述rt2如下:endl; rt2.Show(); /输出矩形rt2的属性值 rt1.Set(3,5,7); /重置矩形rt1左下角的位置 rt2=rt1; /rt2复制矩形rt1 coutrt2复制圆rt1并描述endl; rt2.Show(); cout点p1和圆rt3并描述:endl; rt3.Judge(p1); /判断点p1相对于矩形rt3的位置 rt3.Set(9,7,5); /重置矩形rt3左下角的位置 cout点p2和圆rt3并描述:endl; rt3.Judge(
5、p2); /判断点p2相对于矩形rt3的位置 cout由p3定义的圆rt4并描述:endl; Triangle rt4(p3); /由p3定义的矩形rt4 rt4.Show(); 单链表#ifndef LINKNODE_H#define LINKNODE_H#include#includeLinknode.h#includetemplate /结点类 struct LinkNode T data; LinkNode *link; LinkNode(LinkNode *ptr=NULL) link=ptr; LinkNode(const T& item, LinkNode *ptr=NULL)
6、data=item; link=ptr; ;template class List /单链表类 protected: LinkNode *first; public: List() first=new LinkNode; List() makeEmpty(); void makeEmpty(); LinkNode *getHead() const return first; void inverse(); void input(T endTag); void output(); void Union(List&LA,List&LB); void jishu(List&L); void oush
7、u(List&L);template /置空 void List:makeEmpty() LinkNode *q; while (first-link!=NULL) q=first-link; first-link=q-link; delete q; template /输出void List:output() LinkNode*current=first-link; while ( current!=NULL ) coutdatalink; coutendl;template /后插法建立void List:input(T endTag) LinkNode*newNode,*last; T
8、val; makeEmpty(); cinval;last=first; while(val!=endTag) newNode=new LinkNode(val); if(newNode=NULL)cerr存储分配错误!link=newNode; last=newNode; cinval; last-link=NULL;template /自身逆序void List: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;
9、first-link=pr;template /合并void List: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;
10、p1=p1-link; last=last-link; while(p2!=NULL) last-link=p2; p2=p2-link; last=last-link; last=NULL; template /拆分出奇数void List:jishu(List&L) T value; 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=new Li
11、nkNode(value); last=last-link; last=first;template /拆分出偶数void List:oushu(List&L) T value; 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=new LinkNode(value); last=last-link; last=first;#endifvoid mai
12、n() Listlist1,list2,list3,list4,list5; cout输入链表1,并以0为结束符:endl; list1.input(0); cout输入链表2,并以0为结束符:endl; list2.input(0); list3.Union(list1,list2); cout输出合并后的链表3:endl; list3.output(); list3.inverse(); cout输出逆序后的链表3:endl; list3.output(); list4.jishu(list3); cout输入拆分后的奇数链表4:endl; list4.output(); list5.ou
13、shu(list3); cout输入拆分后的偶数链表5:endl; list5.output();中缀转后缀#ifndef SEQSTACK_H#define SEQSTACK_H#include#includeconst int stackIncreament=20; template class SeqStack /顺序栈类定义private: T *elements; int top; int maxSize; void overflowProcess(); public: SeqStack(int sz=50); SeqStack()deleteelements; void Push(
14、const T& x); bool Pop(T& x); bool getTop(T& x); bool IsEmpty()const return(top=-1)?true:false; bool IsFull()const return(top=maxSize-1)?true:false; int getSize()constreturn top+1; void MakeEmpty()top=-1; void postfix(char x); bool isdigit(T x)const; int isp(char x); int icp(char x);templateSeqStack:
15、SeqStack(int sz) /构造函数 top=-1; maxSize=sz; elements=new TmaxSize; assert(elements!=NULL); template void SeqStack:overflowProcess() /私有函数:当栈满则执行扩充栈存储空间处理 T *newArray=new TmaxSize+stackIncreament; if(newArray=NULL) cerr存储分配失败!endl;exit(1); for (int i=0;i=top;i+) newArrayi=elementsi; maxSize=maxSize+st
16、ackIncreament; deleteelements; elements=newArray; template /进栈void SeqStack:Push(const T& x) if(IsFull()=true) overflowProcess(); elements+top=x; template /退栈bool SeqStack:Pop(T& x) if (IsEmpty()=true) return false; x=elementstop-; return true; template /取栈顶bool SeqStack:getTop(T& x) if (IsEmpty()=t
17、rue) return false; x=elementstop; return true;template /转换函数void SeqStack:postfix(char x) char ch=#,ch1,op; Push(ch);cin.get(ch); while(IsEmpty()=false) if(isdigit(ch) coutch; cin.get(ch); else getTop(ch1); if(isp(ch1)icp(ch) Pop(op); coutop; else Pop(op); if(op=() cin.get(ch); template /判断函数bool Se
18、qStack:isdigit(T x)const if(x=A&x=z) return true; else return false;template /栈内优先规定int SeqStack:isp(char x) int temp; 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; return temp;template /栈外优先规定int SeqStack:icp(cha
19、r x) int temp; 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; return temp;#endifvoid main() SeqStacks; cout请输入中缀表达式,并以#为结束符:endl; s.postfix(#); coutendl;通讯本#include #include #include #include #include #include #include
20、#include mybook.hvoid main() Phonebook phon1; struct item newitem; char searchname30;/ char dailname30; int select,find; struct item *p; int isthere; while(1) system(cls); isthere=0; coutsetw(50)0endl; coutsetw(20)0*endl; coutsetw(20)0* 通讯录! *endl; coutsetw(20)0* *endl; coutsetw(20)0* 请选择你要操作的目录! *e
21、ndl; coutsetw(20)0* *endl; coutsetw(20)0* 1.增加 2.打印 3.查看 4.修改 *endl; coutsetw(20)0* *endl; coutsetw(20)0* 5.删除 6.查找 7.保存并退出 *endl; coutsetw(20)0* *endl; coutsetw(20)0*endl; coutsetw(20)0 copyright by 2010endl; coutendl; coutsetw(20)0select; coutendl; switch(select) case 1: phon1.additem(newitem); break; case 2: coutsetw(15)0请输入您要打印的分类:请选择(A.亲人 B.朋友 C.同学); char sel; coutsetw(35)sel; phon1sel; break; case 3: phon1.showname(); break; case 4: coutsetw(15)0searchname; p=phon1.head; while (p!=NULL) if (strcmp(p-name,searchname)=0) isthere=1; break; p=p-next; if (isthere=0) coutendlsetw(15)0提示:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1