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

上传人:一*** 文档编号:808796 上传时间:2019-07-16 格式:DOC 页数:134 大小:530.50KB
返回 下载 相关 举报
C++精彩资料程序代码大全.doc_第1页
第1页 / 共134页
C++精彩资料程序代码大全.doc_第2页
第2页 / 共134页
点击查看更多>>
资源描述

《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

23、成员是后指针;/用 student 声明 3 个结构指针变量struct student *head,*tail,*temp; /申请第 1 块数据,并设置各结构指针的初值temp=new struct student; /申请内存 head=temp; / 头指针 tail=head; / 尾指针 head-forw=NULL;/循环为链表记录输入数据couttemp-name;if (temp-name0!=*)cintemp-mathtemp-computer;temp-sum=temp-math+temp-computer;temp-next=NULL;tail=temp; /设置链表

24、尾指针 else/ 以下是输入结束处理 delete temp;tail-next=NULL;break;/为下一个学生申请内存temp-next=new struct student; temp-next-forw=temp; /设置前指针temp=temp-next; /使处理指针 temp 指向新内存块/ 将链表数据从头到尾打印出来couttail:“namemathcomputersumnext;/ 将链表数据从尾到头打印出来couthead:“namemathcomputersumforw; #include main() int i;/定义联合类型union utag char c

25、;int k;float x;/声明联合变量union utag u; / 使用联合变量中的字符型成员 u.c=*;cout main() /定义结构类型,并为声明的结构变量赋初值struct s_tag short i;float x; sx=100,3.1416;/定义联合类型,并为声明的联合变量赋初值union u_tag short i;float x; ux=1000;/输出结构类型和结构变量的有关信息cout main() /自定义类型 typedef int ARRAY_INT50;int i;ARRAY_INT a; /用自定义类型声明数组变量 a /以下为数组 a 赋值,并打

