C++程序设计编程实例大全.pdf
《C++程序设计编程实例大全.pdf》由会员分享,可在线阅读,更多相关《C++程序设计编程实例大全.pdf(105页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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 iostreamusi
2、ng 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;cout
3、wang.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;#i
4、nclude 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 choic
5、e 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(
6、)(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
7、,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
8、;/*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,typ
9、e 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
10、.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;in
11、line 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
12、,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,
13、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_Squ
14、are(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*/#includ
15、e 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;
16、)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%
17、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;s
18、omeFunc(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 pos
19、ition;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 s
20、ome 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;i
21、nt 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.v
22、olume(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
23、=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();
24、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
25、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;cla
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 编程 实例 大全
限制150内