C++典型案例及常见错误分析.doc

上传人:豆**** 文档编号:17406593 上传时间:2022-05-23 格式:DOC 页数:76 大小:283.50KB
返回 下载 相关 举报
C++典型案例及常见错误分析.doc_第1页
第1页 / 共76页
C++典型案例及常见错误分析.doc_第2页
第2页 / 共76页
点击查看更多>>
资源描述

《C++典型案例及常见错误分析.doc》由会员分享,可在线阅读,更多相关《C++典型案例及常见错误分析.doc(76页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流C+典型案例及常见错误分析.精品文档.C+简单程序典型案例【案例2-1】设计一个编写仅包含C+程序基本构成元素的程序/* /注释行开始This is the first C+ program. Designed by zrf */ /注释行结束#include /包含头文件using namespace std; /打开命名空间std/ This is the main function/单行注释语句int main(void) /主函数,程序入口/块作用域开始int age; /声明一个变量 age= 20; /赋值语句 coutThe a

2、ge is:n; /输出一个字符串 coutageendl; /输出变量中的值return 0; /主函数返回0/块作用域结束【案例2-2】计算圆的周长和面积C+语言中常量、变量#include using namespace std;int main()const float PI=3.1415926; /float 型常量float r=2.0; /用float 型常量初始化变量coutr=rendl;/输出圆的半径float length; /float型变量声明length=2*PI*r; /计算圆的周长coutLength=lengthendl;/输出圆的周长float area=P

3、I*r*r; /计算圆的面积coutArea=areaendl;/输出圆的面积return 0;【案例2-3】整数的简单运算除法、求余运算法和增量减量运算符#include using namespace std; int main() int x, y; x = 10; y = 3; cout x / y is x / y /整数的除法操作 with x % y is x % y endl; /整数的取余操作x +; -y ;/使用增量减量运算符cout x / y is x / y n /整数的除法操作 x % y is x % yendl; /整数的取余操作return 0; 【案例2-

4、4】多重计数器前置和后置自增运算符#includeusing namespace std;int main()int iCount=1;iCount=(iCount+)+(iCount+)+(iCount+);/后置+coutThe first iCount=iCountendl;iCount=1;iCount=(+iCount)+(+iCount)+(+iCount);/前置+coutThe second iCount=iCountendl;iCount=1;iCount=-iCount+;/后置+coutThe third iCount=iCountendl;iCount=1;iCount

5、=-+iCount;/前置+coutThe fourth iCount=iCountendl;return 0;【案例2-5】对整数“10”和“20”进行位运算位运算的应用#include using namespace std;int main() cout 20&10= (20&10) endl;/按位与运算 cout 2010= (2010) endl;/按位异或运算 cout 20|10= (20|10) endl;/按位或运算 cout 20= (20) endl; /按位取反运算 cout 203= (203) endl;/左移位运算 cout -203= (-203) endl;

6、/左移位运算 cout 3= 3) endl;/右移位运算 cout 3= 3) endl;/右移位运算return 0;【案例2-6】实现逻辑“异或”运算逻辑运算应用#include using namespace std; int main() bool p, q; p = true; q = true; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = false; q = true; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = true; q = false; cout

7、 p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = false; q = false; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果return 0; 【案例2-7】高效筛选器用条件运算符“?”构建条件表达式#includeusing namespace std;int main()int iNum1=1,iNum2,iMax;coutiNum1iNum2;iMax = iNum1iNum2 ? iNum1 : iNum2; /使用条件运算符构建条件表达式coutThe max integer

8、is: iMaxendl; return 0;【案例2-8】“多计算与单提取”功能的实现逗号表达式#includeusing namespace std;int main() int Val1, Val2, Val3, Left, Midd, Righ;Left = 10;Midd = 20; Righ = 30; Val1 = (Left+, -Midd, Righ+); /使用逗号表达式Val2 = (Righ+, Left+, -Midd); /使用逗号表达式Val3 = ( -Midd, Righ+,Left+); /使用逗号表达式 cout Val1=tVal1 nVal2=tVal

9、2 nVal3=tVal3endl; return 0;【案例2-9】高效的算术运算符复合赋值运算符#include using namespace std;int main()int n=20; cout n = n endl;n += 8; cout After n += 8, n = n endl; /使用复合的赋值运算符+=n -= 6; cout After n -= 6, n = n endl; /使用复合的赋值运算符-=n *= 1; cout After n *= 1, n = n endl;/使用复合的赋值运算符*=n /= 4; cout After n /= 4, n =

10、 n endl; /使用复合的赋值运算符/=n %= 3; cout After n %= 3, n = n endl; /使用复合的赋值运算符%=return 0;【案例2-10】计算不同数据类型的存储容量sizeof运算符#include using namespace std ;int main()cout The size of an int is:tt sizeof(int) bytes.n;cout The size of a short int is:t sizeof(short) bytes.n;cout The size of a long int is:t sizeof(l

