CPrimerPlus习题及答案第
2023/12/23 来源:不详 浏览次数:次看白癜风哪家医院好 https://mjbk.familydoctor.com.cn/bjbdfyy_ks811/
习题选自:C++PrimerPlus(第六版)内容仅供参考,如有错误,欢迎指正!
c++的基本类型分为两组:一组由存储为整数的值组成,另外一组由存储为浮点格式的值组成。整数之间通过存储时使用的内存量及有无符号来区分。
short至少16位
int至少与short一样长
long至少32位,且至少与int一样长
longlong至少64位,且至少与long一样长
复习题1.为什么c++有多种整形?有多种整形类型,可以根据特定需求选择最合适的类型。例如,可以使用short来存储空格,使用long来确保存储容量,也可以寻找可提高特定计算的速度的类型。
2.声明与下述描述相符的变量。a.short整数,值为80b.unsignedint整数,值为c.值为的整数
shorta=80;//orshortinta=80;unsignedintb=;//orunsignedb=;unsignedlonglongc=;//orunsignedlongc=;//(注意:不要指望int变量能够存储)3.c++提供了什么措施来防止超出整形的范围?
c++没有提供自动防止超出整型限制的功能,可以使用头文件climits来确定限制情况。
4.33L与33之间有什么区别?常量33L的类型为long,常量33类型为int。
5.下面两条c++语句是否等价?chargrade=45;chargrade=A;这两条语句并不真正等价,虽然对于某些系统来说,他们是等效的。最重要的是,只有在使用ASCII码的系统上,第一条语句才将得分设置为字母A,而第二条语句还可用于使用其他编码的系统。其次,65是一个int常量,而‘A’是一个char常量。
6.如何使用c++来找出编码88表示的字符?指出至少两种方法。//method1charc=88;coutcendl;//method2cout.put(char(88));//method3coutchar(88)endl;//new-styletypecastvaluetocharcout(char)88endl;//old-styletypecastvaluetochar7.将long赋值给float变量会导致舍入误差,将long值赋给double变量呢?将longlong值赋给double变量呢?
这个问题取决于这两个类型的长度。如果long为4个字节,则没有损失。因为最大的long值是20亿,及有10位数。由于double提供至少13位有效数字,因而不需要进行任何舍入。longlong类型可提供19位有效数字,超过了double保证的13位有效数字。
8.下列c++表达式的结果分别是多少?a.89+2b.63/4c.3/46d.6.03/4e.15%4
a=74;b=4;c=0;d=4.5;e=3
9.假设x1和x2是两个double变量,你要将他们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的c++语句。如果要将他们作为double值相加并转换为int呢?inta=(int)x1+(int)x2;inta=int(x1)+int(x2);
要将他们作为double类型相加,在进行转换,可采取下述方式之一:
inta=(int)(x1+x2);inta=int(x1+x2);10.下面每一条语句的变量都是什么类型?
a.autocars=15;b.autoiou=.37f;c.autolevel=B;d.autocrat=U/U;e.autofract=8.25f/2.5;
a.intb.floatc.chard.char32_te.double
编程练习1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺,该程序使用下划线字符来指示输入位置。另外,使用一个const符号来表示转换因子。#includeiostreamconstdoubleInch_per_feet=12.0;usingnamespacestd;intmain(void){cout"Pleaseenteryourheight:_____\b\b\b\b\b";doubleht_inch;cinht_inch;doubleht_feet=ht_inch/Inch_per_feet;cout"Yourheightis:"ht_feet"feetsendl";return0;}2.编写一个程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(BodyMassIndex,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(一英尺为十二英寸),并将以英寸为单位的身高身高转换为以米为单位的身高(1英寸=0.米)。然后,将以磅为单位的体重转换为千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI-体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#includeiostreamusingnamespacestd;constdoubleInch_per_feet=12.0;constdoubleMeter_per_inch=0.;constdoublePound_per_kilogram=2.2;intmain(){cout"Enteryourheightoffeet:";doubleht_feet;cinht_feet;cout"Enteryourheightofinch:";doubleht_inch;cinht_inch;doubleht_meter=(ht_feet*Inch_per_feet+ht_inch)*Meter_per_inch;cout"Enteryourweightinpound:";doublewt_pound;cinwt_pound;doublewt_kilogram=wt_pound/Pound_per_kilogram;doubleBMI=wt_kilogram/ht_meter/ht_meter;cout"BMI:"BMIendl;system("pause");return0;}3.编写一个程序,要求用户以度分秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行时的情况:
Enteralatitudeindegrees,minutes,andseconds:First,enterthedegrees:37Next,entertheminutesofarc:51Finally,enterthesecondsofarc:degrees,51minutes,19seconds=37.degrees
#includeiostreamusingnamespacestd;constdoubleMinute_per_degree=60.0;constdoubleSecond_per_minute=60.0;intmain(){cout"Enteralatitudeindegrees,minutes,andseconds:"endl;cout"First,enterthedegrees:";intdegrees;cindegrees;cout"Next,entertheminutesofarc:";intminutes;cinminutes;cout"Finally,enterthesecondsofarc:";intseconds;cinseconds;doubleDegree=degrees+minutes/Minute_per_degree+seconds/Second_per_minute/Minute_per_degree;coutdegrees"degrees,"minutes"minutes,"seconds"seconds="Degree"degrees";system("pause");return0;}4.编写一个程序,要求用户以整数方式输入秒数(使用long或longlong变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enterthenumberofseconds:00seconds=days,17hours,46minutes,40seconds
#includeiostreamusingnamespacestd;constintSec_per_min=60;constintMin_per_hour=60;constintHour_per_day=24;intmain(){cout"Enterthenumberofseconds:";longlongtotal_sec;cintotal_sec;intsec=total_sec%Sec_per_min;inttotal_min=total_sec/Sec_per_min;intmin=total_min%Min_per_hour;inttotal_hour=total_min/Min_per_hour;inthour=total_hour%Hour_per_day;intday=total_hour/Hour_per_day;couttotal_sec"seconds="day"days,"hour"hours,"min"minutes,"sec"seconds";system("pause");return0;}5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在longlong变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出与下面类似:
Entertheworldspopulation:EnterthepopulationoftheUS:ThepopulationoftheUSis4.%oftheworldpopulation.
#includeiostreamusingnamespacestd;constintSec_per_min=60;constintMin_per_hour=60;constintHour_per_day=24;intmain(){cout"Entertheworldspopulation:";longlongtW_population;cintW_population;cout"EnterthepopulationoftheUS:";longlongUS_population;cinUS_population;cout"ThepopulationoftheUsis"double(US_population)/double(tW_population)*"%oftheworldpopulation";system("pause");return0;}6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果-即每公里的耗油量(升)。
#includeiostreamusingnamespacestd;intmain(){cout"Pleaseenterthedistance(miles):";doubledistance;cindistance;cout"Pleaseentervolumeofgasoline(gallon):";doublevolume;cinvolume;cout"Youcanrun"distance/volume"milespergallon"endl;system("pause");return0;}7.编写一个程序,要求每个用户按欧洲风格输入汽车的耗油量(每公里消耗汽油量(升)),然后将其转换为美国风格的耗油量-每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。公里等于62.14英里,1加仑等于3.升。因此,19mpg大约合12.41/km,mpg大约合8.71/km。
#includeiostreamusingnamespacestd;intmain(){cout"Pleaseenterfuelconsumptionperkm(UN):";doubleUN;cinUN;cout"Gallonspermile(US):"1/UN*62.14*3.;system("pause");return0;}