C++经典程序代码大全.doc

上传人:豆**** 文档编号:33413450 上传时间:2022-08-11 格式:DOC 页数:103 大小:471.50KB
返回 下载 相关 举报
C++经典程序代码大全.doc_第1页
第1页 / 共103页
C++经典程序代码大全.doc_第2页
第2页 / 共103页
点击查看更多>>
资源描述

《C++经典程序代码大全.doc》由会员分享,可在线阅读,更多相关《C++经典程序代码大全.doc(103页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、如有侵权,请联系网站删除,仅供学习与交流C+经典程序代码大全【精品文档】第 102 页/根据半径计算圆的周长和面积#include const float PI=3.1416; /声明常量(只读变量)PI为3.1416float 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

2、 coutl=l; /显示计算结果 coutns=0.0) /如果参数大于0,则计算圆的周长 z=2*PI*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.CPP Written by: Hap Date written: 02:11:10#include void main(void) double s1,s2,s3; s1=1

3、.5; /* 对变量s1赋值*/ couts1=s1endl; /* 对变量s2赋值*/ s2=2.5; couts2=s2endl; s3= /* 对变量s3赋值*/ 3.5; couts3=s3endl; couts1+s2+s3=s1+s2+s3endl; /计算并显示 /计算并显示 couts1+s2+s3=s1+s2+s3endl; #include main() double r=1.0; coutr=rendl; double l; l=2*3.1416*r; /计算圆的周长,赋值给变量l coutl=lendl; /显示圆的周长 double s=3.1416*r*r; /计算

4、圆的面积,赋值给变量s couts=sendl; /显示圆的面积 coutr; /键盘输入 l=2*3.1416*r; /计算圆的周长,赋值给变量l coutl=lendl; /显示圆的周长 s=3.1416*r*r; couts=sendl; /显示圆的面积#include /包含iostream.h头文件void main() /输出字符常量、变量和字符串 char c1=A; coutW; coutc1endl; coutThis is a test.endl; cout-endl; /输出整型常量、变量和表达式 int n=100; cout10; coutn; cout2*nendl

5、; /输出整型表达式 cout-endl; /输出浮点型常量、变量和表达式 double pi=3.1415926,r=10.0,s=pi*r*r; coutpiendl; coutr; couts; cout2*r*piendl; /输出浮点型表达式 cout-endl; /一个cout可以输出多项数据 coutW c1endl; coutThis is a test.endl; coutpi=pi r=r s=sendl;#include /包含iostream.h头文件main() /输入输出字符 char c; cinc; coutc=cn; coutn=nx; coutx=xendl

6、; /输入提示 coutn; coutn=nendl; /多项输入 coutc n xcnx; coutc=c n=n x=xendl;#include /包含iostream.h头文件main() /声明整型变量 int a,b; /从键盘上为整型变量赋值 couta; coutb; /整型数的算术运算 couta+b=a+bendl; couta-b=a-bendl; couta*b=a*bendl; couta/b=a/bendl; couta%b=a%bendl; /测试溢出 short n=32767,m; /n取short类型的最大值 coutn=nendl; m=n+1; /引起

7、溢出 coutn+1=mendl;#include /包含iostream.h头文件main() /声明变量,并初始化 int a=010,b=10,c=0X10; /以十进制形式显示数据 coutDEC:; cout a=a; cout b=b; cout c=cendl; /以八进制形式显示数据 coutOCT:; coutoct; /指定八进制输出 cout a=a; cout b=b; cout c=cendl; /以十六进制形式显示数据 coutHEX:; couthex; /指定十六进制输出 cout a=a; cout b=b; cout c=cendl; /八、十和十六进制数混

8、合运算并输出 couta+b+c=; coutdec; /恢复十进制输出 couta+b+cendl; /测试八、十和十六进制输入 couta; coutb; coutc; coutDEC:decendl; /指定十进制输出 couta=aendl; coutb=bendl; coutc=cendl;#include /包含iostream.h头文件#include / iomanip.h头文件包含setprecision()的定义main() /float型变量的声明、输入、计算和输出 float fx,fy; coutfx; coutfy; coutfx+fy=fx+fyendl; cou

9、tfx-fy=fx-fyendl; coutfx*fy=fx*fyendl; coutfx/fy=fx/fyendlendl; /coutfx%fy=fx%fyendl; Error! /double型变量的声明、输入、计算和输出 float dx,dy; coutdx; coutdy; coutdx+dy=dx+dyendl; coutdx-dy=dx-dyendl; coutdx*dy=dx*dyendl; coutdx/dy=dx/dyendlendl; /coutfx%fy=fx%fyendl; Error! /测试float和double类型数据的有效位 fx=10.0;fy=6.0

