C++程序设计编程实例大全.pdf
C+程序编程实例大全#includeint main()(std:cout,hello world!1;#include iostream,using namespace std;void main()(int*p;p=new int;*p=5;/*p=new int(5);*/cout*p;delete p;#include iostreamusing namespace std;void main()char name 10;coutplease input your name:1;cinname;coutnthe name is nameendl;#include iostreamusing namespace std;void main()char name 10;int age;coutplease input your name:;cinname;coutnhow old are you:;cinage;coutthe name is nameendl;coutnthe age is,ageendl;)#include iostream1using namespace std;void main()(struct student(int no;float math;);int n;cinn;student wang;wang.no=n;cinwang.math;coutwang.nou nwang.mathendl;#include iostream”using namespace std;void main()(int*p;int n;coutnplease input the length of the array:cinn;if(p=new intn)=0)(cout,can,t allocate more memory,terminatingendl;exit(l);)for(int i=0;in;i+)pi=i*2;coutNow output the array:endl;fbr(i=0;in;i+)coutpi,endl;delete p;#include iostream1using namespace std;void main()(int*p;int n;coutHplease input the length of the array:cinn;if(p=new intn)=0)(cout,can,t allocate more memory,terminatingendl;exit(l);)for(int i=0;in;i+)pi=i*i+l;coutNow output the array:nendl;for(i=0;in;i+4-)coutpiM”;coutendl;delete p;/*Note:Your choice is C IDE*/#include using namespace std;float a=2.4;void main()(int a=8;coutaendl;cout:aendl;/*Note:Your choice is C IDE*/#include void main()(int num=50;int&ref=num;ref+=10;cout,num=,num endl;coutrefeHrefendl;num+=40;coutnum=numendl;cout,ref,refendl;/*Note:Your choice is C IDE*/#inciude void main()(int num;int&ref=num;num=5;cout,num=,num endl;coutnref=nrefendl;cout,&num=,&num endl;cout,&ref=,&refendl;/*Note:Your choice is C IDE*/#include using namespace std;void swap(int&x,int&y);void main()(int x=5,y=6;coutbefore swap,x:n x n,y:yendl;swap(x,y);coutnafter swap,x:n x n,y:M y;)void swap(int&rx,int&ry)(int t=rx;rx=ry;ry=t;#include/using namespace std;void main()/*double radius;coutplease input radius:;cinradius;double result=radius*radius*3.14*4;coutnThe result is,(double)result,n,;)*/(double radius;std:coutplease input radius:;std:cinradius;std:coutnThe result is vvradius*radius*3.14*4vn;/*Note:Your choice is C IDE*/#include using namespace std;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(int grade,int size,int&tA,int&tB);void main()(int typeA=0,typeB=0;int student=6;int gradesize=4;fbr(int i=O;istudent;i+)level(arrayi,gradesize,type A,typeB)+;coutnnumber of type A is typeAendl;coutnumber of type B is typeBendl;)int&level(int grade,int size,int&tA,int&tB)(int sum=0;for(int i=0;i=80)return tA;elsereturn tB;/*Note:Your choice is C IDE*/#include using namespace std;float&fn2(float r)(float t;t=3.14*r*r;return t;)void main()(fn2(5.0)=12.4;coutfn2(5.0)endl;/*Note:Your choice is C IDE*/#include#include using namespace std;void main()(string s,t;coutnplease input a zifuchuan:Hendl;cins;t=nI like programming!;coutzifuchuan output:endlsendltendl;couts.append(n OK!n)endl;/*Note:Your choice is C IDE*/#include using namespace std;inline double circumference(double radius);void main()(double r=3.O,s;s=circum ference(r);coutnthe circumference is,s ,.Mendl;)inline double circumference(double radius)!return 2*3.1415926*radius;/*Note:Your choice is C IDE*/#include int add(int x,int y)(int sum;sum=x+y;return sum;)int add(int x,int y,int z)(int sum;sum=x+y+z;return sum;)void main()(int a,b;a=add(5,10);b=add(5,10,20);coutna=M a e ndl;coutnb=H b en d l;/*Note:Your choice is C IDE*/#include templateT min(T a,int n)(inti;T minv=a0;for(i=l;iai)minv=ai;return minv;)void main()(int a=130,2,7,6,4,5,2;double b=1.2,-3.4,6.8,9.8;int c口=1,9,5,7,8;coutna de shuzu zhong min zhi wei:,min(a,9)endl;coutnb de shuzu zhong min zhi wei:nmin(b,4)endl;coutc de shuzu zhong min zhi wei:min(c,6)endl;/*Note:Your choice is C IDE*/#include#define PI 3.1415926535#define CS Circle_Squaretemplatedouble Circle_Square(T x)(return x*x*PI;)double Circle_Square(long x)(return x*x*PI;)void main()(int rl=l;double r2=2.0;long r3=3;coutnThe first cs is MC S(rl)endl;coutnThe second cs is CS(r2)endl;coutnThe third cs is nCS(r3)endl;/*Note:Your choice is C IDE*/#include void main()(int i=0,&l=i,&k=l;i=+l-k;cout,i=,i en d l;/*Note:Your choice is C IDE*/#include class Tdate(public:void set(int,int,int);int isLeapYear();void print();private:int month;int day;int year;void Tdate:set(int m,int d,int y)month=m;day=d;year=y;)int Tdate:isLeapYear()(return(year%4=0&year%100!=0)ll(year%400=0);)void Tdate:print()(coutm onth,/,d a y,/,yearendl;coutisLeapYear()endl;)void main()(Tdate x;x.set(10,23,2000);x.isLeapYear();x.print();/*Note:Your choice is C IDE*/#include class Tdate(public:void set(intjnt,int);int isLeapYear();void print();private:int month;int day;int year;);void Tdate:set(int m,int d,int y)(month=m;day=d;year=y;)int Tdate:isLeapYear()(return(year%4=0&year%100!=0)ll(year%400=0);)voidTdate:print()(coutyear,/m onth,/dayendl;)void someFunc(Tdate&refs)(refs.print();if(refs.isLeapYear()cout MenorH e ndl;elsecoutnrightnendl;)void main()(Tdate s,*pTdate=&s;s.set(2,15,1998);pTdate-print();if(*pTdate).isLeapYear()cout,error,endl;elsecoutnri ghte ndl;someFunc(s);#includeusing namespace std;void sphere();int main()(sphere();)void sphere()(double radius;coutplease input radius:1,;cinradius;if(radius0)return;coutthe result is vvradius*radius*3.14*4vv”n”;/*Note:Your choice is C IDE*/#includeconst int S1ZE=1O;class C stack(private:char stkSIZE;int position;public:void init()position=0;)char push(char ch);char pop(););char Cstack:push(char ch)(if(position=SIZE)(cout“栈满”vendl;return 0;)stkposition+=ch;return ch;)char Cstack:pop()if(position=0)(coutMn 栈空 vvendl;return 0;)return stk position;)void main()(Cstack s;s.init();char ch;coutnplease input some characters:Hendl;cinch;while(ch!=,#,&s.push(ch)cinch;coutendl;coutHnow output the data:endl;while(ch=s.pop()coutch;/*Note:Your choice is C IDE*/#inciude class Tdate(public:void set(int m=5,int d=16,int y=1991)(month=m;d ay=d;ye ar=y;)void print()(coutm onth7,d a y 7,yearendl;)private:int month;int day;int year;);void main()Tdate a,b,c;a.set(4,12,1996);b.set(3);c.set(8,10);a.print();b.print();c.pnnt();*Note:Your choice is C IDE*/#includeclass cube(public:int volume(int ht,int wd)(return ht*wd;)int volume(int ht,int wd,int dp)return ht*wd*dp;)private:int ht,wd,dp;);void main()(cube c;coutc.volume(10,20)endl;coutc.volume(10,20,30)endl;/*Note:Your choice is C IDE*/#include class queue/*默认私有*/int q100;int sloc,rloc;public:queue();void qput(int i);int qget(););queue:queue()(sloc=rloc=0;coutMqueue initializedendl;void queue:qput(int i)(if(sloc=100)(coutnqueue is fullendl;return;)sloc+;qsloc=i;)int queue:qget()(if(rloc=sloc)(coutnqueue is emptyendl;return 0;)rloc+;return qrloc;)void main()(queue a,b;a.qput(lO);b.qput(20);a.qput(20);b.qput(19);couta.qget()n;coutb.qget(),n;couta.qget()ncoutb.qget()un;/*Note:Your choice is C IDE*/#include class test(private:int num;float fl;public:test();test(int n,float f);int getint()return num;float getfloat()(return fl;);test:test()(coutnInitializing defaultuendl;num=0;fl=0.0;)test:test(int n,float f)(cout MI niti alizi n g n ,fendl;num=n;fl=f;)void main()(test x;test y(10,21.5);test*px=new test;test*py=new test(10,21.5);/*Note:Your choice is C IDE*/#include class Tdate(public:Tdate(int m=5,int d=16,int y=1990)(month=m;day=d;year=y;coutm onth,/,d ay 7,yearendl;)private:int month;int day;int year;);void main()Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday(1,22,1998);/*Note:Your choice is C IDE*/#include#include using namespace std;class Student(public:Student(char*pName)coutncall one parameter constructorMendl;stmcpy(name,pName,sizeof(name);namesizeof(name)-lcoutnthe name is Hnam eendl;)Student()(coutncall no parameter constructorHendl;)protected:char name20;);void main()(Student noName;Student ss(Jenny*);/*Note:Your choice is C IDE*/#includeusing namespace std;class test(private:int num;float fl;public:test();test(int n,float f);int getint()return num;float getfloat()(return fl;);test:test()(coutMInitializing defaultnendl;num=0;fl=0.0;)test:test(int n,float f)(cout,Initializing,n ,n f en d l;num=n;fl=f;)void main()(coutMthe main function:*endl;test array5;coutnthe second element of array is arrayl.getint()narray 1 .getfloat()endl;/*Note:Your choice is C IDE*/#includeusing namespace std;class test(private:int num;float fl;public:test(int n);test(int n,float f););inline test:test(int n)(cout,Initializingt,n en d l;num=n;)test:test(int n,float f)cout,Initializingt,n ,,f en d l;num=n;fl=f;void main()(test arrayl3=1,2,3;test array2=test(2,3.5),test(4);test array3=test(5.5,6.5),test(7,8.5);test array4=test(5.5,6.5),7.5,8.5;/*Note:Your choice is C IDE*/#includeusing namespace std;class test(private:int num;float fl;public:test(int n);test(int n,float f););inline test:test(int n)(cout,Initializingt,n en d l;num=n;)test:test(int n,float f)(cout,Initializingt,n M,f en d l;num=n;fl=f;)void main()(test array 1 3=1,2,3;test array2=test(2,3.5),test(4);test array3=test(5.5,6.5),test(7,8.5);test array4=test(5.5,6.5),7.5,8.5);#includeusing namespace std;int main()for(int i=l;i=10;i+)(for(intj=l;j=i;+j)coutnMM;coutendl;)#includeclass Tdatepublic:Tdate(int m=5,int d=16,int y=1990)(month=m;day=d;year=y;coutm onth,7n d a y 7,yearendl;)private:int month;int day;int year;);void main()(Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday(1,21,1998);#include#includeusing namespace std;void main()(try(exception theError;throw(theError);)catch(const exception&theError)(couttheError.what()endl;)trylogic_error theLogicError(Logic Error!1);throw(theLogicError);)catch(const exception&theLogicError)(couttheLogicError.what()endl;)#include#includeusing namespace std;class Person(public:Person(char*na)(coutcall constructorendl;name=new charstrlen(na)+l;if(name!=0)strcpy(name,na);)Person(Person&p)coutMcall copy constructorendl;name=new charstrlen(p.name)+1 ;if(name!=0)strcpy(name,p.name);)void printname()(coutnameendl;-Person()delete name;private:char*name;);void main()(Person wang(wangn);Person li(wang);wang.printname();li.printname();)#includeusing namespace std;class SillyClass(public:SillyClass(int&i):ten(l O),refl(i)()protected:const int ten;int&refl;);void main()(inti;SillyClass sc(i);coutiendl;#includeclass RMB(public:RMB(double value=0.0);operator double()return yuan+jf/100.0;void display()cout(y u an 17100.0)e ndl;)protected:unsigned int yuan;unsigned int jf;);RMB:RMB(double value)(yuan=value;jf=(value-yuan)*100+0.5;)void main()(RMB dl(2.0),d2(1.5),d3;d3=RMB(double)dl+(double)d2);d3=dl+d2;d3.display。;#include#includeclass Student(public:Student(char*pName=no namen,int ssId=O)(strncpy(name,pName,40);name|39=0,;id=ssld;coutConstructing new studentnpNam eendl;)Student(Student&s)(coutConstructing copy of ns.nam eendl;strcpy(name,“copy of);strcat(name,s.name);id=s.id;)Student。(coutDestructing nameendl;)protected:char name40;int id;);void fn(Student s)(coutnIn function fn()nnendl;)void main()(Student randy(Randy*,1234);Student wang(nwang5678);coutCalling fn()nnendl;fn(randy);coutnRetumed from fn()nendl;#incl ude#includeusing namespace std;void main()try(exception theError;throw(theError);)catch(const exception&theError)(couttheError.what()endl;try(logic_error the LogicError(Logic Error!);throw(theLogicError);)catch(const exception&theLogicError)(couttheLogicError.what()endl;)#includeclass Student(public:Student()(cout cons tructing student.n;semesHours=100;gpa=3.5;)Student。coutdestructing studentAn;protected:int semesHours;float gpa;);class Teacher(public:Teacher()couKconstructing teacherAn1;Teacher。coutdestructing te ache r.nu;);class Tutorpairpublic:Tutorpair()cout Mcons tructi ng tutorpair.nn;nomeeting=0;-Tutorpair()(cout destructing tutorpairAn;)protected:Student student;Teacher teacher;int nomeeting;);void main()(Tutorpair tp;coutback mainAn;#includeclass A(static int i;public:A()(i+;)int list()return i;);int A:i=0;void main()(A al,a2,a3;co u t al.list()n,na2.1ist(),na3.1ist();#include#includeclass Student(public:Student(char*pName=no name)(coutMcreate one studentnH;strncpy(name,pName,40);name39=,0,;noOfStudents+;coutnoOfStudentsendl;)Student。coutdestruct one studentnu;noOfStudents;coutnoOfStudentsendl;)static int numberQreturn noOfStudents;)protected:static int noOfStudents;char name40;);int Student:noOfStudents=0;void fn()(Student si;Student s2;coutStudent:number()endl;)void main()(Student wang;fn();coutStudent:number()endl;#includeusing namespace std;int main()(fbr(int i=l;i=10;+i)(for(int j=l;j=i-1 ;+4j)coutM n;for(int k=1;k=21 -2*i;+k)coutnM;coutendl;#include#includeclass Student(public:Student(char*pName);Student。;protected:static Student*pFirst;Student*pNext;char name40;);Student*Student:pFirst=0;Student:Student(chai*pName)(strncpy(name,pName,sizeof(name);namesizeof(name)-l=0;pNext=pFirst;pFirst=this;)Student:Student。(coutthis-nameendl;if(pFirst=this)(pFirst=pNext;return;)for(Student*pS=pFirst;pS;pS=pS-pNext)if(pS-pNe xt=this)(pS-pNext=pNext;return;)Student*fn()Student*pS=new Student(Jennyn);Student sb(Jone);return pS;void main()(Student sa(”Jamsa”);Student*sb=fn();Student sc(Tracey);delete sb;#include#includeclass string(static int totaljength;int length;char*contents;public:string(char*s)(length=strlen(s);contents=new charlength+1;strcpy(contents,s);)static int set_total_length(string&obj)(total_length+=obj.length;return total_length;)string。deletecontents;int string:total_length=0;void main()(string obj 1(the first object1);coutstring:set_total_length(obj l)endl;string obj2(the second object);coutstring:set_total_length(obj2)endl;#includeclass A(public:int i,*p;A()i=10;p=&i;int A:*p=&A:i;void main()(A aa,bb;(bb.*p)+;-*aa.p;coutAA:,a a.*p,BB:,b b.*p,n;cout“AA:*aa.pBB:v*bb.p“n”;#includeclass A(int i;public:int set(int k)(i=+k;retum i;);void main()(int(A:*f)(int)=&A:set;Aaa;cout(aa.*f)(l 0)endl;#includeclass A(static int i;friend class B;public:static void set(int k)i=k;i+;);class B(public:static void ds(int 1)(int*p=&A:i;co u t*p nnH;*p=l;cout*p;int A:i=O;void(*fl)(int)=&A:set;void(*f2)(i nt)=&B:ds;void main()(fl(10);f2(20);#includeconst maxCard=16;enum ErrCode noErr,overflow;enum Bool False,True);class Set(int elems maxCard;int card;public:void EmptySet()card=0;Bool Member(int);ErrCode AddElem(int);void RmvElem(int);void Copy(Set*);Bool Equal(Set*);void Print();void Intersect(Set*,Set*);ErrCode Union(Set*,Set*););Bool Set:Member(int elem)(fbr(int i=0;icard;+i)if(elemsi=elem)return True;return False;)ErrCode Set:AddElem(int elem)(if(Member(elem)return noErr;if(cardmaxCard)(elemscard+=elem;return noErr;return overflow;void Set:RmvElem(int elem)(for(int i=0;icard;+i)if(elemsi=elem)(fbr(;icard-l;+i)elemsi=elemsi+1;-card;return;)void Set:Copy(Set*set)(fbr(int i=0;ielemsi=elemsi;set-card=card;)Bool Set:Equal(Set*set)(if(card!=set-card)return False;for(int i=0;iMember(elemsi)return False;return True;)void Set:Print()(cout,u;fbr(int i=0;i*res(res-card=0;fbr(int i=0;icard;+i)for(int j=O;j card;+j)if(elemsi|=set-elemsj|)|res-elemsres-card+=elemsi;break;ErrCode Set:Union(Set*set,Set*res)(set-Copy(res);fbr(int i=0;iA