C++程序设计编程实例大全.docx
C+程序编程实例大全#include<iostream> int main()(std:cout«"hello world!u;#include "iostream”using namespace std;void main()(int *p;p=new int;*p=5;/*p=new int(5);*/cout«*p;delete p;#include "iostream" using namespace std; void main() char name10;cout«"please input your name:"cin»name;cout«"the name is "«name«endl;#include "iostream"using namespace std;void main() char name10;int age;cout«"please input your name:"cin»name;cout«"how old are you:"cin»age;cout«"the name is "«name«endl;cout«"the age is "«age«endl;#include "iostream"using namespace std;void main()(struct student(int no;float math;1;int n;cin»n;student wang;wang.no=n;cin»wang.math;cout«wang.no«" "«wang.math«endl;#include "iostream"using namespace std;void main()int *p;int n;cout«"please input the length of the array:"cin»n;if(p=new intn)=0)(cout«"can,t allocate more memory, terminating"«endl; exit(l);)for(int i=0;i<n;i+)p|i=i*2;cout«"Now output the array: "«endl;fbr(i=0;i<n;i+) cout«pi«" "«endl; delete p;)#include "iostream"using namespace std;void main()(int *p;int n;cout«"please input the length of the array:"cin»n;if(p=new intn)=0)(cout«"canft allocate more memory, terminating"«endl; exit(l);)for(int i=0;i<n;i+)p|i=i*i+l;cout«"Now output the array: "«endl;ft)r(i=0;i<n;i+4-)cout«pi«"cout«endl;delete p;/* Note: Your choice is C IDE */#include <iostream>using namespace std;float a=2.4;void main()(int a=8;cout«a«endl;cout«:a«endl;/* Note:Your choice is C IDE */#include <iostream.h>void main()(int num=50;int &ref=num;ref+=10;cout«"num="«num«endl;cout«"refi="«ref«endl;num+=40;cout«"num="«num«endl; cou t«"re f,«ref«endl;/* Note:Your choice is C IDE */#include <iostream.h>void main()(int num;int &ref=num;num=5;cout«"num=M«nu m«e ndl;cout«',refi=,«ref«endl;cout«',&num=,'«&num«endl;cout«,'&ref="«&ref<<endl;/* Note:Your choice is C IDE */#include <iostream> using namespace std; void swap(int &x,int &y);void main()(int x=5,y=6;cout«"before swap, x:"«x«n ,y:"«y«endl;swap(x,y);cout«"after swap, x:n«x«" ,y:M«y;)void swap(int &rx,int &ry)(int t=rx;rx=ry;ry=t;#include<iostream>/using namespace std;void main()/*double radius;cout«"please input radius:1'cin»radius;double result=radius*radius*3.14*4;cout«"The result is "«(double)result«Hn")*/(double radius;std:cout«nplease input radius:" std:cin»radius;std:cout«nThe result is ,«radius*radius*3.14*4«,'n,'/* Note:Your choice is C IDE */#include <iostream>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,7 7,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;i<student;i+)level(arrayi ,gradesize,type A,typeB)-H-;cout«nnumber of type A is M«typeA«endl;cout«nnumber of type B is n«typeB«endl;)int &level(int grade,int size,int &tA,int &tB)(int sum=0;for(int i=0;i<size;i+) sum+=gradei;sum/=size;if(sum>=80)return tA;elsereturn tB;/* Note:Your choice is C IDE */#include <iostream>using namespace std;float &fn2( float r)(float t;t=3.14*r*r;return t;)void main()(fn2(5.0)=12.4;cout«fn2( 5.0)«e ndl;/* Note:Your choice is C IDE */#include <iostream>#include <string>using namespace std;void main()(string s,t;cout«"please input a zifuchuan:H«endl;cin»s;t="I like programming!"cout«"zifuchuan output: "<<endl«s«endl«t«endl;cout«s.append(" OK!n)«endl;/* Note:Your choice is C IDE */#include <iostream>using namespace std;inline double circumference(double radius);void main()(double r=3.0,s;s=circumference(r);cout«"the circumference is "<<s«"."«endl;inline double circumference(double radius)(return 2*3.1415926*radius;/* Note:Your choice is C IDE */#include <iostream.h>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);cout«Ha= M«a«e ndl;cout«,'b=*'«b«e ndl;/* Note:Your choice is C IDE */#include <iostream.h>tempi ate <cl ass T>T min(T a,int n)(inti;T minv=a0;fbr(i=l;ivn;i+) if(minv>ai) minv=ai;return minv;)void main()(int a= 1,3,027,6,4,5,2;double b= 1.2,-3.4,6.8,9.8);int c=l,9,5,-6,7,8;cout«na de shuzu zhong min zhi wei: n«min(a,9)«endl;cout«nb de shuzu zhong min zhi wei:M«min(b,4)«endl;cout«nc de shuzu zhong min zhi wei:n«min(c,6)«endl;/* Note:Your choice is C IDE */#include <iostream.h>#define PI 3.1415926535#define CS CircIe_Squaretempi ate<cl ass T>double 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;cout«"The first cs is M«CS(rl)«endl;cout«"The second cs is H«CS(r2)«endl;cout«nThe third cs is M«CS(r3)«endl;/* Note:Your choice is C IDE */#include <iostream.h>void main()(int i=0,&l=i,&k=l;i=+l-k;cout«ui=M«i«endl;/* Note: Your choice is C IDE */#include <iostream.h>class Tdatepublic: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: :isLeap Ye ar()(return(year%4=0&&year%100!=0)ll(year%400=0);)void Tdate :print()(cout«month«,/,«day«V,'«year«endl;cout«isLeapYear()«endl;)void main()(Tdate x;x.set( 10,23,2000);x.isLeapYear();x.print();/* Note:Your choice is C IDE */#include <iostream.h>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: :isLeap Ye ar() (return(year%4=0&&year%100!=0)ll(year%400=0);)voidTdate:print()(cout«year«"/,'«month«,/,«day«endl;void someFunc(Tdate& refs)(refs.print();if(refs.isLe 叩 Year() cout«nerrorn«endl; elsecout«',right,«endl;1void main()(Tdate s,*pTdate=&s;s.set(2,15,1998);pTdate->print();if(*pTdate).isLeapYear()cout«nerrorn«endl;elsecout«nri ght*'«e ndl;someFunc(s);#include<iostream>using namespace std;void sphere();int main()(sphere();void sphere()(double radius;cout«nplease input radius:M;cin»radius;if(radius<0) return;cout«Hthe result is ,«radius*radius*3.14*4«,'n,'/* Note: Your choice is C IDE */#include<iostream.h>const int SIZE=10;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)(coutcv"栈满 vendl: return 0;)stkposition+=ch;return ch;char Cstack:pop()if(position=0) coutv、n 栈空”<vendl;return 0;)return stkposition;1void main()(Cstack s;s.init();char ch;cout«,please input some characters:M«endl;cin»ch;while(ch!=T&&s.push(ch)cin»ch;cout«endl;cout«Mnow output the data: M«endl;while(ch=s.pop()cout«ch;/* Note:Your choice is C IDE */#include <iostream.h>class Tdate(public:void set(int m=5,int d=16,int y=1991)month二m;day 二d;yea匸 y;void print()(cout«month«'7"«day«"/M«year«endl;)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.print();/* Note: Your choice is C IDE */#include<iostream.h>class 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;cout v vc. vol u me( 10,20) vvendl;cout«c.volume( 10,20,30)«endl;/* Note:Your choice is C IDE */#include <iostream.h>class queue/默认私有/int q100;int sloc,rloc;public:queue();void qput(int i);int qget(););queue:queue() (sloc=rloc=0;cout«Mqueue initialized“v<endl;void queue:qput(int i)(if(sloc=100)|cout«nqueue is fulln«endl;return;)sloc+;qsloc=i;int queue:qget()(if(rloc=sloc)(cout«"queue is empty"«endl; return 0;)rloc+;return qrloc;void main()(queue a,b;a.qput(lO);b.qput(20);a.qput(20);b.qput( 19);cout«a.qget()«n M;cout«b.qget()«',n,'cout«a.qget()«H M;cout«b.qget()«nn"/* Note: Your choice is C IDE */#include <iostream.h>class test(private:int num;float fl;public:test();test(int n,float f);int getint()return num;float getfloat()return fl;);test:test()(cout«uInitializing default,'«endl;num=0;fl =0.0;test:test(int n,float f)(cout«',Initializing,'«n«,M«f«endl;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 <iostream.h>class Tdate(public:Tdate(int m=5,int d=16,int y=1990)(month=m;day=d;year=y;cout«month«,'/,'«day«'7',«year«endl;)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 <iostream>#include <string> using namespace std;class Student(public:Student(char *pName) (cout«"call one parameter constructor"vvendl;stmcpy(name,pName,sizeof(name);namesizeof(name)-1 =*0*;cout«nthe name is M«name«endl;Student()(cout«"call no parameter constructorM«endl; protected: char name20;);void main()Student noName;Student ss( "Jenny”); /* Note:Your choice is C IDE */#include<iostream> using namespace std;class testprivate:int num;float fl;public:test();test(int n,float f);int getint()return num;)float getfloat() return fl;);test:test() (cout«MInitializing default"«endl;num=0;fl=0.0;)test:test(int n,float f)(cout«',Initializing,'«n«,'/,«f«endl;num=n;fl=f;)void main()cout«uthe main function: "vvendl;test array5 ;cout«"the second element of array is ,«arrayl.getint()«,«arrayl.getfloat()«endl;/* Note: Your choice is C IDE */#include<iostream>using 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«endl; num=n;)test:test(int n,float f)cout«"Initializingt"«n«","«f«endl;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 */#include<iostream> using 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«endl; num=n;)test:test(int n,float f)(cout«"Initializingt',«n«,"«f«endl;num=n;fl=f;void main()(test arrayl 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);#include<iostream> using namespace std; int main()fbr(int i=l;i<=10;i+)(for(intj=l;j<=i;+j) cout«,Mn;cout«endl;)#include<iostream.h>class TdateJpublic:Tdate(int m=5,int d=16,int y=1990) (month=m;day=d;year=y;cout«month«'7n«day«'7n«year«endl;private:int month;int day;int year;);void main()(Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday( 1,21,1998);#include<exception>#include<iostream>using namespace std;void main()(tryexception theError;throw(theError);catch(const exception &theError)(cout«theError.what()«endI;)trylogic_error theLogicError("Logic Error!"); throw(theLogicError);)catch(const exception & theLogicError)cout«theLogicError.what()«endl;#include<iostream>#include<string> using namespace std;class Person (public:Person(char *na) cout«"call constructor"«endl; name=new charstrlen(na)+l;if(name!=0)strcpy(name,na);)Person(Person&p) (cout«"call copy constructor"«endl;name=new charstrlen(p.name)+1 ;if(name!=0)strcpy(name,p.name);)void printname() cout«name«endl;-Person()delete name;private:char *name;;void main() (Person wang("wang");Person li(wang);wang.printname();li.printname();#include<iostream>using namespace std;class SillyClass (public:SillyClass(int&i):ten( 10),refl(i)protected:const int ten;int&refl;);void main()(int i;SillyClass sc(i);cout«i«endl;#include<iostream.h>class RMB(public:RMB(double value=0.0);operator double()return yuan+j 17100.0;void display()cout«(yuan+jf7100.0)«e ndl;) protected:unsigned int yuan;unsigned int jf;);RMB:RMB(double value)(yuan=value;jf=(value-yuan)* 100+0.5;)void main()(RMBdl(2.0),d2(1.5),d3;d3=RMB(double)dl+(double)d2);d3=dl+d2;d3.display();)#include<iostream.h>#include<string.h> class Student ( public:Student(char *pName="no nameM,int ssId=O) stm cpy (n ame, pN ame ,40);name39=,0,;id=ssld;cout«"Constructing new student"«pName«endl; )Student(Student&s) cout«"Constructing copy of "«s.name«endl;strcpy( name, "copy of");strcat(name,s.name);id=s.id;Student。cout«"Destructing "«name«endl;protected:char name40;int id;);void fn(Student s) (cout«"In function fn()n"«endl;)void main。(Student randy("Randy", 1234);Student wang("wang",5678);cout«"Calling fn()n"«endl;fn( randy);cout«"Retumed from fn()n"«endl;#include<exception> #include<iostream> using namespace std; void main。try(exception theError;throw(theError);)catch(const exception &theError)(cout«theError.what()«endl;try(logic_error the LogicError( "Logic Error!"); throw(theLogicError);)catch(const exception & theLogicError)(cout«theLogi cError. what()«endl;#include<iostream.h> class Student public:Student() cout«"constructing studentAn" semesHours=100;gpa=3.5;Student。cout«"destructing student.Xn”; protected:int semesHours;float gpa;);class Teacher(public:Teacher。cout«"constructing teacherAn"-Teacher。cout«"destructing teacher An" ;class Tutorpairpublic:Tutorpair() (cout«Mconstructi ng tutorpai r.nM; nomeeting=0;)TutorpairQ (cout«ndest ructing tutorpairAn”; protected:Student student;Teacher teacher; int nomeeting;;void main() (Tutorpair tp;cout«nback mainAn"#include<iostream.h>class A ( static int i;public:A()( i+;)int list()return i;);int A:i=0;void main()(A ai,a2,a3;