10、; float fz=fx/fy; dx=10.0;dy=6.0; double dz=dx/dy; coutfz=; coutsetprecision(20)fx/fy=fzendl; coutdz=; coutsetprecision(20)dx/dy=dzendlendl; /float型溢出 float x=3.5e14; coutx=xendl; coutx*x=x*xendl; coutx*x*x=x*x*xendl;#include /包含iostream.h头文件main() /字符类型变量的声明 char c1=A; char c2; /字符数据的运算及输出 c2=c1+32

11、; coutc1=c1endl; coutc2=c2endl; /输出字符及ASCII码 coutc1 : int(c1)endl; coutc2 : int(c2)endl; cout$ : int($)endl; /输入字符 coutc1 c2c1c2; coutc1=c1 c2=c2endl;#include /包含iostream.h头文件main() char c1=a,TAB=t; /阵铃一声 coutc1endl; /使用水平制表符 cout1TAB2TAB3TAB4endl; /使用双引号 coutHe said Thank you.endl; /使用回车换行 coutabcn

12、defn;#include /包含iostream.h头文件main() /声明bool变量,并初始化 bool flag1=false,flag2=true; /输出布尔常量和变量 coutfalse:falseendl; couttrue: trueendl; coutflag1=flag1endl; coutflag2=flag20; /存放关系运算结果 coutflag1=flag1endl; flag2=flag1; /bool类型变量相互赋值 coutflag2=flag2endl; /布尔变量超界处理 flag1=100; coutflag1=flag1endl; flag2=-

13、100; coutflag2=flag2endl;#include const double PI=3.1416; /声明常量(const变量)PI为3.1416main() /声明3个变量 double r,l,s; /输入圆的半径 coutr; /计算圆的周长 l=2*PI*r; coutl=lendl; /计算圆的面积 s=PI*r*r; couts=sendl; #includemain() /定义枚举类型,并指定其枚举元素的值 enum color RED=3, YELLOW=6, BLUE=9 /声明枚举变量a和b,并为枚举变量a赋初值 enum color a=RED; colo

14、r b; /合法,与C语言不同 / 输出枚举常量 coutRED=REDendl; coutYELLOW=YELLOWendl; coutBLUE=BLUEendl; /枚举变量的赋值和输出 b=a; a=BLUE; couta=aendl; coutb=bendl; /a=100; 错误! /a=6 也错误! /枚举变量的关系运算 b=BLUE; / 枚举变量的赋值运算 coutab=(ab)endl;#include const double PI=3.1416; /声明常量(const变量)PI为3.1416main() /声明3个变量 double r=3,l,s; /计算圆的周长 l

15、=2*PI*r; coutl=lendl; /计算圆的面积 s=PI*r*r; couts=sendl; /验证赋值误差 int il,is; il=l; is=s; coutil=ilendl; coutis=isendl; #include main() /变量声明 char c; double x,y; /测试自增cout+E and E+ :endl; c=B; coutc=+cendl; /输出c=C c=B; coutc=c+endl; /输出c=B x=1.5; y=5+ +x; /加号后的空格不能少 couty=yendl; /输出y=7.5 x=1.5; y=5+x+; co

16、uty=yendl; /输出y=6.5 cout-endl;/测试自减cout-E and E- :endl; c=B; coutc=-cendl; /输出c=A c=B; coutc=c-endl; /输出c=B x=1.5; y=5+-x; couty=yendl; /输出y=5.5 x=1.5; y=5+x-; couty=yendl; /输出y=6.5#include main() int a=3, b=2; /输出关系表达式 coutabendl; cout(ab)b)=b)(a=b)(a!=b)endl; bool flag=2*ab+10; coutflag=flag;#incl

17、ude main() float a=3.5,b=2.1,c=0; couta=a b=b c=cendl; /与运算 couta&b=(a&b)endl;/输出1 couta&c=(a&c)endl;/输出0 /或运算 couta|b=(a|b)endl;/输出1 couta|c=(a|c)endl;/输出1 /非运算 cout!a=!aendl!c=!c=0 & a=5; /变量a在0,5区间内 cout0 & a=5=flagendl;/输出1 /算术运算、关系运算和逻辑运算 cout2*b+2|ab+3=2*b+2|ab+3)endl;/输出1#include main() /按位与运

18、算 cout24&12=(24&12)endl; /按位异或运算 cout2412=(2412)endl; /按位或运算 cout24|12=(24|12)endl; /按位取反运算 cout24=(24)endl; /左移位运算 cout53=(53)endl; cout-53=(-53)endl; /右移位运算 cout3=3)endl; cout3=3)endl;#include main() int a=1,b=1,c=3; /显示a,b,c的值 couta=a b=b c=cendl; /计算显示(1) b+=a+2*c%5; 的结果 b+=a+2*c%5; /相当于表达式语句 b=

