C++精彩资料程序代码大全.doc





《C++精彩资料程序代码大全.doc》由会员分享,可在线阅读,更多相关《C++精彩资料程序代码大全.doc(134页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 /根据半径计算圆的周长和面积 #include const float PI=3.1416; /声明常量(只读变量)PI 为 3.1416 float fCir_L(float); /声明自定义函数 fCir_L()的原型 float fCir_S(float); /声明自定义函数 fCir_S()的原型 /以下是 main()函数 main() float r,l,s; /声明 3 个变量coutr; /键盘输入l=fCir_L(r); /计算圆的周长,赋值给变量 l s=fCir_S(r); /计算圆的面积,赋值给变量 s cout=0.0) /如果参数大于 0,则计算圆的周长z=2*P
2、I*x;return(z); /返回函数值 /定义计算圆的面积的函数 fCir_S() float fCir_S(float x) float z=-1.0; /声明局部变量if (x=0.0) /如果参数大于 0,则计算圆的面积z=PI*x*x;return(z); /返回函数值 /* Program: P1-2.CPPWritten by: HapDate written: 02:11:10 */#include void main(void) double s1,s2,s3;s1=1.5; /* 对变量 s1 赋值*/cout main() double r=1.0;coutr; /键盘
3、输入l=2*3.1416*r; /计算圆的周长,赋值给变量 lcout /包含 iostream.h 头文件 void main() /输出字符常量、变量和字符串char c1=A;cout /包含 iostream.h 头文件 main() /输入输出字符char c;cinc;coutn;coutx;coutn;coutcnx;cout /包含 iostream.h 头文件 main() /声明整型变量int a,b; /从键盘上为整型变量赋值couta;coutb;/整型数的算术运算cout /包含 iostream.h 头文件 main() /声明变量,并初始化int a=010,b=
4、10,c=0X10; /以十进制形式显示数据couta;coutb;coutc;cout /包含 iostream.h 头文件 #include / iomanip.h 头文件包含 setprecision()的定义 main() /float 型变量的声明、输入、计算和输出float fx,fy; coutfx;coutfy;coutdx;coutdy;cout /包含 iostream.h 头文件 main() /字符类型变量的声明char c1=A;char c2;/字符数据的运算及输出c2=c1+32;coutc1c2;cout /包含 iostream.h 头文件 main() ch
5、ar c1=a,TAB=t; /阵铃一声cout /包含 iostream.h 头文件 main() /声明 bool 变量,并初始化bool flag1=false,flag2=true; /输出布尔常量和变量cout0; /存放关系运算结果cout const double PI=3.1416; /声明常量(const 变量)PI 为 3.1416 main() /声明 3 个变量double r,l,s; /输入圆的半径coutr; /计算圆的周长l=2*PI*r; cout main() /定义枚举类型,并指定其枚举元素的值enum color RED=3,YELLOW=6,BLUE=
6、9;/声明枚举变量 a 和 b,并为枚举变量 a 赋初值 enum color a=RED;color b; /合法,与 C 语言不同/ 输出枚举常量 cout const double PI=3.1416; /声明常量(const 变量)PI 为 3.1416 main() /声明 3 个变量double r=3,l,s; /计算圆的周长l=2*PI*r; cout main() /变量声明char c;double x,y;/测试自增 cout main() int a=3, b=2;/输出关系表达式coutb)=b) main() float a=3.5,b=2.1,c=0;cout=0
7、 /显示 a,b,c 的值cout main() /用 sizeof 计算各类种常量的字节长度cout main() /声明变量语句中使用顺序运算int x, y;/计算中使用顺序运算x=50; y=(x=x-5, x/5); cout main() /测试表达式类型的转换int n=100,m;double x=3.791,y;cout main() float a,b,s;coutab; /利用 cin 从键盘上为变量 a,b 赋值s=a;if (a main() int x,y;coutx;if (x main() int a,b,c;int smallest;coutabc;if (a
8、 main() int score;/从键盘上输入分数coutscore;/用带 else if 的条件语句判断处理if (score100) cout=90) cout=80) cout=70) cout=60) cout main() int n;coutn;if (n=0 /输入数据couta;coutb;/找出较大值Max=ab?a:b;cout main() int a,b;/输入数据couta;coutb;/除法判断if (b!=0 char c1;cinxc1y; /c1/多路选择语句选择不同表达式计算语句switch(c1) case +:cout float x=365.5;
9、 /声明全局变量 main() int x=1,y=2;double w=x+y;double x=1.414,y=1.732,z=3.14;cout main() /显示 1,2,3.10for(int i=1;i=1;j-)cout main() /计算 s=1+2+3.+100int s=0,n=1;while(nx;while(x!=0) sum+=x;coutx;cout main() /计算 s=1+2+3.+100int s=0,n=0;do n+;s+=n;while(nx;sum+=x; while(x!=0);cout main() /计算和打印打印乘法九九表for (in
10、t i=1;i main() int x,sum=0;/定义标号 L1 L1: coutx;if (x=-1)goto L2; /无条件转移语句,转到 L2 语句处elsesum+=x;goto L1; /无条件转移语句,转到 L1 语句处/定义标号 L2 L2: cout main() /累加键盘输入的数据double x,sum=0.0;while(1) coutx;if (x main() int i;for (i=1;i main() /声明数组和变量int a5,i,sum;double avg;/从键盘上循环为数组赋值for (i=0;iai;/直接显示数组元素cout main(
11、) int i,max,index,a5;/从键盘上为数组赋值for (i=0;iai;/ 利用循环遍历数组,找出最大值的元素及其下标max=a0;for (i=0;i #define size 5 main() /声明变量int i,j;float t,asize;/从键盘上为数组赋值for (i=0;iai;/对数组按从小到大顺序排序for (i=0;iaj)t=ai;ai=aj;aj=t;/显示排序结果for (i=0;ivalue;/二分法查找数组 afound=0;low=0;high=size-1;while(low main() /声明变量int i,j;float t,a5;/
12、从键盘上为数组赋值for (i=0;iai;/对数组按从大到小顺序排序for (i=0;i main() /声明二维数组及变量 int a23,i,j;/从键盘上为数组 a 赋值for (i=0;iaij;/显示数组 afor (i=0;i main() /声明字符数组和变量char str6;int i;/从键盘上输入字符串coutstr; cout=0;i-) cout main() /声明变量和指针变量int a,b,c,*ip;/指针变量 ip 指向变量 aa=100;ip= /使指针变量 ip 指向变量 acout main() /声明数组、变量和指针变量int a23,i,j;in
13、t* ip;/从键盘上为数组 a 赋值for (i=0;iaij;/利用下标变量显示数组 afor (i=0;i main() /声明数组、变量和指针变量int a=1,2,3,4,5,6;int *ip1,*ip2;/测试指针的赋值运算ip1=a;ip2=ip1; coutip1;coutip1=“ main() /声明字符型数组和指针变量char str10;char *strip=str;/输入输出coutstr; /用字符数组输入字符串coutstrip; /用字符指针变量输入字符串coutstrip; /用字符指针变量输入字符串cout main() / 声明用于存放运动员号码的数组
14、int h=1001,1002,1003,1004; / 声明用于存放运动员成绩的数组float x=12.3,13.1,11.9,12.1; /声明用于存放运动姓名的字符型指针数组char *p=“Wang hua“,“Zhang jian“,“Li wei“,“Hua ming“; /i,j,it 是用做循环控制变量和临时变量int i,j,it; /ft 用做暂存变量float ft; /pt 为字符型指针变量用做暂存指针变量char *pt; /用选择法对数组 x 进行排序,并相应调整数组 h 和 p 中的数据for (i=0;i=xj) ft=xi,xi=xj,xj=ft;it=hi
15、,hi=hj,hj=it;pt=pi,pi=pj,pj=pt;/以下打印排序结果for (i=0;i main() /声明指针数组char *colors=“Red“,“Blue“,“Yellow“,“Green“; /指向指针的指针变量char *pt; /通过指向指针的变量访问其指向的内容pt=colors;for (int i=0;i main() /定义结构类型struct bookschar title20;char author15;int pages;float price; ;/声明结构变量struct books Zbk=“VC+ “,“Zhang“,295,35.5; bo
16、oks Wbk; /对结构变量的输出coutWbk.title;coutWbk.author;coutWbk.pages;coutWbk.price;cout main() int i;/定义结构类型 struct student int num;char name10;float maths;float physics;float chemistry;double total;/声明结构数组 ststudent st3;/从键盘上为结构数组输入值 coutsti.num;cinsti.name;cinsti.maths;cinsti.physics;cinsti.chemistry;/计算每
17、个学生的总成绩for (i=0;i main() /定义结构类型struct human char name10;int sex;int age;/声明结构变量和结构指针变量,并初始化struct human x=“WangPing“,1,30,*p=NULL;/结构指针变量指向对象p=/显示结构变量的值coutname=“namesex=“sexage=“age(*p).name;cout(*p).sex;cout(*p).age;/显示结构变量的值cout main() /定义结构类型struct human char name10;int sex;int age;/声明结构变量和结构指针
18、,并初始化struct human x=“WangPing“,1,30,*p=/利用结构指针显示结构中的数据coutname=“;cinp-name;coutsex=“;cinp-sex;coutage=“;cinp-age;coutname=“namesex=“sexage=“age main() /定义结构类型struct human char name10;int sex;int age;/声明结构数组和结构指针变量,并初始化human x=“WeiPing“,1,30,“LiHua“,1,25,“LiuMin“,0,23,*p=NULL;/用下标变量的输出结构数组的元素for (int
19、 i=0;inamesexage main() /定义一个包含指针成员的结构类型struct test char *str;int *ip; x;/使用结构变量 x 中的整型指针 ipx.ip=new int; /分配 1 个单元*(x.ip)=100;cout main() /定义 date 结构struct dateint year;int month;int day;/定义 baby 结构struct baby int num;float weight;date birthday; / date 为结构类型 ; /声明 baby 结构变量并初始化baby b1=10001,10,2002
20、,12,25;/下列是 baby 结构变量 b1 的引用。cout main() /定义名为 list 的递归结构 struct list char name10;int sex;int age;list *next; /成员 next 为指向其自身结构的指针;/使用递归结构变量list L1=“WeiPing“,1,35.5,NULL;cout main() int i;/定义名为 student 的递归结构 struct student char name10;int math;int computer;float sum;student *next; /next 成员是指向自身的结构指针
21、 ;/用 student 声明 3 个结构指针变量struct student *head,*tail,*temp; /申请第 1 块数据,并设置各结构指针的初值temp=new struct student; /申请内存 head=temp; / 头指针 tail=head; / 尾指针 /循环为链表输入数据couttemp-name;if (temp-name0!=*)cintemp-mathtemp-computer;temp-sum=temp-math+temp-computer;temp-next=NULL;tail=temp; /设置链表尾指针 else/ 以下是输入结束处理 de
22、lete temp;tail-next=NULL;break;/为下一个学生申请内存temp-next=new struct student; temp=temp-next; / 使处理指针 temp 指向新内存块/将链表数据从头到尾打印出来coutnamemathcomputersumnext; #include main() int i;/定义名为 student 的递归结构 struct student char name10;int math;int computer;float sum;student *forw; /forw 成员是前指针 student *next; /next
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 精彩 资料 程序代码 大全

限制150内