神秘博客

C++ Primer Plus [第六版]第3章编程练习题答案

1

#include <iostream>

int main()
{
  using namespace std;
  const int ft_height = 12;
  int height;
  
  cout << "请输入你的身高[英寸]:__\b\b";
  cin >> height;
  cout << "你的身高为: " << height / ft_height << " 英尺 " << height % ft_height << " 英寸.\n";


  system("pause");
  return 0;
}

2

#include <iostream>
#include <cmath>

int main()
{
  using namespace std;

  int height_ft, height_in, weight_lb;
  
  cout << "请输入你的身高[英尺]: ";
  cin >> height_ft;
  cout << "请输入你的身高[英寸]: ";
  cin >> height_in;
  cout << "请输入你的体重[磅]: ";
  cin >> weight_lb;
  double bmi = (weight_lb / 2.2) / pow(((height_ft * 12 + height_in)*0.0254), 2);
  cout << "你的BMI为: " << bmi << endl;

  system("pause");
  return 0;

}

3

#include <iostream>

int main()
{
  using namespace std;

  int dimension_du, dimension_min, dimension_s;

  cout << "请以度, 分, 秒的方式输入一个维度.\n";
  cout << "请输入度: ";
  cin >> dimension_du;
  cout << "请输入分: ";
  cin >> dimension_min;
  cout << "请输入秒: ";
  cin >> dimension_s;
  cout << dimension_du << " 度 " << dimension_min << " 分 " << dimension_s << " 秒 = " << float(dimension_du + ((dimension_min + (dimension_s / 60.0)) / 60.0)) << " 度" << endl;

  system("pause");
  return 0;
}

4

#include <iostream>

int main()
{
  using namespace std;

  long long second;

  int days, hours, minutes, seconds;

  cout << "请输入秒数: ";
  cin >> second;
  days = int(second / 86400);
  hours = second % 86400 / 3600;
  minutes = second % 86400 % 3600 / 60;
  seconds = second % 86400 % 3600 % 60;
  cout << second << " 秒 = " << days << " 天 " << hours << " 时 " << minutes << " 分 " << seconds << " 秒" << endl;

  system("pause");
  return 0;
}

5

#include <iostream>

int main()
{
  using namespace std;

  long long Global_population, population;

  cout << "请输入全球人口总数: ";
  cin >> Global_population;
  cout << "请输入美国人口总数: ";
  cin >> population;
  cout << "美国人口总数占全球人口总数的比例为: " << double(population) / double(Global_population) * 100.0 << "%\n";

  system("pause");
  return 0;
}

6

#include <iostream>

int main()
{
  using namespace std;
  double mileage, petrol;

  cout << "请输入驱车里程[英里]: ";
  cin >> mileage;
  cout << "请输入消耗汽油量[加仑]: ";
  cin >> petrol;
  cout << "汽车耗油量为: " << mileage / petrol << " 英里/每加仑\n";

  system("pause");
  return 0;
}

7

#include <iostream>

int main()
{
  using namespace std;

  double petrol;

  cout << "请输入汽车的耗油量[每100公里消耗的汽油[升]]: ";
  cin >> petrol;
  double us_mileage = 62.14 / (petrol / 3.875);
  cout << petrol << " 升/100公里 = " << us_mileage << " 英里/每加仑\n";

  system("pause");
  return 0;
}

 

版权说明:
点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