《C++ Primer Plus》第三章课后题

本文详细介绍了C++中的整型类型选择、防止溢出、常量区别、类型转换、字符表示、运算符使用、单位转换、用户输入处理和数学运算等内容,包括编程练习实例。
摘要由CSDN通过智能技术生成

复习题

1.为什么C++有多种整型?

有多种整数类型,可以根据特定需求选择最合适的类型。

2.声明与下述描述相符的变量

a.short整数,值为80

b.unsigned int整数,值为42110

c.值为3000000000的整数

short a=80;

unsigned int b=42110;

long long c=3000000000;

3.C++提供了什么措施来防止超出整型的范围?

C++没有提供自动防止超出整型限制的功能,可以使用头文件climits来确定限制情况。

4.33L和33之间有什么区别?

常量33L的类型为long,常量33的类型为int。

5.下面两条C++语句是否等价?

char grade=65;

char grade='A';

并不真正等价。只有在使用ASCII码的系统上,第一条语句才将grade设置为A,而第二个语句还可用于使用其他编码的系统。65是一个int常量,而'A'是char常量。

6.如何使用C++来找出编码88表示的字符?指出至少两种方法。

char c=88;cout<<c<<endl;

cout.put(char(88));

cout<<char (88)<<endl;

cout<<(char) 88<<endl;

7.将long值赋给float变量会导致舍入误差,将long值赋给double变量呢?将long long赋给double呢?

这个问题的答案取决于这两个类型的长度。如果long为4个字节,则没有损失。因为最大的long值有10位数,而double至少13位有效位,因而不需要任何舍入。long long有19位有效数字,超过了double保证的13位有效位。

8.下列C++表达式的结果分别是多少?

a.8*9+2

b.6*3/4

c.3/4*6

d.6.0*3/4

e.15%4

a. 8*9+2=72+2=74

b. 6*3/4=18/4=4

c. 3/4*6=0*6=0

d. 6.0*3/4=18.0/4=4.5

e. 15%4=3

9.假设x1和x2是两个double变量,您要将它们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的C++语句。如果要将它们作为double值相加并转换为int呢?

int a = (int) x1 + (int) x2;

int a = int(x1) + int(x2);

int b = (int) (x1+x2);

int b = int(x1+x2);

10.下面每条语句声明的变量都是什么类型?

a. auto cars = 15

b. auto iou = 150.37f

c. auto level = 'B'

d. auto crat = U'/U00002155'

e. auto fract = 8.25f/2.5

a. int

b. float

c. char

d. char32_t

e. double

编程练习

1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为厘米),然后将身高转换为米和厘米。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

#include <iostream>
using namespace std;

int main(){
	int h=0;
    const float hund=100.0;
    cout<<"请输入身高(厘米):___\b\b\b";
    cin>>h;
    cout<<"身高"<<h<<"厘米="<<h/hund<<"米"<<endl;
    return 0;
 }

2.编写一个小程序,要求以几米几厘米的方式输入其身高,并以斤为单位输入其体重。(使用3个变量乘存储这些信息。)该程序报告其 BMI (Body Mass Index,体重指数)。为了计算 BMI:将身高转换为以米为单位的身高,然后,将以斤为单位的体重转换为以千克为单位的体重。最后,计算相应的BMI——体重(千克)除以身高(米)的平方,用符号常量表示各种转换因子。

#include <iostream>
using namespace std;

int main(){
	int m,cm,w;
    const float half=0.5;
    const float percen=0.01;
    cout<<"你的身高是:(米)";
    cin>>m;
    cout<<"(厘米)";
    cin>>cm;
    cout<<"你的体重是(斤):";
    cin>>w;
    cout<<"你的BMI是:"<<w*half/((m+percen*cm)*(m+percen*cm));
    return 0;
 }

3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分为60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行时的情况:

Enter a latitude in degrees, minutes, and seconds:

First, enter the degrees:37

Next, enter the minutes of arc:51

Finally, enter the seconds of arc:19

37 degrees, 51 minutes, 19 seconds =37.8553 degrees

#include <iostream>
using namespace std;

int main(){
	int degree,minute,second;
    const float sixty=60.0;
    cout<<"Enter a latitude in degrees, minutes, and seconds:"<<endl;
    cout<<"First, enter the degrees:";
    cin>>degree;
    cout<<"Next, enter the minutes of arc:";
    cin>>minute;
    cout<<"Finally, enter the seconds of arc:";
    cin>>second;
    cout<<degree<<" degrees,"<<minute<<" minutes,"<<second<<" seconds="<<degree+minute/sixty+second/(sixty*sixty)<<" degrees";
    return 0;
 }

4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒,该程序的输出应与下面类似:

Enter the number of seconds:31600000

31600000 seconds = 365 days, 17 hours,46 mimutes, 40 secords.

#include <iostream>
using namespace std;

int main(){
	long long second;
    const int hour=24;
    const int minute=60;
    const int seconds=60;
    cout<<"Enter the number of seconds:";
    cin>>second;
    int day=second/(seconds*minute*hour);
    int h=(second-(seconds*minute*hour)*day)/(seconds*minute);
    int m=(second-(seconds*minute*hour)*day-(seconds*minute)*h)/seconds;
    int s=(second-(seconds*minute*hour)*day-(seconds*minute)*h-seconds*m);
    cout<<second<<" seconds="<<day<<" days,"
    	<<h<<" hours,"
        <<m<<" minutes,"
        <<s<<" seconds";
    return 0;
 }

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比,该程序的输出应与下面类似:

Enter the world's population:6898758899

Enter the population of the US: 310783781

the population of the US is 4.50492% of the world population.

#include <iostream>
using namespace std;

int main(){
	long long pop,uspop;
    cout<<"Enter the world's population:";
    cin>>pop;
    cout<<"Enter the population of the US:";
    cin>>uspop;
    cout<<"the population of the US is "<<(1.0*uspop)/pop*100<<"% of the world population.";
    return 0;
 }

6.编写一个程序,要求用户编入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程,如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果———即每100公里的耗油量(升)

#include <iostream>
using namespace std;

int main(){
    double dis,pe;
    cout<<"请输入里程(英里):";
    cin>>dis;
    cout<<"请输入耗油量(加仑):";
    cin>>pe;
    cout<<dis/pe<<"英里/加仑";
    return 0;
 }
#include <iostream>
using namespace std;

int main(){
    double dis,pe;
    cout<<"请输入里程(公里):";
    cin>>dis;
    cout<<"请输入耗油量(升):";
    cin>>pe;
    cout<<pe/dis*100<<"升/百公里";
    return 0;
 }

7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每 100公里消耗的汽油量(升)),然后将其转接为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,I加仑等于3.875 升。因此,19mpg大约合12.41/100km,27mpg 大约合 8.71/100km

#include <iostream>
using namespace std;

int main(){
    double dp;
    cout<<"请输入汽车耗油量(升/百公里):";
    cin>>dp;
    cout<<dp<<"升/百公里="<<62.14/(dp/3.875)<<"英里/加仑";
    return 0;
 }
  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值