神秘博客

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

1

#include <iostream>

int main()
{
  using namespace std;
  char first[20];
  char last[10];
  char deserve;
  int age;

  cout << "What is your first name?\n";
  cin.getline(first, 20);
  cout << "What is your last name?\n";
  cin >> last;
  cout << "What letter grade do you deserve?\n";
  cin >> deserve;
  cout << "What is your age?\n";
  cin >> age;
  cout << "name: " << last << ", " << first << endl;
  cout << "Grade: " << char(deserve + 1) << endl;
  cout << "Age: " << age << endl;

  system("pause");
  return 0;
}

2

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string name;
  string dessert;

  cout << "Enter your name:\n";
  getline(cin, name);
  cout << "Enter your favorite dessert:\n";
  getline(cin, dessert);
  cout << "I have some delicious " << dessert;
  cout << " for you, " << name << ".\n";
  
  system("pause");
  return 0;
}

3

#include <iostream>
#include <cstring>

int main()
{
  using namespace std;
  char first[20];
  char last[20];

  cout << "Enter your first name: ";
  cin.getline(first, 20);
  cout << "Enter your last name: ";
  cin.getline(last, 40);
  char name[40];
  strcpy_s(name, 20, last);
  strcat_s(name, 40, ", ");
  strcat_s(name, 40, first);
  cout << "Here's the information in a single string: " << name << endl;

  system("pause");
  return 0;
}

 

4

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string first;
  string last;

  cout << "Enter yout first name: ";
  getline(cin, first);
  cout << "Enter your last name: ";
  getline(cin, last);
  string name = last + ", " + first;
  cout << "Here's the information in a single string: " << name << endl;

  system("pause");
  return 0;
}

5

#include <iostream>
#include <string>

using namespace std;

struct CandyBar
{
  string sugarname;
  double weight;
  int calorie;
};

int main()
{
  CandyBar snack = { "Mocha Munch",2.3,350 };

  cout << snack.sugarname << " " << snack.weight << " " << snack.calorie << endl;

  system("pause");
  return 0;
}

6

#include <iostream>
#include <string>

using namespace std;

struct CandyBar
{
  string sugarname;
  double weight;
  int calorie;
};

int main()
{
  CandyBar snack[3] = 
  {
  { "Mocha Munch",2.3,350 },
  { "Mocha Munch",2.3,350 },
  { "Mocha Munch",2.3,350 }
  };

  cout << snack[0].sugarname << " " << snack[0].weight << " " << snack[0].calorie << endl;
  cout << snack[1].sugarname << " " << snack[1].weight << " " << snack[1].calorie << endl;
  cout << snack[2].sugarname << " " << snack[2].weight << " " << snack[2].calorie << endl;

  system("pause");
  return 0;
}

7

#include <iostream>
#include <string>

using namespace std;

struct pizza
{
  string company;
  double diameter;
  double weight;
};

int main()
{
  pizza Pizza;

  cout << "请输入披萨饼公司的名称: ";
  cin >> Pizza.company;
  cout << "请输入披萨饼的直径[cm]: ";
  cin >> Pizza.diameter;
  cout << "请输入披萨饼的重量[kg]: ";
  cin >> Pizza.weight;
  cout << "公司: " << Pizza.company << endl;
  cout << "直径: " << Pizza.diameter << " cm" << endl;
  cout << "重量: " << Pizza.weight << " kg" << endl;

  system("pause");
  return 0;
}

8

#include <iostream>
#include <string>

using namespace std;

struct pizza
{
  string company;
  double diameter;
  double weight;
};

int main()
{
  pizza *Pizza = new pizza;

  cout << "请输入披萨饼的直径[cm]: ";
  cin >> Pizza->diameter;
  cout << "请输入披萨饼公司的名称: ";
  cin >> Pizza->company;
  cout << "请输入披萨饼的重量[kg]: ";
  cin >> Pizza->weight;
  cout << "公司: " << Pizza->company << endl;
  cout << "直径: " << Pizza->diameter << " cm" << endl;
  cout << "重量: " << Pizza->weight << " kg" << endl;

  delete Pizza;
  system("pause");
  return 0;
}

9

#include <iostream>
#include <string>

using namespace std;

struct CandyBar
{
  string sugarname;
  double weight;
  int calorie;
};

int main()
{
  CandyBar *snack = new CandyBar[3];
  snack[0] = { "Mocha Munch",2.3,350 };
  snack[1] = { "Mocha Munch",2.3,350 };
  snack[2] = { "Mocha Munch",2.3,350 };

  cout << snack[0].sugarname << " " << snack[0].weight << " " << snack[0].calorie << endl;
  cout << snack[1].sugarname << " " << snack[1].weight << " " << snack[1].calorie << endl;
  cout << snack[2].sugarname << " " << snack[2].weight << " " << snack[2].calorie << endl;

  delete[]snack;
  system("pause");
  return 0;
}

10

#include <iostream>
#include <array>

int main()
{
  using namespace std;
  array<int, 3> second;

  cout << "请输入第一次跑40米的成绩[s]: ";
  cin >> second[0];
  cout << "请输入第二次跑40米的成绩[s]: ";
  cin >> second[1];
  cout << "请输入第三次跑40米的成绩[s]: ";
  cin >> second[2];
  double average = (second[0] + second[1] + second[2]) / 3.0;
  cout << "三次跑40米的平均成绩: " << average << " s\n";

  system("pause");
  return 0;
}

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