11、ong) bytes.n;cout The size of a char is:tt sizeof(char) bytes.n;cout The size of a wchar_t is:t sizeof(wchar_t) bytes.n;cout The size of a float is:tt sizeof(float) bytes.n;cout The size of a double is:t sizeof(double) bytes.n;return 0;【案例2-11】巧妙获取整数部分double和int数据类型的转换#include using namespace std;in

12、t main() int nn=10,mm; double xx=4.741,yy; coutnn*xx=nn*xxendl; /表达式类型转换 mm=xx; yy=nn; /赋值类型转换 coutmm=mmendl yy=yyendl; coutint(xx)=int(xx)endl (int)xx=(int)xxendl; /强制类型转换 coutint(1.412+xx)=int(1.412+xx)endl; /强制类型转换 cout(int)1.412+xx=(int)1.412+xxendl; /强制类型转换return 0;【案例2-12】将分数转换为小数强制类型转换#includ

13、e using namespace std; int main()for( int i=1; i = 5; +i ) cout i / 3 is: (float) i / 3 endl; /强制类型转换return 0; 【案例2-13】安全的除法计算器#include using namespace std; int main() int a, b; cout a; cout b; if(b) cout Divide Result is: a / b n; /排除除数为零的情况else cout Divide by zero!n; return 0; 【案例2-14】猜数游戏嵌套的if条件语

14、句#include #include using namespace std; int main() int MagNum, GueNum; MagNum = rand(); /产生随机数cout GueNum; if (GueNum = MagNum) /if语句块起始位置cout * It is Right *n MagNum is the Magess number.n; /if语句块结束位置else / else语句块起始位置cout Sorry, youre wrong. MagNum) cout Guessed number is too high.n; else cout Gue

15、ssed number is too low.n; /else语句块结束位置return 0; 【案例2-15】根据输入月份输出从年初到本月底的天数不带break的switch#include using namespace std;int main()int year,month,days=0;coutyearmonth;switch (month) /每个case分支均没有break语句 case 12: days +=31;case 11: days +=30; case 10: days +=31;case 9: days +=30;case 8: days +=31;case 7:

16、days +=31;case 6: days +=30;case 5: days +=31;case 4: days +=30;case 3: days +=31; case 2: /判断是否为闰年if (year % 4=0 & year % 100!=0 | year %400=0) days +=29;else days +=28;case 1: days +=31;if (days=0) cout Wrong monthendl;else cout Total days is: days endl;return 0;【案例2-16】计算数的阶乘do-while循环语句#include

17、using namespace std;int main() long limits; cout limits; cout Factorial numbers of 0 is 1endl; cout Factorial numbers of 1 is 1endl; long fac=1, i=1; do/使用do-while循环 fac *= +i; cout Factorial numbers of i is facendl; while (fac limits); return 0;【案例2-17】计算数的阶乘for循环#include using namespace std;int ma

18、in() long limits; cout limits; cout Factorial numbers of 0 is 1endl; cout Factorial numbers of 1 is 1endl; long fac=1;for(int i=2;fac=limits;i+)/使用for 循环 fac *= i; cout Factorial numbers of i is facendl;return 0;【案例2-18】筛选素数步长为2的for循环#include #include using namespace std;int main()long n;cout n;if (

19、n 2) cout n is not prime. endl;else if (n 4) cout n is prime. endl;else if (n%2 = 0) cout n = 2* n/2 endl;elsefor (int i=3; i = n/2; i += 2)/步长为2 if (n%i = 0) cout n = i * n/i endl; exit(0); cout n is prime. endl;return 0;【案例2-19】输出120之间的偶数continue语句#include using namespace std; int main() coutThe e

20、ven numbers are as follows:endl; for(int i=0; i=20; i+) if(i%2) continue; /根据条件使用continue结束本次循环cout i ; return 0; 【案例2-20】统计输入整数的个数并求和exit()函数#include #include using namespace std; int main() int sum=0,num=0,m; coutPlease input integers (0:end):m; num+; sum+=m; if(m=0) coutEntered numbers:num intege

21、rs.n;coutThe sum is:sumendl;exit(0);/ 使用exit()函数终止程序 while(1);return 0; 【案例2-21】“剪刀、石头、布”游戏枚举类型#include using namespace std;enum Choice ROCK, CLOTH, SCISS;/声明枚举类型Choiceenum Winner Play1, Play2, Tie; /声明枚举类型Winnerint main()int n; Choice cho1, cho2; Winner winner;cout Choose rock (0), cloth (1), or Sc

22、iss (2): endl;cout n;cho1 = Choice(n);cout n;cho2 = Choice(n);if (cho1 = cho2) winner = Tie;else if (cho1 = ROCK)if (cho2 = CLOTH) winner = Play2;else winner = Play1;else if (cho1 = CLOTH)if (cho2 = SCISS) winner = Play2;else winner = Play1;else if (cho2 = ROCK) winner = Play2;else winner = Play1;if

