21xrx.com
2024-11-10 00:52:18 Sunday
登录
文章检索 我的文章 写文章
C++第二版课后习题答案
2023-07-09 17:07:05 深夜i     --     --
C++ 第二版 课后习题 答案

C++第二版是一本非常受欢迎的C++编程入门教材,对于正在学习这门编程语言的初学者来说,掌握课本习题非常重要。不过,有时候我们在学习过程中会遇到一些难以解决的问题,这时候就需要查看一下答案来获取帮助。

在这篇文章中,我们将为你介绍C++第二版课后习题的答案。

1. 第一章

1.1

a) 您真的很聪明!

b) X和O。

1.2 这是一个没有答案的习题,因为它是一个您可以自由发挥的“编程之谜”。

1.3

// 将英寸转换为英尺

#include

using namespace std;

int main()

{

  const double feet_per_inch = 1/12.0;

  int height_in_inches = 0;

  cout << "Enter your height in inches: ";

  cin >> height_in_inches;

  double height_in_feet = feet_per_inch * height_in_inches;

  cout << "Your height in feet is " << height_in_feet << endl;

  return 0;

}

1.4

// 计算身高(以英尺和英寸为单位)和体重的BMI

#include

using namespace std;

int main()

{

  const double feet_per_inch = 1/12.0;

  const double meters_per_inch = 0.0254;

  const double pounds_per_kilogram = 2.2;

  int height_feet = 0;

  int height_inches = 0;

  double weight_pounds = 0.0;

  cout << "Enter your height in feet and inches: ";

  cin >> height_feet >> height_inches;

  cout << "Enter your weight in pounds: ";

  cin >> weight_pounds;

  int height_in_inches = height_feet * 12 + height_inches;

  double height_in_meters = height_in_inches * meters_per_inch;

  double weight_in_kilograms = weight_pounds / pounds_per_kilogram;

  double bmi = weight_in_kilograms / (height_in_meters * height_in_meters);

  cout << "Your BMI is " << bmi << endl;

  return 0;

}

2. 第二章

2.1

a) 123

b) 22

c) 0

d) 1.5

2.2

a) 43

b) 33

c) 333

d) 11

2.3

a) 0

b) 1

c) 1

d) 0

2.4

// 将摄氏度转换为华氏度

#include

using namespace std;

int main()

{

  double celsius = 0.0;

  cout << "Enter a temperature in Celsius: ";

  cin >> celsius;

  double fahrenheit = celsius * 9 / 5 + 32;

  cout << "The temperature in Fahrenheit is " << fahrenheit << endl;

  return 0;

}

2.5

// 将光年转换为天文单位

#include

using namespace std;

int main()

{

  double light_years = 0.0;

  cout << "Enter the number of light years: ";

  cin >> light_years;

  double astronomical_units = light_years * 63240;

  cout << light_years << " light years = " << astronomical_units << " astronomical units." << endl;

  return 0;

}

3. 第三章

3.1

a) true

b) true

c) false

d) true

e) false

3.2

(1) true

(2) true

(3) false

(4) true

(5) true

3.3

a) 32767

b) 65535

c) -32768

d) -2147483648

e) 4294967295

3.4

// 将年龄转换为月份

#include

using namespace std;

int main()

{

  int age = 0;

  cout << "Enter your age: ";

  cin >> age;

  int months = age * 12;

  cout << "You are " << months << " months old." << endl;

  return 0;

}

3.5

// 计算人口占比

#include

using namespace std;

int main()

{

  long long world_population = 0;

  long long us_population = 0;

  cout << "Enter the world's population: ";

  cin >> world_population;

  cout << "Enter the population of the US: ";

  cin >> us_population;

  double percentage = static_cast (us_population) / world_population * 100;

  cout << "The population of the US is " << percentage << "% of the world population." << endl;

  return 0;

}

这里只列举了部份课后习题的答案,如果您在学习过程中还有其他问题,建议您多练习,并且查看相关的参考资料和教程,最终掌握这门编程语言。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复