C程序設(shè)計(jì)編程實(shí)例大全_第1頁(yè)
C程序設(shè)計(jì)編程實(shí)例大全_第2頁(yè)
C程序設(shè)計(jì)編程實(shí)例大全_第3頁(yè)
C程序設(shè)計(jì)編程實(shí)例大全_第4頁(yè)
C程序設(shè)計(jì)編程實(shí)例大全_第5頁(yè)
已閱讀5頁(yè),還剩40頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、C+序編程實(shí)例大全#includeintmain()std:couthelloworld!;#includeiostreamusingnamespacestd;voidmain()int*p;p=newint;*p=5;/*p=newint(5);*/cout*p;deletep;#includeiostreamusingnamespacestd;voidmain()charname10;coutname;coutthenameisnameendl;#includeiostreamusingnamespacestd;voidmain()charname10;intage;coutname;co

2、utage;coutthenameisnameendl;couttheageisagen;studentwang;wang.no=n;cinwang.math;coutwang.nowang.mathendl;#includeiostreamusingnamespacestd;voidmain()int*p;intn;coutn;if(p=newintn)=0)coutcantallocatemorememory,terminatingendl;exit(1);for(inti=0;in;i+)pi=i*2;coutNowoutputthearray:endl;for(i=0;in;i+)co

3、utpiendl;deletep;#includeiostreamusingnamespacestd;voidmain()(int*p;intn;coutn;if(p=newintn)=0)(coutcantallocatemorememory,terminatingendl;exit(1);for(inti=0;in;i+)pi=i*i+1;coutNowoutputthearray:endl;for(i=0;in;i+)coutpi;coutendl;deletep;/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;floata=2.4;

4、voidmain()(inta=8;coutaendl;cout:aendl;/*Note:YourchoiceisCIDE*/#includevoidmain()(intnum=50;int&ref=num;ref+=10;coutnum=numendl;coutref=refendl;num+=40;coutnum=numendl;coutrefrefendl;/*Note:YourchoiceisCIDE*/#includevoidmain()intnum;int&ref=num;num=5;coutnum=numendl;coutref=refendl;cout&num=&numend

5、l;cout&ref=&refendl;/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;voidswap(int&x,int&y);voidmain()intx=5,y=6;coutbeforeswap,x:x,y:yendl;swap(x,y);coutafterswap,x:x,y:y;voidswap(int&rx,int&ry)intt=rx;rx=ry;ry=t;#include/usingnamespacestd;voidmain()/*doubleradius;coutradius;doubleresult=radius*ra

6、dius*3.14*4;coutTheresultis(double)resultn;*/doubleradius;std:coutradius;std:coutTheresultisradius*radius*3.14*4n;/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;intarray64=60,80,90,75,75,85,65,77,80,88,90,98,89,100,78,81,62,68,69,75,85,85,77,91;int&level(intgrade口,intsize,int&tA,int&tB);voidmain

7、()inttypeA=0,typeB=0;intstudent=6;intgradesize=4;for(inti=0;istudent;i+)level(arrayi,gradesize,typeA,typeB)+;coutnumberoftypeAistypeAendl;coutnumberoftypeBistypeBendl;int&level(intgrade口,intsize,int&tA,int&tB)intsum=0;for(inti=0;i=80)returntA;elsereturntB;/*Note:YourchoiceisCIDE*/#includeusingnamesp

8、acestd;float&fn2(floatr)floatt;t=3.14*r*r;returnt;voidmain()fn2(5.0)=12.4;coutfn2(5.0)endl;/*Note:YourchoiceisCIDE*/#include#includeusingnamespacestd;voidmain()strings,t;coutpleaseinputazifuchuan:s;t=Ilikeprogramming!;coutzifuchuanoutput:endlsendltendl;couts.append(OK!)endl;/*Note:YourchoiceisCIDE*/

9、#includeusingnamespacestd;inlinedoublecircumference(doubleradius);voidmain()doubler=3.0,s;s=circumference(r);coutthecircumferenceiss.endl;inlinedoublecircumference(doubleradius)return2*3.1415926*radius;/*Note:YourchoiceisCIDE*/#includeintadd(intx,inty)intsum;sum=x+y;returnsum;intadd(intx,inty,intz)i

10、ntsum;sum=x+y+z;returnsum;voidmain()inta,b;a=add(5,10);b=add(5,10,20);couta=aendl;coutb=bendl;/*Note:YourchoiceisCIDE*/#includetemplateTmin(Ta口,intn)inti;Tminv=a0;for(i=1;iai)minv=ai;returnminv;voidmain()inta尸130,2,7,6,4,5,2;doubleb尸1.2,-3.4,6.8,9.8;intc=1,9,5,-6,7,8;coutadeshuzuzhongminzhiwei:min(a

11、,9)endl;coutbdeshuzuzhongminzhiwei:min(b,4)endl;coutcdeshuzuzhongminzhiwei:min(c,6)endl;/*Note:YourchoiceisCIDE*/#include#definePI3.1415926535#defineCSCircle_SquaretemplatedoubleCircle_Square(Tx)returnx*x*PI;doubleCircle_Square(longx)returnx*x*PI;voidmain()intr1=1;doubler2=2.0;longr3=3;coutThefirstc

