c++教材课后题答案

上传人:liy****000 文档编号:116667398 上传时间:2019-11-17 格式:DOCX 页数:68 大小:57.93KB
返回 下载 相关 举报
c++教材课后题答案_第1页
第1页 / 共68页
c++教材课后题答案_第2页
第2页 / 共68页
c++教材课后题答案_第3页
第3页 / 共68页
c++教材课后题答案_第4页
第4页 / 共68页
c++教材课后题答案_第5页
第5页 / 共68页
点击查看更多>>
资源描述

《c++教材课后题答案》由会员分享,可在线阅读,更多相关《c++教材课后题答案(68页珍藏版)》请在金锄头文库上搜索。

1、习题11.1 书写一个简单的C+程序,从而理解C+程序的结构及特点。该程序只由一个主函数组成,主函数的函数体中只包含一个语句:coutThis is a C+ program.;【1.1答】#include void main()cout This is a C+ program.;习题22.1 写出C+标识符的命名规则,写出5个合法的标识符和5个不合法的标识符。2.2 下列符号哪些是C+标识符?哪些不是,为什么?2m a+c int i*j mm data n-a m22 x -n a_1【2.2答】mm data m22 x a_1 是C+标识符,int是是C+标识符也是关键字2.3 指出

2、下列合法的常量,并说明其类型。 65538 1.3 2.1E4 7L 9uL 9 a 123u【2.3答】65538 整型常量1.3 double型常量2.1E4 double型常量7L 长整型常量9uL 无符号长整型常量9 字符型常量a 字符型常量123u 无符号整型常量2.4 写出符合C+规则的int、double、char和字符串4种类型的常量各2个。2.5 说明字符常量和字符串常量有何区别?2.6 下列变量的定义是否合法?为什么? INT i,j,k; char c1,c2; int a,b;float f,b; unsigned int m char: a1,a2,a3; unsig

3、ned float x,y;【2.6答】和合法。的INT要小写;少分号;char后多了冒号;数据类型错误2.7 将下列数学表示式写成C+表达式。(1) (2)(3) (4)【2.7答】(1)(2*x*x+3*y*y)/(x-y)(2)(-b+sqrt(b*b-4*a*c)/(2*a) 和 (-b-sqrt(b*b-4*a*c)/(2*a)或 (-b+sqrt(b*b-4*a*c)/2/a 和 (-b-sqrt(b*b-4*a*c)/2/a(3)1+(1+a/b)/(1-(a/c)(4)x/sqrt(fabs(x*x*x+y*y*y+z*z*z)2.8 已有如下变量定义,求下列表达式的值。dou

4、ble x=1.2,y=8.5;int a=3;x+a%3*(int)(x+y)int a=2,b=3;double x=3.5,y=2.5;(double)(a+b)/2+(int)x%(int)yint x=4,y=8;(+x)*(-y)int e=1,f=4,g=2;double m=10.5,n=4.0,k;k=(e+f)/g+sqrt(n)*1.2/g+mdouble x=2.5,y=4.7;int a=7;x+a%3*(int)(x+y)%2/4int a,b;int x;x=(a=2,b=5,a+,b+,a+b)【2.8答】 1.2 3.5 35 13.7 2.5 9 1.2#i

5、nclude void main() double x=1.2,y=8.5; int a=3; cout(x+a%3*(int)(x+y)endl; 3.5#include void main() int a=2,b=3; double x=3.5,y=2.5; cout(double)(a+b)/2+(int)x%(int)y)endl; 35#include void main() int x=4,y=8; cout(+x)*(-y)endl; 13.7#include #include void main() int e=1,f=4,g=2; double m=10.5,n=4.0,k;

6、 k=(e+f)/g+sqrt(n)*1.2/g+m; coutkendl; 2.5#include void main() double x=2.5,y=4.7; int a=7; cout(x+a%3*(int)(x+y)%2/4)endl; 9#include void main() int a,b; int x; x=(a=2,b=5,a+,b+,a+b); coutxst;d=s*t;coutd=dendl;3.2 编写程序求圆柱体的表面积和体积。程序运行时输入圆柱的半径和高,输出圆柱的表面积和体积。#includeconst double PI=3.1415926;void mai

7、n() double r,h,areaf,volume; cinrh; areaf=2.*PI*r*h+PI*r*r*2.; volume=PI*r*r*h; coutarea=areafendl; coutvolume=volumeendl;3.3 输入梯形的上边长、下边长及高度,计算梯形的面积。#includeconst double PI=3.1415926;void main()double d1,d2,h,area;cind1d2h;area=(d1+d2)*h/2.;coutarea=areaendl;3.4 编程求输入的两个字符中的最小者。#includevoid main()c

8、har c1,c2;cinc1c2;coutc1=c1tc2=c2c2) coutmin=c2endl;else coutmin=c1endl;3.5 编程求输入的三个整数中的最大值。#includevoid main() int a,b,c,max; cinabc; max=a; if(bmax)max=b; if(cmax)max=c; coutmax=maxendl;3.6 编写程序:输入一个整数,若为0或正数时,显示:is positive;为负数时,显示:is negative。#includevoid main() int a; cina; if(a=0) couta is pos

9、itiveendl; else couta is negative=5000,八折。#include#includevoid main() double b; cinb; switch(int(b)/1000) case 0:coutm=bendl;break; case 1:coutm=b*0.95endl;break; case 2:coutm=b*0.90endl;break; case 3: case 4: coutm=b*0.85endl;break; case 5: default :coutm=b*0.80endl; 3.9 编一程序,将输入数据归入某一类别,输出其类别。数据分为

10、四类:小于10、1099、100999、1000以上。例如,输入358时,显示“358 is 100 to 999”。#include#includevoid main() int x; cinx; if(x10)coutx is littler then 10=10&x=99)coutx is 10 to 99=100&x=999)coutx is 100 to 999endl; if(x=1000)coutx is equal 10001000)coutx is bigger then 1000endl;3.10 编一程序,输入字符a时显示America,b时显示Britain,c时显示China,d时显示Denmark,其它字符时显示Harland。#include#includevoid main() char c; cinc; if(c=A&c=Z)c=c+32; switch(c) case a:coutAmericaendl;break; case b:coutBritainendl;break;

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

当前位置:首页 > 资格认证/考试 > 其它考试类文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号