《C++ Primer Plus》第三章习题与参考答案

1,内容选自《C++ Primer Plus》(第6版)中文版,2017年1月河北第21次印刷版本
2,文章系笔者学习笔记,若有错误,欢迎指正
3,如有雷同,纯属巧合

3.6 复习题

1.为什么c++有多种整形?

有多种整型类型,可以根据特定需求选择最合适的类型。例如,可以使用short来存储空格,使用long来确保存储容量,也可以寻找可提高特定计算的速度的类型。

2.声明与下述描述相符的变量。
a. short整数,值为80
b. unsigned int整数,值为42110
c. 值为3000000000的整数

a.short rbis=80;
b.unsigned int q=42110;
c.unsigned long ants=3000000000; or long long ants=3000000000;

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

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

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

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

5.下面两条c++语句是否等价?
char grade =45;
char grade = ‘A’;

这两条语句并不真正等价,虽然对于某些系统来说,它们是等效的。最重要的是,只有在使用ASCII码的系统上,第一条语句才将得分设置为字母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值将是20亿,即有10位数。由于double提供了至少13位有效数字,因而不需要进行任何舍入。long long类型可提供19位有效数字,超过了double保证的13位有效数字。

8.下列c++表达式的结果分别是多少?
a.89+2
b.6
3/4
c.3/46
d.6.0
3/4
e.15%4

a:74
b:4
c:0
d:4.5
e:3

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

下面的代码都可用于完成第一项任务:
int pos=(int)x1+(int)x2;
int pos=int(x1)+int(x2);
2,要将它们作为double类型相加,再进行转换,可采取下述方式之一:
int pos=(int)(x1+x2);
int pos=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

3.7 编程练习

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

#include <iostream>
using namespace std;

const double inch_per_feet = 12.0;

int main()
{
    int inch;
    cout << "Enter the height in inch:______\b\b\b\b\b\b";
    cin >> inch;
    cout << "the height is " << inch / inch_per_feet << " feet" << endl;
    return 0;
}

2.编写一个程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index ,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(一英尺为十二英寸),并将以英寸为单位的身高身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI-体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

#include <iostream>
#include <math.h>
using namespace std;

const double inches_per_foot = 12.0;
const double metres_per_inch = 0.0254;
const double pounds_per_kilogram = 2.2;

int main()
{
    double foot, inch, pound;
    cout << "Enter the height of foot:" << endl;
    cin >> foot;
    cout << "Enter the height of inch:" << endl;
    cin >> inch;
    cout << "Enter the height of pound:" << endl;
    cin >> pound;
    double height = (foot * inches_per_foot + inch) * metres_per_inch;
    double weight = pound / pounds_per_kilogram;
    double BMI = weight / pow(height, 2);
    cout << "the BMI is " << BMI << endl;
    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;

const double minute_per_degree = 60;
const double second_per_minute = 60;

int main()
{
    double degrees, minutes, seconds;
    cout << "Enter a latitude in degrees,minutes,and seconds:" << endl;
    cout << "First,enter the degrees:" << endl;
    cin >> degrees;
    cout << "Next,enter the minutes of arc:" << endl;
    cin >> minutes;
    cout << "Finally,enter the seconds of arc:" << endl;
    cin >> seconds;
    double result_degrees = degrees + minutes / minute_per_degree + seconds / (minute_per_degree * second_per_minute);
    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds"
         << " = " << result_degrees << " degrees." << endl;
    return 0;
}

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

#include <iostream>
using namespace std;

const int hours_per_day = 24;
const int minutes_per_hour = 60;
const int seconds_per_minute = 60;

int main()
{
    long long input_seconds;
    cout << "Enter the number of seconds:" << endl;
    cin >> input_seconds;
    int days = input_seconds / (seconds_per_minute * minutes_per_hour * hours_per_day);
    int hours = input_seconds % (seconds_per_minute * minutes_per_hour * hours_per_day) / (seconds_per_minute * minutes_per_hour);
    int minutes = input_seconds % (seconds_per_minute * minutes_per_hour * hours_per_day) % (seconds_per_minute * minutes_per_hour) / seconds_per_minute;
    int seconds = input_seconds % (seconds_per_minute * minutes_per_hour * hours_per_day) % (seconds_per_minute * minutes_per_hour) % seconds_per_minute;
    cout << input_seconds << " seconds = " << days << " days, " << hours << " hours, "
         << minutes << " minutes, " << seconds << " seconds" << endl;
    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 world_popu;
    cout << "Enter the world's population:" << endl;
    cin >> world_popu;
    long long us_popu;
    cout << "Enter the population of the US:" << endl;
    cin >> us_popu;
    double percent = double(us_popu) / double(world_popu) * 100;
    cout << "The population of the US is " << percent << "% of the world population." << endl;
    return 0;
}

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

#include <iostream>
using namespace std;

int main()
{
    double mileage_mile;
    cout << "Enter the driving mileage(mile):" << endl;
    cin >> mileage_mile;
    double gasoline_gallon;
    cout << "Enter the gasoline consumption(gallon):" << endl;
    cin >> gasoline_gallon;
    double mile_consum_gallon = mileage_mile / gasoline_gallon;
    cout << "The mileage per gallon:" << mile_consum_gallon << endl;
    // european style
    double mileage_kilometre;
    cout << "Enter the driving mileage(kilometre):" << endl;
    cin >> mileage_kilometre;
    double gasoline_liter;
    cout << "Enter the gasoline consumption(liter):" << endl;
    cin >> gasoline_liter;
    double kilometre_consum_liter = 100 * gasoline_liter / mileage_kilometre;
    cout << "The mileage per 100 kilometre:" << kilometre_consum_liter << endl;
    system("PAUSE");
    return 0;
}

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

#include <iostream>
using namespace std;

int main()
{
    double fuel_per_km;
    cout << "Fuel consumption of car(Fuel consumption per 100 km(L)):" << endl;
    cin >> fuel_per_km;
    double fuel_per_gallon = 62.14 / (fuel_per_km / 3.875);
    cout << "The mileage per gallon:" << fuel_per_gallon << endl;
    system("PAUSE");
    return 0;
}
发布了8 篇原创文章 · 获赞 1 · 访问量 688

猜你喜欢

转载自blog.csdn.net/Heisenberg_Li/article/details/102499449