版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验一类和对象实验课程名:面向对象程序设计(C++)专业班级: 学号: 实验时间: 实验地点: 指导教师:一、实验目的和要求理解类和对象的概念,掌握声明类和定义对象的方法。掌握构造函数和析构函数的实现方法。初步掌握使用类和对象编制C++程序。掌握对象数组、对象指针和string类的使用方法。掌握使用对象、对象指针和对象引用作为函数参数的方法。掌握类对象作为成员的使用方法。掌握静态数据成员和静态成员函数的使用方法。理解友元的概念和掌握友元的使用方法。二、实验容设计一个静态数组存储结构的顺序表类,要数据元素6,最后依次显示当前线性表中的数50实验代码:#include<iostream>usingnamespacestd;constintMaxSize=100; //100实际问题具体定义template<classT> //SeqListclassSeqList{public:SeqList{length=0;} //无参构造函数SeqList(Taintn); //有参构造函数~SeqList //析构函数为空intLength{returnlength;} //求线性表的长度TGet(inti); //iintLocate(Tx //的元素序号voidInsert(int iTx); //i为xTDelete(int i); //删除线性表的第i个元素voidPrintList(); //遍历线性表,按序号依次输各元素private:Tdata[MaxSize]; //存放数据元素的数intlength; //线性表的长度};template<classT>SeqList<T>::SeqList(Ta[],intn){inti;if(n>MaxSize)throw"参数非法";for(i=0;i<n;i++)data[i]=a[i];length=n;}template<classT>TSeqList<T>::Get(inti){if(i<1&&i>length)throw"查找位置非法";elsereturndata[i-1];}template<classT>intSeqList<T>::Locate(Tx){inti;for(i=0;i<length;i++)if(data[i]==xreturni+1; ii+1return0; //退出循环,说明查找失败}template<classT>voidSeqList<T>::Insert(inti,Tx){intj;if(length>=MaxSizethrow"上溢";if(i<1||i>length+1throw"位置";for(j=length;j>=i;j--)data[j]=data[j-1]; //注意第jj-1处data[i-1]=x;length++;}template<classT>TSeqList<T>::Delete(inti){Tx;intj;if(length==0)throw"下溢";if(i<1||i>length)throw"位置";x=data[i-1];for(j=i;j<length;j++)data[j-1]=data[j]; //j组下标length--;returnx;}template<classT>voidSeqList<T>::PrintList(){inti;for(i=0;i<length;i++)cout<<data[i]<<'';cout<<endl;}intmain(){intm,n,t;inta[10]={0,1,2,3,4,5,6,7,8,9};SeqList<int>seq(a,10);SeqList<int>*p;p=&seq;cout<<"线性表的长度为:"<<p->Length()<<endl;p->PrintList();cout<<"请输入要查找元素的位置:"<<endl;cin>>n;cout<<"您所要找的元素为:"<<p->Get(n)<<endl;cout<<"请输入要查找的元素值:"<<endl;cin>>n;cout<<"该值所在的位置为:"<<p->Locate(n)<<endl;cout<<"请分别输入插入位置与要插入的元素"<<endl;cin>>n>>t;p->Insert(n,t);p->PrintList();cout<<"请输入要删除的元素所在的位置:"<<endl;cin>>n;p->Delete(n);return0;}运行结果:设计一个带头结点的单链表类,要求:(尽量利用已知的存储空间。表类的正确性。实验代码:#include<iostream>usingnamespacestd;template<classT>structNode{Tdata;Node<T*next; //此处<T>也可以省略};template<classT>classLinkList{public:LinkList(){first=newNode<T>;first->next=NULL;} 建立只有头结点的空链表LinkList(Taintn); //n~LinkList //析构函数intLength //求单链表的长度TGet(inti); //取单链表中第i个结点的元素值intLocate(Tx); //求单链表中值为x的元素序号voidInsert(inti,Tx); //在单链表中第i个位置插入元素为x的结点TDelete(inti); //ivoidPrintList //素Node<T*first; //单链表的头指针};template<classT>LinkList<T>::LinkList(Ta[],intn)//头查法建立单链表{inti;Node<T>*s;first=newNode<T>;first->next=NULL; //for(i=n-1;i>=0i--){s=newNode<T>;s->data=a[i]; //为每个数组元素建立一个结点s->next=first->next; //first->next=s;}}template<classT>LinkList<T>::~LinkList()//析构函数{Node<T>*p,*q;p=first;{q=p;p=p->next;deleteq;}}template<classT>intLinkList<T>::Length()//求链表的长度{inti=0;Node<T>*p;p=first;while(p){p=p->next;i++;}returni-1;}template<classT>TLinkList<T>::Get(inti)//求单链表中第i个元素的值{intn=0;Node<T>*p;p=first;{p=p->next;n++;}returnp->data;}template<classT>intLinkList<T>::Locate(Tx)//求单链表中值为x的元素序号{inti;Node<T>*p;p=first;for(i=0;p;i++){if(p->data==x)returni;p=p->next;}}template<classT>voidLinkList<T>::PrintList()//输出函数{Node<T>*p;p=first->next;while(p){cout<<p->data<<'';p=p->next;}cout<<endl;}template<classT>voidLinkList<T>::Insert(intiT在第ix{intn=0;Node<T>*p,*q;p=first;while(p&&n<i-1){p=p->next;++n;}q=newNode<T>;q->data=x;p->next=q;}template<classT>TLinkList<T>::Delete(inti)//删除第i个结点{intn=0;Node<T>*p,*q;p=first;while(p->next&&n<i-1){p=p->next;++n;}q=p->next;p->next=q->next;returnq->data;deleteq;}intmain(){Node<int>*p,*q,*r;inta[10]={0,1,2,3,4,5,6,7,8,9};LinkList<int>L1(a,10),L2,L3;//定义三个链表cout<<L1.Length()<<endl;p=L1.first->next,q=L2.first,r=L3.first;L2.first=newNode<int>;L2.first->next=NULL;L3.first=newNode<int>;L3.first->next=NULL;while(p)//当链表L1中有元素时进行循环{if(p->data%2==0)//当L1中的元素为偶数时插入L2{q=newNode<int>;q->data=p->data;q->next=L2.first->next;L2.first->next=q;}else{r=newNode<int>;r->data=p->data;r->next=L3.first->next;L3.first->next=r;}p=p->next;}L1.PrintList();L2.PrintList();L3.PrintList();cout<<"链表的长度为:"<<L1.Length()<<endl;cout<<"链表的第四个元素为:"<<L1.Get(4)<<endl;cout<<"链表中元素5为第"<<L1.Locate(5)<<"个元素"<<endl;L1.Insert(4,17);cout<<"插入元素后链表为:";L1.PrintList();L1.Delete(8);cout<<"删除第8个元素后链表变为:";L1.PrintList();return0;}实验结果:设计一个不带头结点的单链表类,要求:不带头结点单链表类的成员函数包括取数据元素个k位置结点时的不同情况。)表类的正确性。实验代码:#include<iostream>usingnamespacestd;template<classT>structNode{Tdata;Node<T>*next;};template<classT>classLinkList{public:LinkList(){first=newNode<T>;first->next=NULL;} //有头结点的空链表LinkList(Ta[],intn);~LinkList();intLength //求单链表的长度TGet(inti); //ivoidInsert(intiTx); //i个位置插入元素值为x结点TDelete(inti); //ivoidPrintList(); //遍历单链表,按序号依次输出各元素private:Node<T*first; //单链表的头指针};template<classT>LinkList<T>::LinkList(Ta[],intn){inti=1;Node<T>*p,*q;first=newNode<T>;first->data=a[0];first->next=NULL;p=first;for(i=1;i<n;i++){q=newNode<T>;q->data=a[i];q->next=NULL;p->next=q;p=q;}}template<classT>LinkList<T>::~LinkList(){Node<T>*p;p=first;while(p){p=p->next;deletefirst;first=p;}}template<classT>intLinkList<T>::Length(){inti=0;Node<T>p=first;while(p){i++;}returni;}template<classT>TLinkList<T>::Get(inti){intj=1;Node<T>*p;p=first;while(p&&j<i){p=p->next;j++;}returnp->data;}template<classT>voidLinkList<T>::Insert(inti,Tx){intj=1;Node<T>*p,*q;p=first;while(p&&j<i-1){p=p->next;j++;}q=newNode<T>;q->data=x;p->next=q;}template<classT>TLinkList<T>::Delete(inti){intj=1;Node<T>*p,*q;p=first;while(p&&j<i-1){p=p->next;j++;}q=p->next;p->next=q->next;returnq->data;deleteq;}template<classT>voidLinkList<T>::PrintList(){Node<T>*p;p=first;while(p){cout<<p->data<<'';p=p->next;}cout<<endl;}intmain(){inta[10]={0,1,2,3,4,5,6,7,8,9};LinkList<int>L(a,10);cout<<"链表长为:"<<L.Length()<<endl;cout<<"链表的第6个元素为:"<<L.Get(6)<<endl;L.Insert(5,17);cout<<"在链表第5个位置插入元素17后链表变为:";L.PrintList();L.Delete(8);cout<<8L.PrintList();return0;}实验结果为:环问题;围坐-圈,每人持有一个正整数密码。开始时任意给出一个报数上限值m1数。报到m时停止报数,报mm1顺序报数.如此下去,直到所有人全部出列为止。要求设计一个程序模拟此过程,并给出出列人的编号序列。测试数据:n=7,7个人的密码依次为3,1,7,2,4,8,4初始报数上限值m=20实验代码:#include<iostream>usingnamespacestd;structNode//定义一个接点包含编号,密码,指针变量{intnum;intcode;intdata;//标志位,当该人没有被踢出时为1Node*next;};classCirList{public:voidDone();//踢人,实现约瑟夫环的功能CirList();//构造函数,建立一个带头结点的循环链表voidinputcode();//输入密码private:Node*first;intperson;//定义游戏人数};CirList::CirList(){inti;Node*p;first=newNode;first->next=NULL;cout<<"请输入游戏人数:";cin>>person;for(i=person;i>0;i--){p=newp->num=i;p->data=1;p->next=first->next;first->next=p;}while(p->next){p=p->next;}p->next=first;}voidCirList::inputcode(){Node*p;inti;p=first->next;cout<<"请输入每个人的密码:";for(i=0;i<person;i++){cin>>p->code;p=p->next;}}voidCirList::Done(){intm=20,n,i=1;Node*p;p=first;p=p->next;for(n=person;n>0;n--){while(i!=m){p=p->next;if(p->data==1){i++;}}cout<<p->num<<'';m=p->code;p->data=0;i=0;}cout<<endl;}intmain(){CirListlist;list.inputcode();list.Done();return0;}实验结果:*5.设计一个带头结点的循环双向链表类,要求:元素个数、插入、删除、取数据元素。设计一个测试主函数,实际运行验证所设计循环双向实验代码:#include<iostream>usingnamespacetemplate<classT>structNode{Tdata;Node<T>*next,*front;};template<classT>classLinkList{public:LinkList( ){first=new Node<T>;first->next=NULL;first->front=NULL;}//建立只有头结点的空链表LinkList(Ta[intn);//n链表~LinkList();//析构函数intLength();//求双向链表的长度TGet(inti); i元素值intLocate(Tx); //x序号voidInsert(inti,Tx); //ixTDelete(inti); i点voidPrintList( //依次输出各元素private:Node<T>*first;//双向链表的头指针};template<classT>LinkList<T>::LinkList(Ta[],intn){inti;Node<T>*p,*q;first=newNode<T>;first->front=NULL;first->next=NULL;p=newNode<T>;p->data=a[0];first->next=p;p->front=first;p->next=NULL;for(i=1;i<n;i++){q=newq->front=p;p->next=q;q->next=NULL;q->data=a[i];p=q;}p->next=first;first->front=p;}template<classT>LinkList<T>::~LinkList(){Node<T>*p,*q;p=first;while(p->next==first){q=p;p=p->next;deleteq;p->front=NULL;}deletep;}template<classT>intLinkList<T>::Length(){Node<T>*p;inti=0;p=first;while(p->next!=first){p=p->next;i++;}returni;}template<classT>TLinkList<T>::Get(inti){Node<T>*p;intj=0;p=first;while(j<i&&p->next!=first){p=p->next;j++;}returnp->data;}template<classT>intLinkList<T>::Locate(Tx){inti=0;Node<T>*p;p=first;while(p->data!=x&&p->next!=first){p=p->next;i++;}returni;}template<classT>voidLinkList<T>::Insert(inti,Tx){Node<T>*p,*q;intj=0;p=first;while(j<i-1&&p->next!=first){p=p->next;j++;}q=newq->data=x;q->next=p->next;p->next->front=q;p->next=q;q->front=p;}template<classT>TLinkList<T>::Delete(inti){Node<T>*p,*q;intj=0;p=first;while(j<i-1&&p->next!=first){p=p->next;j++;}q=p->next;p->next=q->next;q->next->front=p;deleteq;returnq->data;}template<classT>voidLinkList<T>::PrintList()//输出函数{Node<T>*p;p=first->next;while(p!=first){cout<<p->data<<'';p=p->next;}cout<<endl;}intmain(){inta[10]={0,1,2,3,4,5,6,7,8,9};LinkList<int>L(a,10);L.PrintList();cout<<"链表的长度为:"<<L.Length()<<endl;cout<<"链表的第四个元素为:"<<L.Get(4)<<endl;cout<<"链表中元素5"<<L.Locate(5)<<"<<endl;L.Insert(4,17);cout<<17L.PrintList(L.Delete(8);cout<<8L.PrintList(return0;}实 验 结 果 :*6.设计一个单链表实现一元多项式求和问题。要求:设计存储结构表示一元多项式;设计算法实现一元多项式相加。实验代码:#include<iostream>usingnamespacestd;structNode{inte;intx;Node*next;};classLinkList{public:LinkList(){first=newNode;first->next=NULL;}LinkList(inta[],intb[],intn); //nvoidPrintList(LinkList&L); //元素friendvoidfunction(LinkList&La,LinkList&Lb,LinkList&Lc);//友元函数,多项式的加减private:Node*first; //单链表的头指针};LinkList::LinkList(inta[],intb[],intn)//生成多项式{first=newNode; //生成头结点Node*r=first; //尾指针初始化for(inti=0;i<n;i++){Node*s=newNode;s->e=a[i];s->x=b[i];r->next=s;r=s; //插入到终端结点之后}r->next=NULL; //单链表建立完毕,将终端结点的指针域置空}voidLinkList::PrintList(LinkList&L)//输出多项式{cout<<"多项式为:";Node*p=first->next;while(p){if(!p->next)cout<<p->x<<"x"<<p->e<<endl;elsecout<<p->x<<"x"<<p->e<<"+";p=p->next;}}intmain(){voidfunction(LinkList&La,LinkList&Lb,LinkList&Lc); //加减inta1[100],b1[100],n;cout<<"请输入多项式La的项数n"<<endl;cin>>n;cout<<"请输入多项式La的指数(按从小到大的次序)"<<endl;for(int
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026重庆万州区长滩镇非全日制公益性岗位招聘2人备考题库附答案详解(夺分金卷)
- 2026中日青年交流中心有限公司社会招聘3人备考题库及答案详解参考
- 2026浙江宁波市鄞州区公立学校招聘编外员工4人备考题库及答案详解(各地真题)
- 国海证券分支机构2026届“管培生”春季校园招聘25人备考题库附答案详解(基础题)
- 2026浙江舟山市青少年活动中心编外教师招聘1人备考题库及答案详解(新)
- 2026年跨境电商品牌产品认证全攻略:合规路径与实战指南
- 2026年供应链溯源区块链智能合约测试与应用
- 2026康复大学招聘专任教师48名笔试模拟试题及答案解析
- 2026福建厦门翔安区珩厝小学招聘非在编合同教师(代病假)1人考试备考试题及答案解析
- 2026湖南郴州市北湖区招聘教师23人笔试备考题库及答案解析
- 我的家乡湖南长沙宣传简介
- 北师大版一年级数学下册《捉迷藏》说课稿课件
- 高考英语高频词组+短语+固定搭配
- 撤销冒名登记备案申请书
- 危重病人抢救评分标准
- 中国缺血性卒中和短暂性脑缺血发作二级预防指南(2022年版)解读
- GB.T19418-2003钢的弧焊接头 缺陷质量分级指南
- YB/T 5051-1997硅钙合金
- GB/T 15796-2011小麦赤霉病测报技术规范
- 2023年上海铁路局校园招聘笔试模拟试题及答案解析
- 厚度自动控制和板形控课件
评论
0/150
提交评论