




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗數(shù)組、結(jié)構(gòu)體和函數(shù)綜合編程練習(xí)1.學(xué)生成績統(tǒng)計從鍵盤輸入一個班(全班最多不超過30人)學(xué)生某門課的成績,當輸入成績?yōu)樨撝禃r,輸入結(jié)束,分別實現(xiàn)下列功能:(1)統(tǒng)計不及格人數(shù)并打印不及格學(xué)生名單;(2)統(tǒng)計成績在全班平均分及平均分之上的學(xué)生人數(shù),并打印這些學(xué)生的名單;(3)統(tǒng)計各分數(shù)段的學(xué)生人數(shù)及所占的百分比。注:將成績分為六個分數(shù)段,60分以下為第0段,6069為第1段,7079為第2段,8089為第3段,9099為第4段,100分為第5段。編程要求:1 .較好的用戶輸入輸出提示信息2 .使用子函數(shù)來實現(xiàn)上述各個功能,并且要使用結(jié)構(gòu)體數(shù)組來實現(xiàn),該結(jié)構(gòu)體中包括學(xué)生學(xué)號和成績3 .最好不要使
2、用全局變量#include<stdio.h>#defineARR_SIZE30typedefstructtagStudentlongnum;學(xué)生學(xué)號floatscore;學(xué)生分數(shù)Student;intReadScore(Studentstu);intGetFail(Studentstu,intn);floatGetAver(Studentstu口,intn);intGetAboveAver(Studentstu,intn);voidGetDetail(Studentstu,intn);main()intn,fail,aboveAver;StudentstuARR_SIZE;prin
3、tf("Pleaseenternumandscoreuntilscore<0:n");n=ReadScore(stu);printf("Totalstudents:%dn",n);fail=GetFail(stu,n);printf("Failstudents=%dn",fail);aboveAver=GetAboveAver(stu,n);printf("Aboveaverstudents=%dn",aboveAver);GetDetail(stu,n);/*函數(shù)功能:從鍵盤輸入一個班學(xué)生某門課的成績及其學(xué)
4、號當輸入成績?yōu)樨撝禃r,輸入結(jié)束函數(shù)參數(shù):存放學(xué)生信息的Student結(jié)構(gòu)體數(shù)組函數(shù)返回值:學(xué)生總數(shù)*/intReadScore(Studentstu)inti=0;scanf("%ld%f",&stui.num,&stui.score);while(stui.score>=0)i+;scanf("%ld%f",&stui.num,&stui.score);returni;/*函數(shù)功能:統(tǒng)計不及格人數(shù)并打印不及格學(xué)生名單函數(shù)參數(shù):存放學(xué)生信息的Student結(jié)構(gòu)體數(shù)組整型變量n,存放學(xué)生總數(shù)函數(shù)返回值:不及格人數(shù)*/i
5、ntGetFail(Studentstu,intn)inti,count;printf("Fail:nnumber-scoren");count=0;for(i=0;i<n;i+)if(stui.score<60)printf("%ld%.0fn",stui.num,stui.score);count+;returncount;/*函數(shù)功能:計算全班平均分函數(shù)參數(shù):存放學(xué)生信息的Student結(jié)構(gòu)體數(shù)組整型變量n,存放學(xué)生總數(shù)函數(shù)返回值:平均分*/floatGetAver(Studentstu,intn)inti;floatsum=0;for
6、(i=0;i<n;i+)sum=sum+stui.score;returnsum/n;/*函數(shù)功能:統(tǒng)計成績在全班平均分及平均分之上的學(xué)生人數(shù)并打印其學(xué)生名單函數(shù)參數(shù):存放學(xué)生信息的Student結(jié)構(gòu)體數(shù)組整型變量n,存放學(xué)生總數(shù)函數(shù)返回值:成績在全班平均分及平均分之上的學(xué)生人數(shù)*/intGetAboveAver(Studentstu,intn)inti,count;floataver;aver=GetAver(stu,n);printf("aver=%fn",aver);printf("Aboveaver:nnumber-scoren");cou
7、nt=0;for(i=0;i<n;i+)if(stui.score>=aver)printf("%ld%.0fn",stui.num,stui.score);count+;returncount;/*函數(shù)功能:統(tǒng)計各分數(shù)段的學(xué)生人數(shù)及所占的百分比函數(shù)參數(shù):存放學(xué)生信息的Student結(jié)構(gòu)體數(shù)組整型變量n,存放學(xué)生總數(shù)函數(shù)返回值:無*/voidGetDetail(Studentstu,intn)inti,j,stuLevel6;for(i=0;i<6;i+)stuLeveli=0;for(i=0;i<n;i+)if(stui.score<60)j
8、=0;elsej=(int)stui.score-50)/10;stuLevelj+;for(i=0;i<6;i+)if(i=0)printf("<60%d%.2f%n",stuLeveli,(float)stuLeveli/(float)n*100);elseif(i=5)printf("%d%d%.2f%n",(i+5)*10,stuLeveli,(float)stuLeveli/(float)n*100);elseprintf("%d-%d%d%.2f%n",(i+5)*10,(i+5)*10+9,stuLeveli
9、,(float)stuLeveli/(float)n*100);2成績排名次某班期末考試科目為數(shù)學(xué)(MT、英語(EN)和物理(PH»,有最多不超過30人參加考試。要求:(1)計算每個學(xué)生的總分和平均分;(2)按總分成績由高到低排出成績的名次;(3)打印出名次表,表格內(nèi)包括學(xué)生編號、各科分數(shù)、總分和平均分;(4)任意輸入一個學(xué)號,能夠查找出該學(xué)生在班級中的排名及其考試分數(shù)#include<stdio.h>#defineSTU30typedefstructtagStudentlongnum;/學(xué)號floataver;/平均分intsum;/總分intmath;/數(shù)學(xué)(MT)i
10、ntenglish;/英語(EN)intphysics;/物理(PH)Student;voidInput(Studentstu,intn);voidGetSumAver(Studentstu,intn);voidSort(Studentstu,intn);voidPrint(Studentstu,intn);intSearch(Studentstu,intn,longx);main()intn,pos;longx;StudentstuSTU;printf("Pleaseenterthetotalnumberofthestudents(n<=30):");scanf(&
11、quot;%d",&n);/*輸入?yún)⒓涌荚嚨膶W(xué)生人數(shù)*/printf("EnterNo.andscoreas:MTENPHn");Input(stu,n);/*輸入學(xué)生成績*/GetSumAver(stu,n);/*計算總分和平均分*/printf("Beforesort:n");Print(stu,n);Sort(stu,n);/*排名次*/printf("Aftersort:n");Print(stu,n);*/printf("Pleaseentersearchingnumber:");sca
12、nf("%ld",&x);/*以長整型格式輸入待查找學(xué)生的學(xué)號pos=Search(stu,n,x);/*名次查詢*/if(pos!=-1)printf("position:tNOtMTtENtPHtSUMtAVERn");printf("%8dt%4ldt%4dt%4dt%4dt%5dt%5.0fn",pos+1,stupos.num,stupos.math,stupos.english,stupos.physics,stupos.sum,stupos.aver);elseprintf("Notfound!n&qu
13、ot;);/*函數(shù)功能:輸入某班學(xué)生期末考試三門課程成績函數(shù)參數(shù):結(jié)構(gòu)體數(shù)組stu,存放學(xué)生信息整型變量n,存放學(xué)生人數(shù)函數(shù)返回值:無*/voidInput(Studentstu,intn)inti;for(i=0;i<n;i+)scanf("%ld",&stui.num);scanf("%d",&stui.math);scanf("%d",&stui.english);scanf("%d",&stui.physics);/*函數(shù)功能:計算每個學(xué)生的總分和平均分函數(shù)參數(shù):結(jié)構(gòu)體
14、數(shù)組stu,存放學(xué)生信息整型變量n,存放學(xué)生人數(shù)函數(shù)返回值:無*/voidGetSumAver(Studentstu,intn)inti;for(i=0;i<n;i+)stui.sum=stui.english+stui.math+stui.physics;stui.aver=(float)stui.sum/3;/*函數(shù)功能:按總分成績由高到低排出成績的名次函數(shù)參數(shù):結(jié)構(gòu)體數(shù)組stu,存放學(xué)生信息整型變量n,存放學(xué)生人數(shù)函數(shù)返回值:無*/voidSort(Studentstu,intn)inti,j,k;StudenttempStu;for(i=0;i<n-1;i+)k=i;for
15、(j=i+1;j<n;j+)if(stuj.sum>stuk.sum)k=j;if(k!=i)tempStu=stui;stui=stuk;stuk=tempStu;/*函數(shù)功能:打印名次表,表格內(nèi)包括學(xué)生編號、各科分數(shù)、總分和平均分函數(shù)參數(shù):結(jié)構(gòu)體數(shù)組stu,存放學(xué)生信息整型變量n,存放學(xué)生人數(shù)函數(shù)返回值:無*/voidPrint(Studentstu,intn)inti;printf("NOt|MTtENtPHtSUMtAVERn");printf("n");for(i=0;i<n;i+)printf("%ldt|&quo
16、t;,stui.num);printf("%4dt%4dt%4dt",stui.math,stui.english,stui.physics);printf("%5dt%5.0fn",stui.sum,stui.aver);/*函數(shù)功能:查找學(xué)生的學(xué)號函數(shù)參數(shù):結(jié)構(gòu)體數(shù)組stu,存放學(xué)生信息整型變量n,存放學(xué)生人數(shù)長整型變量x,存放待查找學(xué)生的學(xué)號-1函數(shù)返回值:找到時,返回學(xué)生學(xué)號在數(shù)組中的下標位置,否則返回值*/intSearch(Studentstu,intn,longx)inti;for(i=0;i<n;i+)if(stui.num=x)r
17、eturn(i);return(-1);遞歸程序設(shè)計求游戲人員的年齡1.求游戲人員的年齡#include<stdio.h>intage(intn);intmain()intage5;age5=age(5);printf("第5個人的年齡為%dn",age5);intage(intn)if(n=1)return10;elsereturnage(n-1)+2;求最大公約數(shù)#include<stdio.h>intgcd(intx,inty);intmain()intx,y;intgcdResult;printf("輸入要計算最大公約數(shù)的兩個數(shù):&
18、quot;);scanf("%d%d",&x,&y);gcdResult=gcd(x,y);printf("最大公約數(shù)為%dn",gcdResult);intgcd(intx,inty)if(x=y)returnx;elseif(x>y)returngcd(x-y,y);elsereturngcd(x,y-x);/*x<y*/實驗鏈表編程/注:該程序并沒有出錯控制,假設(shè)用戶輸入都是正常的范圍內(nèi)#include<iostream>usingnamespacestd;structNodeintdata;Node*nex
19、t;;voidcreateList(Node*head,intnum);intfindByNo(Node*head,intnum);intfindByData(Node*head,intdata);voidinsertData(Node*head,intdata,intnum);voiddeleteData(Node*head,intnum);voidprintOut(Node*head);voidmain()/inta6=21,23,25,27,29,31;intnum;intdata;Node*head=newNode();cout<<"請輸入6個結(jié)點的值:"
20、;createList(head,6);cout<<"該鏈表為:"printOut(head);printf("請輸入要查找的結(jié)點的序號(1-6):");cin>>num;data=findByNo(head,num);cout<<"查找至U的結(jié)點的值為"<<data<<endl;printf("請輸入要查找的結(jié)點的值:");cin>>data;num=findByData(head,data);cout<<"查找到的結(jié)點
21、的序號為:"<<num<<endl;cout<<"插入結(jié)點位置:";cin>>num;cout<<"插入結(jié)點的值:";cin>>data;insertData(head,data,num);cout<<"請輸入要刪除的結(jié)點序號cin>>num;deleteData(head,num);intfindByNo(Node*head,intnum)intcount=0;Node*p=head;while(p->next!=NULL)p=p-
22、>next;count+;if(count=num)returnp->data;intfindByData(Node*head,intdata)Node*p=head;intcount=0;while(p->next!=NULL)p=p->next;count+;if(p->data=data)returncount;/輸入序號num和值data。在序號為num的結(jié)點后插入data,并輸出該鏈表voidinsertData(Node*head,intdata,intnum)intcount=0;Node*p=head;while(p->next!=NULL)
23、p=p->next;count+;if(count=num)Node*q=newNode();q->data=data;q->next=p->next;p->next=q;printOut(head);return;/輸入序號num,刪除序號為num的結(jié)點,并輸出該鏈表voiddeleteData(Node*head,intnum)intcount=0;Node*p=head->next;Node*pre=head;while(p!=NULL)count+;if(count=num)/刪除節(jié)點pre->next=p->next;deletep;p
24、rintOut(head);return;elsepre=p;p=p->next;voidprintOut(Node*head)Node*p=head->next;while(p!=NULL)printf("%d",p->data);p=p->next;cout<<endl;voidcreateList(Node*head,intnum)Node*p;Node*q=head;for(inti=0;i<num;i+)p=newNode();cin>>p->data;p->next=NULL;q->next
25、=p;q=q->next;實驗結(jié)構(gòu)、鏈表綜合編程注意:下面的代碼用到了文件,但是學(xué)生編程并不要求使用文件【編寫程序】:建立多個班級學(xué)生成績鏈表,其中,每個結(jié)點包含下面這些信息:學(xué)號、姓名、成績要求完成下面的功能:1 .建立2個班學(xué)生成績的無序鏈表,其中每個班包含10個結(jié)點數(shù)據(jù)(輸入或從文件中讀取每個同學(xué)的信息),將每個班的成績鏈表按成績高低排序后分別輸出該鏈表;2 .將2個班級學(xué)生成績合并(按成績高低排序)后輸出;3.查找學(xué)生成績:通過輸入同學(xué)的學(xué)號,將該同學(xué)從鏈表中找出,并輸出該同學(xué)信息;如果找不到,則輸出“無此學(xué)生”。/classl.txt101a56102b78103c69104d
26、26105e60106f66107g70108h90109i92110j99class2.txt201aa26202bb55203cc78204dd46205ee89206ff65207gg99208hh85209ii73210jj96代碼如下:#include<iostream>usingnamespacestd;#include<fstream>structNodecharno5;charname6;doubleperform;Node*next;voidprintOut(Node*head)Node*p=head->next;while(p!=NULL)co
27、ut<<p->no<<""<<p->name<<""<<p->perform<<endl;p=p->next;cout<<endl;voidmain()Node*q;Node*p;inti;/讀入班級1,構(gòu)成鏈表Node*headClass1=newNode();q=headClass1;ifstreamfin1("class1.txt");if(!fin1)cout<<"文件1打開失??!"<
28、;<endl;return;for(i=0;i<10;i+)p=newNode();fin1>>p->no>>p->name>>p->perform;p->next=NULL;q->next=p;q=q->next;cout<<"班級1原始數(shù)據(jù)為:"<<endl;printOut(headClass1);/讀入班級2,構(gòu)成鏈表Node*headClass2=newNode();q=headClass2;ifstreamfin2("class2.txt&quo
29、t;);if(!fin2)cout<<"文件1打開失?。?quot;<<endl;return;for(i=0;i<10;i+)p=newNode();fin2>>p->no>>p->name>>p->perform;p->next=NULL;q->next=p;q=q->next;cout<<"班級2原始數(shù)據(jù)為:"<<endl;printOut(headClass2);/排序/對班級1高低排序輸出q=headClass1->next
30、;Node*temp=newNode();Node*max;while(q!=NULL)temp->perform=q->perform;max=q;p=q->next;while(p!=NULL)if(p->perform>temp->perform)max=p;temp->perform=p->perform;p=p->next;strcpy(temp->no,q->no);strcpy(temp->name,q->name);temp->perform=q->perform;strcpy(q->
31、;no,max->no);strcpy(q->name,max->name);q->perform=max->perform;strcpy(max->no,temp->no);strcpy(max->name,temp->name);max->perform=temp->perform;q=q->next;deletetemp;temp=NULL;cout<<"班級1排序后數(shù)據(jù)為:"<<endl;printOut(headClass1);/對班級2高低排序輸出q=headClas
32、s2->next;temp=newNode();while(q!=NULL)temp->perform=q->perform;max=q;p=q->next;while(p!=NULL)if(p->perform>temp->perform)max=p;temp->perform=p->perform;p=p->next;strcpy(temp->no,q->no);strcpy(temp->name,q->name);temp->perform=q->perform;strcpy(q->no
33、,max->no);strcpy(q->name,max->name);q->perform=max->perform;strcpy(max->no,temp->no);strcpy(max->name,temp->name);max->perform=temp->perform;q=q->next;deletetemp;temp=NULL;cout<<"班級2排序后數(shù)據(jù)為:"<<endl;printOut(headClass2);/鏈表合并Node*head=newNode()
34、;Node*h=head;p=headClass1->next;q=headClass2->next;while(p!=NULL&&q!=NULL)if(p->perform>q->perform)h->next=p;p=p->next;(h->next)->next=NULL;h=h->next;elseh->next=q;q=q->next;(h->next)->next=NULL;h=h->next;if(p!=NULL)h->next=p;elseif(q!=NULL)h-&
35、gt;next=q;headClass1=NULL;headClass2=NULL;/打印合并鏈表cout<<"合并后的結(jié)果為:"<<endl;printOut(head);/查找學(xué)生此時只有head合并為有學(xué)生的,別的兩個鏈表都是空的了。charno5;cout<<"請輸入要查找的學(xué)生學(xué)號:"cin>>no;h=head;boolisFind=false;while(h->next!=NULL)h=h->next;if(strcmp(h->no,no)=0)cout<<h-&
36、gt;no<<""<<h->name<<""<<h->perform<<endl;isFind=true;break;if(!isFind)cout<<"無此學(xué)生"<<endl;實驗類與對象linkedList.h#ifndefLINKEDLIST_H_#defineLINKEDLIST_H_#include<iostream>usingnamespacestd;structNodeintdata;Node*next;class
37、LinkedListpublic:LinkedList(void)/生成頭結(jié)點,方便后面的操作head=newNode;head->next=NULL;voidreadList(intnum);/num表示在初始構(gòu)造鏈表時,讀入結(jié)點的個數(shù)intfindByNo(intnum);intfindByData(intdata);voidinsertData(intdata,intnum);voiddeleteData(intnum);voidprintOut();private:Node*head;#endiflinkedList.cpp#include"linkedList.h&q
38、uot;intLinkedList:findByNo(intnum)intcount=0;Node*p=head;while(p->next!=NULL)p=p->next;count+;if(count=num)returnp->data;return0;intLinkedList:findByData(intdata)Node*p=head;intcount=0;while(p->next!=NULL)p=p->next;count+;if(p->data=data)returncount;return0;/輸入序號num和值data。在序號為num的結(jié)
39、點后插入data,并輸出該鏈表voidLinkedList:insertData(intdata,intnum)intcount=0;Node*p=head;while(p->next!=NULL)p=p->next;count+;if(count=num)Node*q=newNode();q->data=data;q->next=p->next;p->next=q;printOut();return;/輸入序號num,刪除序號為num的結(jié)點,并輸出該鏈表voidLinkedList:deleteData(intnum)intcount=0;Node*p=h
40、ead->next;Node*pre=head;while(p!=NULL)count+;if(count=num)/刪除節(jié)點pre->next=p->next;deletep;printOut();return;elsepre=p;p=p->next;voidLinkedList:printOut()Node*p=head->next;while(p!=NULL)cout<<p->data;p=p->next;cout<<endl;voidLinkedList:readList(intnum)Node*p;Node*q=hea
41、d;for(inti=0;i<num;i+)p=newNode();cin>>p->data;p->next=NULL;q->next=p;q=q->next;/main.cpp#include<iostream>usingnamespacestd;#include"linkedList.hvoidmain()/inta6=21,23,25,27,29,31;intnum;intdata;LinkedList*pList=newLinkedList();cout<<"請輸入6個結(jié)點的值:"pList
42、->readList(6);cout<<"該鏈表為:"pList->printOut();printf("請輸入要查找的結(jié)點的序號(1-6):");cin>>num;data=pList->findByNo(num);cout<<"查找至U的結(jié)點的值為"<<data<<endl;printf("請輸入要查找的結(jié)點的值:");cin>>data;num=pList->findByData(data);cout<<
43、;"查找到的結(jié)點的序號為:"<<num<<endl;cout<<"插入結(jié)點位置:";cin>>num;cout<<"插入結(jié)點的值:";cin>>data;pList->insertData(data,num);cout<<"請輸入要刪除的結(jié)點序號:";cin>>num;pList->deleteData(num);實驗繼承與多態(tài)/myShape.h#ifndefMYSHAPE_H_#defineMYSHAPE
44、_H_#include<iostream>usingnamespacestd;classshapepublic:virtualvoidarea(void)=0;virtualvoidshowarea(void)=0;protected:doublem_area;classcirclepublicshapepublic:circle(doubler)radius=r;voidarea(void);voidshowarea(void);protected:doubleradius;classrectangle:publicshapepublic:rectangle(doublel=0,
45、doublew=0)length=l;width=w;voidarea(void);voidshowarea(void);protected:doublelength;doublewidth;#endif/myShape.cpp#include"myShape.h"voidcircle:area(void)m_area=3.14*radius*radius;voidrectangle:area(void)m_area=length*width;voidcircle:showarea(void)cout<<"半徑為"<<radius
46、<<"的圓形面積為"<<m_area<<endl;voidrectangle:showarea(void)cout<<"長寬分另1J為"<<width<<','<<length<<"的長方形面積為"<<m_area<<endl;/main.cpp#include"myShape.h"#include<iostream>usingnamespacestd;voidmain
47、()shape*pShape;circlemyCircle(1);rectanglemyRectangle(2,3);pShape=&myCircle;pShape->area();pShape->showarea();pShape=&myRectangle;pShape->area();pShape->showarea();實驗?zāi)0孱惡瓦\算符重載模板類注:下面的程序假設(shè)用戶輸入都是正確的,即沒有考慮用戶輸入錯誤數(shù)據(jù)的處理linkedList.h#ifndefLINKEDLIST_H_#defineLINKEDLIST_H_#include<ios
48、tream>usingnamespacestd;template<classT>structNodeTdata;Node<T>*next;template<classT>classLinkedListpublic:LinkedList(void)/生成頭結(jié)點,方便后面的操作head=newNode<T>();head->next=NULL;voidreadList(intnum);/num表示在初始構(gòu)造鏈表時,讀入結(jié)點的個數(shù)intfindByNo(intnum);intfindByData(Tdata);voidinsertData(
49、Tdata,intnum);voiddeleteData(intnum);voidprintOut();private:Node<T>*head;#endiflinkedList.cpp#include"linkedList.h"template<classT>intLinkedList<T>:findByNo(intnum)intcount=0;Node<T>*p=head;while(p->next!=NULL)p=p->next;count+;if(count=num)returnp->data;ret
50、urn0;template<classT>intLinkedList<T>:findByData(Tdata)Node<T>*p=head;intcount=0;while(p->next!=NULL)p=p->next;count+;if(p->data=data)returncount;return0;/輸入序號num和值data。在序號為num的結(jié)點后插入data,并輸出該鏈表template<classT>voidLinkedList<T>:insertData(Tdata,intnum)intcount=0
51、;Node<T>*p=head;while(p->next!=NULL)p=p->next;count+;if(count=num)Node<T>*q=newNode<T>();q->data=data;q->next=p->next;p->next=q;printOut();return;/輸入序號num,刪除序號為num的結(jié)點,并輸出該鏈表template<classT>voidLinkedList<T>:deleteData(intnum)intcount=0;Node<T>*p=
52、head->next;Node<T>*pre=head;while(p!=NULL)count+;if(count=num)/刪除節(jié)點pre->next=p->next;deletep;printOut();return;elsepre=p;p=p->next;template<classT>voidLinkedList<T>:printOut()Node<T>*p=head->next;while(p!=NULL)cout<<p->data;p=p->next;cout<<end
53、l;template<classT>voidLinkedList<T>:readList(intnum)Node<T>*p;Node<T>*q=head;for(inti=0;i<num;i+)p=newNode<T>();cin>>p->data;p->next=NULL;q->next=p;q=q->next;/main.cpp#include<iostream>usingnamespacestd;#include"linkedList.h"#include
54、"linkedList.cpp"voidmain()/inta6=21,23,25,27,29,31;intnum;chardata;LinkedList<char>*pList=newLinkedList<char>();cout<<"請輸入6個結(jié)點的值:"pList->readList(6);cout<<"該鏈表為:II.pList->printOut();printf("請輸入要查找的結(jié)點的序號(1-6):");cin>>num;data=pLis
55、t->findByNo(num);cout<<"查找至U的結(jié)點的值為"<<data<<endl;printf("請輸入要查找的結(jié)點的值:");cin>>data;num=pList->findByData(data);cout<<"查找到的結(jié)點的序號為:"<<num<<endl;cout<<"插入結(jié)點位置:";cin>>num;cout<<"插入結(jié)點的值:";cin
56、>>data;pList->insertData(data,num);cout<<"請輸入要刪除的結(jié)點序號:";cin>>num;pList->deleteData(num);運算符重載注意:下面的代碼可以在VC6中運行。VC6中有一個小bug。如果像以前一樣usingnamespacestd那么就會出現(xiàn)錯誤,無法重載<<,而如果代碼在VC7(VS.net中運行是完全沒有問題的)下面僅僅給出一種能在VC6中運行的方案。還有另一種解決方法(實驗指導(dǎo)書中的方案),就是將運算符<<的重載的實現(xiàn)也寫在類里面,而
57、不是分開來(即聲明和實現(xiàn)一起寫在Time這個類中)在VC6中,有bug,兩種解決方案方案一:使用std:cout<<,可以將重載的<<定義在類外,方案二:使用usingnamespacestd;但是,重載的<<必須定義為內(nèi)聯(lián)的VS中都可以,沒有問題/mytime.h#ifndefMYTIME_H_#defineMYTIME_H_#include<iostream>classTimeprivate:inthours;intminutes;public:Time();constructorTime(inth,intm=0);constructorvo
58、idAddMin(intm);/時間增加m分鐘voidAddHr(inth);/時間增加h小時voidReset(inth=0,intm=0);/重置時間Timeoperator+(constTime&t)const;/時間求和Timeoperator-(constTime&t)const;/時間求差Timeoperator*(doublemult)const;/乘法friendTimeoperator*(doublem,constTime&t);/乘法friendstd:ostream&operator<<(std:ostream&,con
59、stTime&);/顯示時間;#endif/mytime.cpp#include"mytime.h"Time:Time()hours=minutes=0;Time:Time(inth,intm)hours=h;minutes=m;voidTime:AddMin(intm)minutes+=m;hours+=minutes/60;minutes%=60;voidTime:AddHr(inth)hours+=h;voidTime:Reset(inth,intm)hours=h;minutes=m;TimeTime:operator+(constTime&t)co
60、nstTimesum;sum.minutes=minutes+t.minutes;sum.hours=hours+t.hours+sum.minutes/60;sum.minutes%=60;returnsum;TimeTime:operator-(constTime&t)constTimediff;inttot1,tot2;tot1=t.minutes+60*t.hours;tot2=minutes+60*hours;diff.minutes=(tot2-tot1)%60;diff.hours=(tot2-tot1)/60;returndiff;TimeTime:operator*(
61、doublemult)constTimeresult;longtotalminutes=hours*mult*60+minutes*mult;result.hours=totalminutes/60;result.minutes=totalminutes%60;returnresult;Timeoperator*(doublem,constTime&t)returnt*m;std:ostream&operator<<(std:ostream&os,constTime&t)os<<t.hours<<"hours,"<<t.minutes<<"minutes"<<std:endl;returnos;/main.cpp#include"myTime.h"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)校物資庫管理制度
- 學(xué)校營養(yǎng)餐管理制度
- 學(xué)生休復(fù)學(xué)管理制度
- 學(xué)生請銷假管理制度
- 安保部衛(wèi)生管理制度
- 安全監(jiān)測與管理制度
- 安費諾公司管理制度
- 定制店員工管理制度
- 實訓(xùn)室學(xué)生管理制度
- 審核崗薪酬管理制度
- 16J914-1 公用建筑衛(wèi)生間
- 化工設(shè)備操作工培訓(xùn)資料課件
- petrel RE詳細培訓(xùn)資料
- 等級醫(yī)院評審匯報課件
- 火場偵察和督查的組織與實施
- 2021年中日青年交流中心有限公司二十一世紀飯店校園招聘筆試試題及答案解析
- 食用菌生產(chǎn)技術(shù) 大球蓋菇栽培技術(shù)課件
- 小班語言課《水果歌》PPT
- TSG11-2020 鍋爐安全技術(shù)規(guī)程
- 人才培養(yǎng)方案編寫質(zhì)量標準
- 靜脈輸液-PPT課件
評論
0/150
提交評論