神秘博客

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

1

#include <iostream>

int main()
{
  using namespace std;

  cout << "姓名: Mr.Lei" << endl;
  cout << "地址: 广东省深圳市龙岗区" << endl;
  system("pause");
  return 0;
}

2

#include <iostream>

int main()
{
  using namespace std;
  long x;
  cout << "请输入距离:" << endl;
  cin >> x;
  cout << x << " = " << x * 220 << " 码" << endl;

  system("pause");
  return 0;
}

3

#include <iostream>

using namespace std;

void showa();
void showb();

int main()
{

  showa();
  showa();
  showb();
  showb();

  system("pause");
  return 0;
}

void showa()
{
  cout << "Three blind mice" << endl;
}

void showb()
{
  cout << "See how they run" << endl;
}

4

#include <iostream>

int main()
{
  using namespace std;

  int ages;

  cout << "请输入你的年龄: ";
  cin >> ages;
  cout << "你今年 " << ages << " 岁, 已经度过了 " << ages * 12 << " 个月." << endl;

  system("pause");
  return 0;
}

5

#include <iostream>

double convert(float n);

int main()
{
  using namespace std;

  float a;

  cout << "请输入温度[摄氏度]: ";
  cin >> a;
  cout << a << " 摄氏度 = " << convert(a) << " 华氏度" << endl;

  system("pause");
  return 0;
}

double convert(float n)
{
  return (1.8*n + 32.0);
}

6

#include <iostream>

double convert(double n);

int main()
{
  using namespace std;
  double years;
  
  cout << "请输入光年: ";
  cin >> years;
  cout << years << " 光年 = " << convert(years) << " 天文单位." << endl;

  system("pause");
  return 0;
}

double convert(double n)
{
  return (n * 63240);
}

7

#include <iostream>

using namespace std;

void convert(int n, int m);

int main()
{
  int hour;
  int min;

  cout << "请输入小时: ";
  cin >> hour;
  cout << "请输入分钟: ";
  cin >> min;
  convert(hour, min);

  system("pause");
  return 0;
}

void convert(int n, int m)
{
  cout << "时间: " << n << ":" << m << endl;
}

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