19、b+(a+2*c%5); cout(1) b=bendl; /计算显示(2) a=c-2*b; 的结果 a=1,b=1,c=3; a=c-2*b; / 相当于表达式语句 a=a(c-2*b); cout(2) a=aendl; /计算显示(3) a*=b=c=3;的结果 a=1,b=1,c=3; a*=b=c=3; /相当于语句组 c=3;b=c;a=a*b; cout(3) a=a b=b c=cendl; /计算显示(4) a+=b+=c;的结果 a=1,b=1,c=3; a+=b+=c; /相当于语句组 b=b+c; a=a+b; cout(4) a=a b=b c=cendl; /计算

20、显示(5) a-=b=+c+2;的结果 a=1,b=1,c=3; a-=b=+c+2; /相当于语句组 +c;b=b+c+2;a=a-b; cout(5) a=a b=b c=cendl;#include main() /用 sizeof 计算各类种常量的字节长度 coutsizeof($)=sizeof($)endl; coutsizeof(1)=sizeof(1)endl; coutsizeof(1.5)=sizeof(1.5)endl; coutsizeof(Good!)=sizeof(Good!)endl; /用sizeof 计算各类型变量的字节长度 int i=100; char c

21、=A; float x=3.1416; double p=0.1; coutsizeof(i)=sizeof(i)endl; coutsizeof(c)=sizeof(c)endl; coutsizeof(x)=sizeof(x)endl; coutsizeof(p)=sizeof(p)endl; /用sizeof 计算表达式的字节长度 coutsizeof(x+1.732)=sizeof(x+1.732)endl; /用 sizeof 计算各类型的字节长度 coutsizeof(char)=sizeof(char)endl; coutsizeof(int)=sizeof(int)endl;

22、coutsizeof(float)=sizeof(float)endl; coutsizeof(double)=sizeof(double)endl; /用sizeof 计算数组的字节长度 char str=This is a test.; int a10; double xy10; coutsizeof(str)=sizeof(str)endl; coutsizeof(a)=sizeof(a)endl; coutsizeof(xy)=sizeof(xy)endl; /用sizeof 计算自定义类型的长度 struct st short num; float math_grade; float

23、 Chinese_grade; float sum_grade; st student1; coutsizeof(st)=sizeof(st)endl; coutsizeof(student1)=sizeof(student1)endl;#include main() /声明变量语句中使用顺序运算 int x, y; /计算中使用顺序运算 x=50; y=(x=x-5, x/5); coutx=xendl; couty=yendl;#include main() /测试表达式类型的转换 int n=100,m; double x=3.791,y; coutn*x=n*xendl; /赋值类型转

24、换 m=x; y=n; coutm=mendl; couty=yendl; /强制类型转换 coutint(x)=int(x)endl; cout(int)x=(int)xendl; coutint(1.732+x)=int(1.732+x)endl; cout(int)1.732+x=(int)1.723+xendl; coutdouble(100)=double(100)endl;#include main() float a,b,s; couta bab; /利用cin从键盘上为变量 a,b 赋值 s=a; if (ab) s=b; /if语句中只有这一个语句,可省略花括号 s=s*s;

25、 /变量s中保存a,b中较大的一个数的平方 couts=s;#include main() int x,y; coutx; if (x=0) /满足条件执行 y=2*x; couty=y; /输出结果 else /不满足条件执行 y=x*x; couty=y; /输出结果#include main() int a,b,c; int smallest; couta b cabc; if (a=b) /外层条件语句 if (a=c) /内层条件语句 smallest=a; else smallest=c; else if (b=c) /内层条件语句 smallest=b; else smalles

26、t=c; coutSmallest=smallestendl;#include main() int score; /从键盘上输入分数 coutscore; /用带else if的条件语句判断处理 if (score100) coutThe score is out of range!=90) coutYour grade is a A.=80) coutYour grade is a B.=70) coutYour grade is a C.=60) coutYour grade is a D.endl; else coutYour grade is a E.endl;#include ma

27、in() int n; coutn; if (n=0 & n=100 &n%2=0) coutn=nendl; else coutThe n is out of range!endl;#include main() int a,b,Max; /输入数据 couta; coutb; /找出较大值 Max=ab?a:b; coutMax=Maxendl;#include main() int a,b; /输入数据 couta; coutb; /除法判断 if (b!=0 & a%b=0) coutb divides aendl; couta/b=a/bendl; else coutb does not divide aendl;#include main() /x,y 为操作数,c为运算符 int x,y,z; char c1; cinxc1y; /c1 /多路选择语句选择不同表达式计算语句 switch(c1) case +:coutx+y=x+yendl; break; case -:coutx-y=x-yendl; break; case *:coutx*y=x*yendl; break; case /:coutx/y=x/yendl; break; case %:coutx%y=x%y

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

当前位置:首页 > 教育专区 > 高考资料

本站为文档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