23、 (winner = Tie) cout tTied!n;else if (winner = Play1) cout tPlayer No. 1 wins. endl;else cout tPlayer No. 2 wins. endl;return 0;【案例2-22】简单的学生信息类型结构体#include #include using namespace std;struct student /学生信息结构体 int num; char name20; char gender; int age; stu1=1001,Zhang San,M,19;int main()student stu

24、2=1002,Li Si,M,20;/声明结构体变量并初始化student stu3=1003,Wang Hong,F,22; /声明结构体变量并初始化 coutsetw(7)stu1.numsetw(20)stu1.namesetw(3)stu1.gendersetw(3)stu1.ageendl;coutsetw(7)stu2.numsetw(20)stu2.namesetw(3)stu2.gendersetw(3)stu2.ageendl;coutsetw(7)stu3.numsetw(20)stu3.namesetw(3)stu3.gendersetw(3)stu3.ageendl;r

25、eturn 0;【案例2-23】综合案例百钱买百鸡问题#includeusing namespace std;int main() int n=100; cout鸡公 鸡母 鸡雏endl; /i表示鸡公,j表示鸡母,k表示鸡雏 for ( int i = 1; i = n; i+ ) for ( int j = 1; j = n; j+ ) for( int k = 1; k = n; k+ ) if( n = 5 * i + 3 * j + k / 3 ) & ( k % 3 = 0 ) & ( n = i + j + k ) cout i j k endl; return 0;#inclu

26、deint main() int x,y,z,j=0; printf(Folleing are possible plans to buy 100 fowls with 100 Yuan.n); for(x=0;x=20;x+) /外层循环控制鸡翁数 for(y=0;y=33;y+) /内层循环控制鸡母数y在033变化 z=100-x-y; /内外层循环控制下,鸡雏数z的值受x,y的值的制约 if(z%3=0&5*x+3*y+z/3=100) /验证取z值的合理性及得到一组解的合理性 printf(%2d:cock=%2d hen=%2d chicken=%2dn,+j,x,y,z); 【案例

27、3-1】编写输出专用函数无参函数#includeusing namespace std;void DispMessage(void) /定义无参函数 coutThis is a Message!endl; int main() DispMessage(); /调用无参函数DispMessage return 0;【案例3-2】编写求和函数有参函数#includeusing namespace std;double add(double x,double y) /定义有参函数 double z; z=x+y; return(z); int main() double a=0.5, b=1.0;

28、coutadd(a,b)=add(a,b)endl; /调用有参函数add() return 0;【案例3-3】编写求和函数函数的不同调用形式#includeusing namespace std;double add(double x,double y) /函数的定义,其有返回值 double z; z=x+y; coutx+y=zendl; return(z);int main() double a=0.5,b=1.0;/以不同参数形式调用函数add() coutadd(1.5,2.5)=add(1.5,2.5)endl; coutadd(a,b)=add(a,b)endl; coutad

29、d(2*a,a+b)=add(2*a,a+b)endl; double c=2*add(a,b); /以表达式方式调用函数add() coutc=cendl; add(2*a,b); /以语句方式调用函数add() cout add(a, add(a,b)=add(a, add(a,b)endl; /以函数参数形式调用函数add() return 0;【案例3-4】编写符号函数函数的返回值#includeusing namespace std;int sgn(double x) /定义符号函数sgn(),其返回值为int类型 if (x0) return(1); /返回出口1 if (x0)

30、return(-1); /返回出口2 return(0); /返回出口3int main() double x; for (int i=0;i=2;i+) coutx; coutsgn(x)=sgn(x)endl; return 0;【案例3-5】编写最值函数函数原型声明#includeusing namespace std;/函数原型声明语句也可以在这里int main() float max(float,float); /max()函数原型声明语句 float a,b,Max; /变量声明语句 couta;/输入参数a coutb; /输入参数b Max=max(a,b); /调用max(

31、)函数 coutmax(a,b)=Maxy)?x:y; return(z); /返回值类型为浮点型 【案例3-6】值传递和引用传递的区别#include using namespace std;void fun(int,int&); /函数参数一个为值传递,一个引用传递int main() int a = 22, b = 44; cout Initial a = a , b = b endl; fun(a,b); cout After fun(a,b), a = a , b = b endl; fun(2*a-3,b); cout After fun(2*a-3,b), a = a , b =

32、 b endl; return 0;void fun(int x, int& y) x = 88; y = 99;【案例3-7】编写最值函数内联函数#includeusing namespace std;inline int max(int x,int y) /使用inline关键字声明max()为内联函数 return xy?x:y; int main() int a=3,b=5,c; c=max(a,b); coutmax(a,b)=cendl; coutmax(15,11)=max(15,11)endl; return 0;【案例3-8】计算圆的面积和周长函数通过引用返回多于1个的数值#include using namespace std;void ComCircle(double&, double&, double); /函数的原型声明int main() double r, a, c; cout r; ComCircle(a, c, r); cout The area = a , and the circumference = c endl; return 0;void ComCircle(double& area, double& circ

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

当前位置:首页 > 教育专区 > 小学资料

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