26、印 for (i=0;i /定义结构类型 struct student int num;char name20;float grade; ; void main(void) /声明数组int i,size;char str=“This is a string.“;int int_values = 51, 23, 2, 44, 45,0,11; float float_values = 15.1, 13.3, 22.2, 10.4, 1.5; student st_arr=101,“WangLin“,92,102,“LiPing“,85,103,“ZhaoMin“,88;/显示 char 类型数

27、组元素及其大小size=sizeof(str) / sizeof(char);cout /add()函数的定义,其有返回值 double add(double x,double y) double z;z=x+y;cout /定义符号函数 sgn(),其返回值为 int 类型 int sgn(double x) if (x0) return(1); /返回出口 1if (xx;cout /函数原型语句可以在这里 /定义 main()函数 main() /max()函数原型声明语句float max(float,float);/变量声明语句float a,b,Max;/输入参数并计算couta;

28、coutb;Max=max(a,b); /调用 max()函数 couty)?x:y;return(z); #include /定义 f()函数 f(int x,int y) /f()的参数以值方式传递 +x;-y;cout/定义公共结构类型 struct student int num;char name10;float maths;float physics;float chemistry;double total; ;/定义结构输入函数 input_Rec(struct student *p) /参数为 student 类型的结构指针变量 cinp-num;cinp-name;cinp-

29、maths;cinp-physics;cinp-chemistry; /定义结构数据交换函数 swap_Rec(struct student *p1,struct student *p2) struct student x;/交换两个记录的数据x=*p1;*p1=*p2;*p2=x; /输出结构的值 put_Rec(struct student *p) coutnumnamemathsphysicschemistrytotaltotal=p1-maths+p1-physics+p1-chemistry;/对 3 个学生的数据排序for (i=0;i /定义结构 struct student c

30、har name10;float grade; ;/交换 student 类型的数据 void swap(student temp=x;x=y;y=temp; /返回 student 类型的引用,求优者 student /显示 student 类型的数据 void show(student 编译报错!/*string=x; 编译报错!int i=0;while (*(string+i)!=NULL) i+;return i; /main()函数中测试 str_len() void main() char a=“ABCDE“;cout void disp(void); /这个函数声明语句不能少/

31、定义 main()函数的参数和返回值类型是 void 类型 void main(void) /调用 void 类型函数disp(); /以下定义 disp()函数 void disp(void) cout /函数原型语句 int abs(int x); long abs(long x); float abs(float x);/main()函数的定义 void main(void) /声明变量int i1=32767,i2=-32767;long l1=456789,l2=-456789;float x1=1.1234,x2=-1.1234;/直接在 cout 输出中调用函数cout /max

32、()为内联函数 inline int max(int x,int y) /注意 inline 关键字 return xy?x:y; /定义 main()函数 main() int a=3,b=5,c;c=max(a,b); cout main() /函数原型声明int fact(int x);int n,sn;/依次从键盘上输入 3 个正整型数据计算它们的阶乘for (int i=1;in;sn=fact(n);cout /带参数的 main()函数 int main(int argc,char *argv) int i;for(i=0;i /用函数原型声明要使用的函数 void show_a

33、rray1(int*,int); void show_array2(int a,int); void sort(int*,int); main() /声明数组并初始化int a=2,4,6,1,3,5;int b33=2,4,6,1,3,5,0,1,2;/显示数组的值cout /定义结构 struct student char name10;float grade; ;/更改 student 数据的 grade 成员,参数形式为引用 void change(student /更改 student 数据的 grade 成员,参数形式为指针 void change1(student *p,floa

34、t grade) p-grade=grade; /更改 student 类型的数据,普通参数形式 void change2(student x,float grade) x.grade=grade; /显示 student 类型的数据,参数形式为引用 void show(student for (int i=0;i/参数为函数指针的函数 int get_result(int a, int b, int (*sub)(int,int) int r;r=sub(a,b);return r; /计算最大值 int max(int a, int b) cout b) ? a: b); /计算最小值 i

35、nt min(int a, int b) coutab;cout #include #define size 3 /定义 book 结构类型 struct book char title20;char author15;int pages;float price; ; /book 结构的输入函数 input_book(bookcoutbk.author;coutbk.pages;coutbk.price; /book 结构的输出函数 output_book(book extern float p=3.14;/在 show()函数中使用外部变量 show() int i;cout /使用静态变量

36、的计数器函数 count1() /声明静态变量 i,并置初值为 0。i 在 count()中局部可见static int i=0; return(+i); /使用局部变量的计数器函数 count2() int i=0; return(+i); /在 main()函数中调用 count()函数 main() int i;/调用 count1()10 次cout main() int i,s=0;extern int fact(int x);for (i=2;i #include #include #include main() /声明变量FILE *fp1;char str80;/从键盘上任意输

37、入一个字符串cout #include #include #include void main(void) /变量声明char ch;FILE *fp1;/以写入方式打开 d.dat 文件 if (fp1=fopen(“d.dat“,“w“)=NULL) coutch; while (ch!=*) fputc(ch,fp1); /将字符写到 fp1 指向的“流“文件中cinch; fclose(fp1); /关闭文件/ 以读方式打开 d.dat 文件if (fp1=fopen(“d.dat“,“r“)=NULL) cout #include #include #include main() /

38、声明变量int i=0;char p100; / 声明输入缓冲区 FILE *fp1; / 声明文件指针变量/以写入方式打开 d.dat 文件if (fp1=fopen(“d.dat“,“w“)=NULL)coutp; /从键盘上输入数据if (stricmp(p,“end“) /如果输入的字符串为 end,则结束循环fputs(p,fp1); /写入文件操作fputs(“n“,fp1); elsebreak; /退出循环fclose(fp1); /关闭文件/ 以读方式打开 d.dat 文件if (fp1=fopen(“d.dat“,“r“)=NULL) cout #include #incl

39、ude #include #include #define MAX 10 main() /声明变量int i,n;FILE *fp1; / 声明文件指针变量/以写入方式打开 d.dat 文件if (fp1=fopen(“d.dat“,“w“)=NULL)cout #include #include #include #include #define MAX 3 main() /定义结构类型struct student int num;char name10;float grade;/声明数组和变量student st3;int i;FILE *fp1; / 声明文件指针变量/以写入方式打开 d

40、.dat 文件if (fp1=fopen(“d.dat“,“w“)=NULL)coutsti.num;cinsti.name;cinsti.grade;fprintf(fp1,“%d %s %fn“,sti.num,sti.name,sti.grade);fclose(fp1); /关闭文件/ 以读方式打开 d.dat 文件if (fp1=fopen(“d.dat“,“r“)=NULL) cout #include #include #include int main(void) FILE *fpd,*fpw; / 声明 FILE 结构指针变量 unsigned char dw;int i=0

41、;/以二进制读方式打开 Calc.exe 文件if(fpd=fopen(“C:WINDOWSCalc.exe“, “rb“)=NULL) cout #include #include #include void main(void) /声明变量int i;char ch;FILE *fp1;/以写入方式打开 d.dat 文件 if (fp1=fopen(“d.dat“,“w“)=NULL) coutch; while (ch!=*) fputc(ch,fp1); /将字符写到 fp1 指向的“流“文件中cinch; cout #include #include #include #define

42、 MAX 5/显示数组的数据 void show_array(double x,int size) for(int i=0;i #include #include #include #define MAX 5 /定义结构类型 struct student int num;char name20;float grade; ;/显示 student 结构数据 void show_str(student a,char *name) cout #include #include int main(void) /声明变量char ch;char str20;int n;float x;/用 stdin

43、从键盘上输入数据fprintf(stdout,“ch strn“);fscanf(stdin,“%c %s“,fprintf(stdout,“n x n“);fscanf(stdin,“%d %f“,cout void main( void ) int c;/* Create an error by writing to standard input. */putc( A, stdin );if( ferror( stdin ) )perror( “Write error“ );clearerr( stdin );/* See if read causes an error. */printf

44、( “Will input cause an error? “ );c = getc( stdin );if( ferror( stdin ) )perror( “Read error“ );clearerr( stdin ); #include #include /此预处理指令不可少 const double HD=3.1415926/180; main() cout /以下是几个简单宏替换预处理指令 #define YES 1 #define PI 3.1415926 #define RAD PI/180 #define MESG “This is a string.“/以下是主程序 main() /以下各语句使用了宏替换 cout /以下为带参数宏替换的预处理指令 #define PRINT(k) cout(b) ? (a)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com