C++经典程序代码大全.doc
如有侵权,请联系网站删除,仅供学习与交流C+经典程序代码大全【精品文档】第 102 页/根据半径计算圆的周长和面积#include <iostream.h>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个变量 cout<<"r=" /显示字符串 cin>>r; /键盘输入 l=fCir_L(r); /计算圆的周长,赋值给变量l s=fCir_S(r); /计算圆的面积,赋值给变量s cout<<"l="<<l; /显示计算结果 cout<<"ns="<<s; /定义计算圆的周长的函数fCir_L()float fCir_L(float x) float z=-1.0; /声明局部变量 if (x>=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 <iostream.h>void main(void) double s1,s2,s3; s1=1.5; /* 对变量s1赋值*/ cout<<"s1="<<s1<<endl; /* 对变量s2赋值*/ s2=2.5; cout<<"s2="<<s2<<endl; s3= /* 对变量s3赋值*/ 3.5; cout<<"s3="<<s3<<endl; cout<<"s1+s2+s3="<<s1+s2+s3<<endl; /计算并显示 /计算并显示 cout<<"s1+s2+s3="<<s1+s2+s3<<endl; #include <iostream.h>main() double r=1.0; cout<<"r="<<r<<endl; double l; l=2*3.1416*r; /计算圆的周长,赋值给变量l cout<<"l="<<l<<endl; /显示圆的周长 double s=3.1416*r*r; /计算圆的面积,赋值给变量s cout<<"s="<<s<<endl; /显示圆的面积 cout<<"r=" /显示提示输入的信息 cin>>r; /键盘输入 l=2*3.1416*r; /计算圆的周长,赋值给变量l cout<<"l="<<l<<endl; /显示圆的周长 s=3.1416*r*r; cout<<"s="<<s<<endl; /显示圆的面积#include <iostream.h> /包含iostream.h头文件void main() /输出字符常量、变量和字符串 char c1='A' cout<<'W' cout<<c1<<endl; cout<<"This is a test."<<endl; cout<<"-"<<endl; /输出整型常量、变量和表达式 int n=100; cout<<10; cout<<n; cout<<2*n<<endl; /输出整型表达式 cout<<"-"<<endl; /输出浮点型常量、变量和表达式 double pi=3.1415926,r=10.0,s=pi*r*r; cout<<pi<<endl; cout<<r; cout<<s; cout<<2*r*pi<<endl; /输出浮点型表达式 cout<<"-"<<endl; /一个cout可以输出多项数据 cout<<'W'<<" "<<c1<<endl; cout<<"This is a test."<<endl; cout<<"pi="<<pi<<" r="<<r<<" s="<<s<<endl;#include <iostream.h> /包含iostream.h头文件main() /输入输出字符 char c; cin>>c; cout<<"c="<<c<<endl; /输入输出整型数据 int n; cin>>n; cout<<"n="<<n<<endl; /输入输出浮点型数据 double x; cin>>x; cout<<"x="<<x<<endl; /输入提示 cout<<"n=" cin>>n; cout<<"n="<<n<<endl; /多项输入 cout<<"c n x"<<endl; cin>>c>>n>>x; cout<<"c="<<c<<" n="<<n<<" x="<<x<<endl;#include <iostream.h> /包含iostream.h头文件main() /声明整型变量 int a,b; /从键盘上为整型变量赋值 cout<<"a=" cin>>a; cout<<"b=" cin>>b; /整型数的算术运算 cout<<a<<"+"<<b<<"="<<a+b<<endl; cout<<a<<"-"<<b<<"="<<a-b<<endl; cout<<a<<"*"<<b<<"="<<a*b<<endl; cout<<a<<"/"<<b<<"="<<a/b<<endl; cout<<a<<"%"<<b<<"="<<a%b<<endl; /测试溢出 short n=32767,m; /n取short类型的最大值 cout<<"n="<<n<<endl; m=n+1; /引起溢出 cout<<"n+1="<<m<<endl;#include <iostream.h> /包含iostream.h头文件main() /声明变量,并初始化 int a=010,b=10,c=0X10; /以十进制形式显示数据 cout<<"DEC:" cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; /以八进制形式显示数据 cout<<"OCT:" cout<<oct; /指定八进制输出 cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; /以十六进制形式显示数据 cout<<"HEX:" cout<<hex; /指定十六进制输出 cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; /八、十和十六进制数混合运算并输出 cout<<"a+b+c=" cout<<dec; /恢复十进制输出 cout<<a+b+c<<endl; /测试八、十和十六进制输入 cout<<"DEC:a=" cin>>a; cout<<"OCT:b=" cin>>b; cout<<"HEX:a=" cin>>c; cout<<"DEC:"<<dec<<endl; /指定十进制输出 cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c<<endl;#include <iostream.h> /包含iostream.h头文件#include<iomanip.h> / iomanip.h头文件包含setprecision()的定义main() /float型变量的声明、输入、计算和输出 float fx,fy; cout<<"fx=" cin>>fx; cout<<"fy=" cin>>fy; cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl; cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl; cout<<fx<<"*"<<fy<<"="<<fx*fy<<endl; cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl; /cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error! /double型变量的声明、输入、计算和输出 float dx,dy; cout<<"dx=" cin>>dx; cout<<"dy=" cin>>dy; cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl; cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl; cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl; cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl; /cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error! /测试float和double类型数据的有效位 fx=10.0;fy=6.0; float fz=fx/fy; dx=10.0;dy=6.0; double dz=dx/dy; cout<<"fz=" cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl; cout<<"dz=" cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl; /float型溢出 float x=3.5e14; cout<<"x="<<x<<endl; cout<<"x*x="<<x*x<<endl; cout<<"x*x*x="<<x*x*x<<endl;#include <iostream.h> /包含iostream.h头文件main() /字符类型变量的声明 char c1='A' char c2; /字符数据的运算及输出 c2=c1+32; cout<<"c1="<<c1<<endl; cout<<"c2="<<c2<<endl; /输出字符及ASCII码 cout<<c1<<" : "<<int(c1)<<endl; cout<<c2<<" : "<<int(c2)<<endl; cout<<'$'<<" : "<<int('$')<<endl; /输入字符 cout<<"c1 c2"<<endl; cin>>c1>>c2; cout<<"c1="<<c1<<" c2="<<c2<<endl;#include <iostream.h> /包含iostream.h头文件main() char c1='a',TAB='t' /阵铃一声 cout<<c1<<endl; /使用水平制表符 cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl; /使用双引号 cout<<"He said "Thank you"."<<endl; /使用回车换行 cout<<"abcn"<<"def"<<'n'#include <iostream.h> /包含iostream.h头文件main() /声明bool变量,并初始化 bool flag1=false,flag2=true; /输出布尔常量和变量 cout<<"false:"<<false<<endl; cout<<"true: "<<true<<endl; cout<<"flag1="<<flag1<<endl; cout<<"flag2="<<flag2<<endl; /布尔变量的赋值和输出 int x=1; flag1=x>0; /存放关系运算结果 cout<<"flag1="<<flag1<<endl; flag2=flag1; /bool类型变量相互赋值 cout<<"flag2="<<flag2<<endl; /布尔变量超界处理 flag1=100; cout<<"flag1="<<flag1<<endl; flag2=-100; cout<<"flag2="<<flag2<<endl;#include <iostream.h>const double PI=3.1416; /声明常量(const变量)PI为3.1416main() /声明3个变量 double r,l,s; /输入圆的半径 cout<<"r=" cin>>r; /计算圆的周长 l=2*PI*r; cout<<"l="<<l<<endl; /计算圆的面积 s=PI*r*r; cout<<"s="<<s<<endl; #include<iostream.h>main() /定义枚举类型,并指定其枚举元素的值 enum color RED=3, YELLOW=6, BLUE=9 /声明枚举变量a和b,并为枚举变量a赋初值 enum color a=RED; color b; /合法,与C语言不同 / 输出枚举常量 cout<<"RED="<<RED<<endl; cout<<"YELLOW="<<YELLOW<<endl; cout<<"BLUE="<<BLUE<<endl; /枚举变量的赋值和输出 b=a; a=BLUE; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; /a=100; 错误! /a=6 也错误! /枚举变量的关系运算 b=BLUE; / 枚举变量的赋值运算 cout<<"a<b="<<(a<b)<<endl;#include <iostream.h>const double PI=3.1416; /声明常量(const变量)PI为3.1416main() /声明3个变量 double r=3,l,s; /计算圆的周长 l=2*PI*r; cout<<"l="<<l<<endl; /计算圆的面积 s=PI*r*r; cout<<"s="<<s<<endl; /验证赋值误差 int il,is; il=l; is=s; cout<<"il="<<il<<endl; cout<<"is="<<is<<endl; #include <iostream.h>main() /变量声明 char c; double x,y; /测试自增cout<<"+E and E+ :"<<endl; c='B' cout<<"c="<<+c<<endl; /输出c=C c='B' cout<<"c="<<c+<<endl; /输出c=B x=1.5; y=5+ +x; /加号后的空格不能少 cout<<"y="<<y<<endl; /输出y=7.5 x=1.5; y=5+x+; cout<<"y="<<y<<endl; /输出y=6.5 cout<<"-"<<endl;/测试自减cout<<"-E and E- :"<<endl; c='B' cout<<"c="<<-c<<endl; /输出c=A c='B' cout<<"c="<<c-<<endl; /输出c=B x=1.5; y=5+-x; cout<<"y="<<y<<endl; /输出y=5.5 x=1.5; y=5+x-; cout<<"y="<<y<<endl; /输出y=6.5#include <iostream.h>main() int a=3, b=2; /输出关系表达式 cout<<a<b<<endl; cout<<(a<b)<<(a>b)<<(a>=b)<<(a=b)<<(a!=b)<<endl; bool flag=2*a<b+10; cout<<"flag="<<flag;#include <iostream.h>main() float a=3.5,b=2.1,c=0; cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl; /与运算 cout<<"a&&b="<<(a&&b)<<endl;/输出1 cout<<"a&&c="<<(a&&c)<<endl;/输出0 /或运算 cout<<"a|b="<<(a|b)<<endl;/输出1 cout<<"a|c="<<(a|c)<<endl;/输出1 /非运算 cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;/输出0 1 /关系运算和逻辑运算 bool flag=a>=0 && a<=5; /变量a在0,5区间内 cout<<"a=>0 && a<=5="<<flag<<endl;/输出1 /算术运算、关系运算和逻辑运算 cout<<"a+5>2*b+2|a<b+3="<<(a+5>2*b+2|a<b+3)<<endl;/输出1#include <iostream.h>main() /按位与运算 cout<<"24&12="<<(24&12)<<endl; /按位异或运算 cout<<"2412="<<(2412)<<endl; /按位或运算 cout<<"24|12="<<(24|12)<<endl; /按位取反运算 cout<<"24="<<(24)<<endl; /左移位运算 cout<<"5<<3="<<(5<<3)<<endl; cout<<"-5<<3="<<(-5<<3)<<endl; /右移位运算 cout<<"5>>3="<<(5>>3)<<endl; cout<<"-5>>3="<<(-5>>3)<<endl;#include <iostream.h>main() int a=1,b=1,c=3; /显示a,b,c的值 cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl; /计算显示(1) b+=a+2*c%5; 的结果 b+=a+2*c%5; /相当于表达式语句 b=b+(a+2*c%5); cout<<"(1) b="<<b<<endl; /计算显示(2) a<<=c-2*b; 的结果 a=1,b=1,c=3; a<<=c-2*b; / 相当于表达式语句 a=a<<(c-2*b); cout<<"(2) a="<<a<<endl; /计算显示(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="<<c<<endl; /计算显示(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="<<c<<endl; /计算显示(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="<<c<<endl;#include <iostream.h>main() /用 sizeof 计算各类种常量的字节长度 cout<<"sizeof('$')="<<sizeof('$')<<endl; cout<<"sizeof(1)="<<sizeof(1)<<endl; cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl; cout<<"sizeof("Good!")="<<sizeof("Good!")<<endl; /用sizeof 计算各类型变量的字节长度 int i=100; char c='A' float x=3.1416; double p=0.1; cout<<"sizeof(i)="<<sizeof(i)<<endl; cout<<"sizeof(c)="<<sizeof(c)<<endl; cout<<"sizeof(x)="<<sizeof(x)<<endl; cout<<"sizeof(p)="<<sizeof(p)<<endl; /用sizeof 计算表达式的字节长度 cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl; /用 sizeof 计算各类型的字节长度 cout<<"sizeof(char)="<<sizeof(char)<<endl; cout<<"sizeof(int)="<<sizeof(int)<<endl; cout<<"sizeof(float)="<<sizeof(float)<<endl; cout<<"sizeof(double)="<<sizeof(double)<<endl; /用sizeof 计算数组的字节长度 char str="This is a test." int a10; double xy10; cout<<"sizeof(str)="<<sizeof(str)<<endl; cout<<"sizeof(a)="<<sizeof(a)<<endl; cout<<"sizeof(xy)="<<sizeof(xy)<<endl; /用sizeof 计算自定义类型的长度 struct st short num; float math_grade; float Chinese_grade; float sum_grade; st student1; cout<<"sizeof(st)="<<sizeof(st)<<endl; cout<<"sizeof(student1)="<<sizeof(student1)<<endl;#include <iostream.h>main() /声明变量语句中使用顺序运算 int x, y; /计算中使用顺序运算 x=50; y=(x=x-5, x/5); cout<<"x="<<x<<endl; cout<<"y="<<y<<endl;#include <iostream.h>main() /测试表达式类型的转换 int n=100,m; double x=3.791,y; cout<<"n*x="<<n*x<<endl; /赋值类型转换 m=x; y=n; cout<<"m="<<m<<endl; cout<<"y="<<y<<endl; /强制类型转换 cout<<"int(x)="<<int(x)<<endl; cout<<"(int)x="<<(int)x<<endl; cout<<"int(1.732+x)="<<int(1.732+x)<<endl; cout<<"(int)1.732+x="<<(int)1.723+x<<endl; cout<<"double(100)="<<double(100)<<endl;#include <iostream.h>main() float a,b,s; cout<<"a b"<<endl; cin>>a>>b; /利用cin从键盘上为变量 a,b 赋值 s=a; if (a<b) s=b; /if语句中只有这一个语句,可省略花括号 s=s*s; /变量s中保存a,b中较大的一个数的平方 cout<<"s="<<s;#include <iostream.h>main() int x,y; cout<<"x=" cin>>x; if (x<=0) /满足条件执行 y=2*x; cout<<"y="<<y; /输出结果 else /不满足条件执行 y=x*x; cout<<"y="<<y; /输出结果#include <iostream.h>main() int a,b,c; int smallest; cout<<"a b c"<<endl; cin>>a>>b>>c; if (a<=b) /外层条件语句 if (a<=c) /内层条件语句 smallest=a; else smallest=c; else if (b<=c) /内层条件语句 smallest=b; else smallest=c; cout<<"Smallest="<<smallest<<endl;#include <iostream.h>main() int score; /从键盘上输入分数 cout<<"score=" cin>>score; /用带else if的条件语句判断处理 if (score<0 | score>100) cout<<"The score is out of range!"<<endl; else if (score>=90) cout<<"Your grade is a A."<<endl; else if (score>=80) cout<<"Your grade is a B."<<endl; else if (score>=70) cout<<"Your grade is a C."<<endl; else if (score>=60) cout<<"Your grade is a D."<<endl; else cout<<"Your grade is a E."<<endl;#include <iostream.h>main() int n; cout<<"n=" cin>>n; if (n>=0 && n<=100 &&n%2=0) cout<<"n="<<n<<endl; else cout<<"The "<<n<<" is out of range!"<<endl;#include <iostream.h>main() int a,b,Max; /输入数据 cout<<"a=" cin>>a; cout<<"b=" cin>>b; /找出较大值 Max=a>b?a:b; cout<<"Max="<<Max<<endl;#include <iostream.h>main() int a,b; /输入数据 cout<<"a=" cin>>a; cout<<"b=" cin>>b; /除法判断 if (b!=0 && a%b=0) cout<<b<<" divides "<<a<<endl; cout<<"a/b="<<a/b<<endl; else cout<<b<<" does not divide "<<a<<endl;#include <iostream.h>main() /x,y 为操作数,c为运算符 int x,y,z; char c1; cin>>x>>c1>>y; /c1 /多路选择语句选择不同表达式计算语句 switch(c1) case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl; break; case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl; break; case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl; break; case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl; break; case '%':cout<<x<<"%"<<y<<"="<<x%y<<