12、sisCS(r1)endl;coutThesecondcsisCS(r2)endl;coutThethirdcsisCS(r3)endl;/*Note:YourchoiceisCIDE*/#includevoidmain()inti=0,&l=i,&k=l;i=+l-k;couti=iendl;/*Note:YourchoiceisCIDE*/#includeclassTdatepublic:voidset(int,int,int);intisLeapYear();voidprint();private:intmonth;intday;intyear;voidTdate:set(intm,in

13、td,inty)month=m;day=d;year=y;intTdate:isLeapYear()return(year%4=0&year%100!=0)|(year%400=0);voidTdate:print()coutmonth/day/yearendl;coutisLeapYear()endl;voidmain()Tdatex;x.set(10,23,2000);x.isLeapYear();x.print();/*Note:YourchoiceisCIDE*/#includeclassTdatepublic:voidset(int,int,int);intisLeapYear();

14、voidprint();private:intmonth;intday;intyear;voidTdate:set(intm,intd,inty)month=m;day=d;year=y;intTdate:isLeapYear()return(year%4=0&year%100!=0)|(year%400=0);voidTdate:print()coutyear/month/dayendl;voidsomeFunc(Tdate&refs)refs.print();if(refs.isLeapYear()couterrorendl;elsecoutrightprint();if(*pTdate)

15、.isLeapYear()couterrorendl;elsecoutrightendl;someFunc(s);#includeusingnamespacestd;voidsphere();intmain()sphere();voidsphere()doubleradius;coutradius;if(radius0)return;couttheresultisradius*radius*3.14*4n;/*Note:YourchoiceisCIDE*/#includeconstintSIZE=10;classCstackprivate:charstkSIZE;intposition;pub

16、lic:voidinit()position=0;charpush(charch);charpop();charCstack:push(charch)if(position=SIZE)cout棧滿endl;return0;stkposition+=ch;returnch;charCstack:pop()if(position=0)coutn??誩ndl;return0;returnstk-position;voidmain()Cstacks;s.init();charch;coutpleaseinputsomecharacters:ch;while(ch!=#&s.push(ch)cinch;

17、coutendl;coutnowoutputthedata:endl;while(ch=s.pop()coutch;/*Note:YourchoiceisCIDE*/#includeclassTdatepublic:voidset(intm=5,intd=16,inty=1991)month=m;day=d;year=y;voidprint()coutmonth/day/yearendl;private:intmonth;intday;intyear;voidmain()Tdatea,b,c;a.set(4,12,1996);b.set(3);c.set(8,10);a.print();b.p

18、rint();c.print();/*Note:YourchoiceisCIDE*/#includeclasscubepublic:intvolume(intht,intwd)returnht*wd;intvolume(intht,intwd,intdp)returnht*wd*dp;private:intht,wd,dp;voidmain()cubec;coutc.volume(10,20)endl;coutc.volume(10,20,30)endl;/*Note:YourchoiceisCIDE*/#includeclassqueue/*默認(rèn)私有*/intq100;intsloc,rlo

19、c;public:queue();voidqput(inti);intqget();queue:queue()sloc=rloc=0;coutqueueinitializedendl;voidqueue:qput(inti)if(sloc=100)coutqueueisfullendl;return;sloc+;qsloc=i;intqueue:qget()if(rloc=sloc)coutqueueisemptyendl;return0;rloc+;returnqrloc;voidmain()queuea,b;a.qput(10);b.qput(20);a.qput(20);b.qput(1

20、9);couta.qget();coutb.qget()n;couta.qget();coutb.qget()n;/*Note:YourchoiceisCIDE*/#includeclasstestprivate:intnum;floatf1;public:test();test(intn,floatf);intgetint()returnnum;floatgetfloat()returnf1;;test:test()coutInitializingdefaultendl;num=0;f1=0.0;test:test(intn,floatf)coutInitializingn,fendl;nu

21、m=n;f1=f;voidmain()testx;testy(10,21.5);test*px=newtest;test*py=newtest(10,21.5);/*Note:YourchoiceisCIDE*/#includeclassTdatepublic:Tdate(intm=5,intd=16,inty=1990)month=m;day=d;year=y;coutmonth/day/yearendl;private:intmonth;intday;intyear;voidmain()Tdateaday;Tdatebday(2);Tdatecday(3,12);Tdatedday(1,2

22、2,1998);/*Note:YourchoiceisCIDE*/#include#includeusingnamespacestd;classStudentpublic:Student(char*pName)coutcalloneparameterconstructorendl;strncpy(name,pName,sizeof(name);namesizeof(name)-1=0;coutthenameisnameendl;Student。coutcallnoparameterconstructorendl;protected:charname20;voidmain()StudentnoN

23、ame;Studentss(Jenny);/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;classtestprivate:intnum;floatf1;public:test();test(intn,floatf);intgetint()returnnum;floatgetfloat()returnf1;;test:test()coutInitializingdefaultendl;num=0;f1=0.0;test:test(intn,floatf)coutInitializingn,fendl;num=n;f1=f;voidmain(

24、)coutthemainfunction:endl;testarray5;ofarrayiscoutthesecondelementarray1.getint(),array1.getfloat()endl;/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;classtestprivate:intnum;floatf1;public:test(intn);test(intn,floatf);inlinetest:test(intn)coutInitializingtnendl;num=n;test:test(intn,floatf)coutI

25、nitializingtn,fendl;num=n;f1=f;voidmain()testarray13=1,2,3;testarray2=test(2,3.5),test(4);testarray3=test(5.5,6.5),test(7,8.5);testarray4尸test(5.5,6.5),7.5,8.5;/*Note:YourchoiceisCIDE*/#includeusingnamespacestd;classtestprivate:intnum;floatf1;public:test(intn);test(intn,floatf);inlinetest:test(intn)

26、coutInitializingtnendl;num=n;test:test(intn,floatf)coutInitializingtn,fendl;num=n;f1=f;voidmain()testarray13=1,2,3;testarray2=test(2,3.5),test(4);testarray3=test(5.5,6.5),test(7,8.5);testarray4尸test(5.5,6.5),7.5,8.5;#includeusingnamespacestd;intmain()for(inti=1;i=10;i+)for(intj=1;j=i;+j)coutM;couten

27、dl;#includeclassTdatepublic:Tdate(intm=5,intd=16,inty=1990)month=m;day=d;year=y;coutmonth/day/yearendl;private:intmonth;intday;intyear;voidmain()Tdateaday;Tdatebday(2);Tdatecday(3,12);Tdatedday(1,21,1998);#include#includeusingnamespacestd;voidmain()tryexceptiontheError;throw(theError);catch(constexc

28、eption&theError)couttheError.what()endl;trylogic_errortheLogicError(LogicError!);throw(theLogicError);catch(constexception&theLogicError)couttheLogicError.what()endl;#include#includeusingnamespacestd;classPersonpublic:Person(char*na)coutcallconstructorendl;name=newcharstrlen(na)+1;if(name!=0)strcpy(

29、name,na);Person(Person&p)coutcallcopyconstructorendl;name=newcharstrlen()+1;if(name!=0)strcpy(name,);voidprintname()coutnameendl;Person()deletename;private:char*name;voidmain()Personwang(wang);Personli(wang);wang.printname();li.printname();#includeusingnamespacestd;classSillyClasspublic:

30、SillyClass(int&i):ten(10),refI(i)protected:constintten;int&refI;voidmain()inti;SillyClasssc(i);coutiendl;#includeclassRMBpublic:RMB(doublevalue=0.0);operatordouble()returnyuan+jf/100.0;voiddisplay。cout(yuan+jf/100.0)endl;protected:unsignedintyuan;unsignedintjf;RMB:RMB(doublevalue)yuan=value;jf=(valu

31、e-yuan)*100+0.5;voidmain()RMBd1(2.0),d2(1.5),d3;d3=RMB(double)d1+(double)d2);d3=d1+d2;d3.display();#include#includeclassStudentpublic:Student(char*pName=noname,intssId=0)strncpy(name,pName,40);name39=0;id=ssId;coutConstructingnewstudentpNameendl;Student(Student&s)coutCendl;str

32、cpy(name,copyof);strcat(name,);id=s.id;Student()coutDestructingnameendl;protected:charname40;intid;voidfn(Students)coutInfunctionfn()nendl;voidmain()Studentrandy(Randy,1234);Studentwang(wang,5678);coutCallingfn()nendl;fn(randy);coutReturnedfromfn()nendl;#include#includeusingnamespacestd;voidma

33、in()tryexceptiontheError;throw(theError);catch(constexception&theError)couttheError.what()endl;trylogic_errortheLogicError(LogicError!);throw(theLogicError);catch(constexception&theLogicError)couttheLogicError.what()endl;#includeclassStudentpublic:Student。coutconstructingstudent.n;semesHours=100;gpa

34、=3.5;Student()coutdestructingstudent.n;protected:intsemesHours;floatgpa;classTeacherpublic:Teacher()coutconstructingteacher.n;Teacher()coutdestructingteacher.n;classTutorpairpublic:Tutorpair()coutconstructingtutorpair.n;nomeeting=0;Tutorpair()coutdestructingtutorpair.n;protected:Studentstudent;Teach

35、erteacher;intnomeeting;;voidmain()Tutorpairtp;coutbackmain.n;#includeclassAstaticinti;public:A()i+;intlist()returni;intA:i=0;voidmain()Aa1,a2,a3;couta1.list(),a2.list(),a3.list();#include#includeclassStudentpublic:Student(char*pName=noname)coutcreateonestudentn;strncpy(name,pName,40);name39=0;noOfSt

36、udents+;coutnoOfStudentsendl;Student()coutdestructonestudentn;noOfStudents-;coutnoOfStudentsendl;staticintnumber。returnnoOfStudents;protected:staticintnoOfStudents;charname40;intStudent:noOfStudents=0;voidfn()Students1;Students2;coutStudent:number()endl;voidmain()Studentwang;fn();coutStudent:number(

37、)endl;#includeusingnamespacestd;intmain()for(inti=1;i=10;+i)for(intj=1;j=i-1;+j)cout;for(intk=1;k=21-2*i;+k)coutM;coutendl;#include#includeclassStudentpublic:Student(char*pName);Student();protected:staticStudent*pFirst;Student*pNext;charname40;Student*Student:pFirst=0;Student:Student(char*pName)strn

38、cpy(name,pName,sizeof(name);namesizeof(name)-1=0;pNext=pFirst;pFirst=this;Student:Student()coutnamepNext)if(pS-pNext=this)pS-pNext=pNext;return;Student*fn()Student*pS=newStudent(Jenny);Studentsb(Jone);returnpS;voidmain()Studentsa(Jamsa);Student*sb=fn();Studentsc(Tracey);deletesb;#include#includeclas

39、sstringstaticinttotal_length;intlength;char*contents;public:string(char*s)length=strlen(s);contents=newcharlength+1;strcpy(contents,s);staticintset_total_length(string&obj)total_length+=obj.length;returntotal_length;string()delete口contents;intstring:total_length=0;voidmain()stringobj1(thefirstobject

40、);coutstring:set_total_length(obj1)endl;stringobj2(thesecondobject);coutstring:set_total_length(obj2)endl;#includeclassApublic:inti,*p;A()i=10;p=&i;;intA:*p=&A:i;voidmain()Aaa,bb;(bb.*p)+;-*aa.p;coutAA:aa.*pBB:bb.*pn;coutAA:*aa.pBB:*bb.pn;#includeclassAinti;public:intset(intk)i=+k;returni;voidmain()

41、int(A:*f)(int尸&A:set;Aaa;cout(aa.*f)(10)endl;#includeclassAstaticinti;friendclassB;public:staticvoidset(intk)i=k;i+;classBpublic:staticvoidds(intl)int*p=&A:i;cout*pn;*p=1;cout*p;intA:i=0;void(*f1)(int尸&A:set;void(*f2)(int尸&B:ds;voidmain()f1(10);f2(20);#includeconstmaxCard=16;enumErrCodenoErr,overflo

42、w;enumBoolFalse,True;classSetintelemsmaxCard;intcard;public:voidEmptySet()card=0;BoolMember(int);ErrCodeAddElem(int);voidRmvElem(int);voidCopy(Set*);BoolEqual(Set*);voidPrint();voidIntersect(Set*,Set*);ErrCodeUnion(Set*,Set*);BoolSet:Member(intelem)for(inti=0;icard;+i)if(elemsi=elem)returnTrue;returnFalse;ErrCodeSet:AddElem(intelem)if(Member(elem)returnnoErr;if(cardmaxCard)elemscard+=elem;returnnoErr;returnoverflow;voidSet:RmvElem(intelem)for(inti=0;icard;+i)if(elemsi=elem)for(;icard-1;+i)elemsi=elemsi+1;-card;return;voidSet:Copy(Set*set)for(inti=0;i

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論