C++primer plus第四章编程练习

复习题

不熟悉的知识点:

枚举型变量:

用enum定义一个名为Response的类型,它包含Yes,No和Maybe等枚举量,其中Yes的值为1,No的值为0,Maybe的值为2。

定义枚举量时,其元素会从0开始赋值,用户也可以手动指定每一个元素的值,例如:

enum Response{No,Yes,Maybe};

或者:

enum Response {No = 0,Yes = 1,Maybe = 2};

指针与动态分配混合使用的练习:

Unsigned int size;

Cout<<”Enter a number:”;

Cin>>size;

Int *arr = new int[size];

Struct fish{
char kind[20];

Int weight;

Float length;

};

Fish *pf = new fish;

Cout<<”Enter the kind of fish:”;

Cin>>pf->kind;

编程练习

  1. 编写一个C++程序,如下述输出示例所示那样请求并显示信息:

#include<iostream>

using namespace std;

int main() {

  char first[20], last[20];

  char grade;

  int age;

  cout << "What is your first name?" << endl;

  cin.getline(first, 20);

  cout << "What is your last name?" << endl;

  cin.getline(last, 20);

  cout << "What letter grade do you deserve?" << endl;

  cin >> grade;

  cout << "What is your age?" << endl;

  cin >> age;

  cout << "Name:" << last << "," << first << endl;

  cout << "Grade:" << grade<<endl;

  cout << "Age:" << age;

  return 0;

}

  1. 修改程序清单4.4,使用C++string类而不是char数组

  #include<iostream>

#include<string>

using namespace std;

int main() {

  const int ArSize = 20;

  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";

  return 0;

}

  1. 编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是程序运行的情形:

#include<iostream>

#include<cstring>

using namespace std;

int main() {

  char first[10];

  char last[10];

  char name[20];

  cout << "Enter your first name:" << endl;

  cin.getline(first, 10);

  cout << "Enter your last name:" << endl;

  cin.getline(last, 10);

  strcpy_s(name, last);

  strcat_s(name, ",");

  strcat_s(name, first);

  cout << "Here's the information in a single string:" << name;

  return 0;

}

注:strcat strcpy在vs中编译出错,有  严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C4996 'strcpy': This function

因而改成strcat_s   strcpy_s

  1. 编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:

#include<iostream>

#include<string>

using namespace std;

int main() {

  string first;

  string last;

  string name;

  cout << "Enter your first name:" << endl;

  getline(cin, first);

  cout << "Enter your last name:" << endl;

  getline(cin, last);

  name = last + "," + first;

  cout << "Here's the information in a single string:" << name;

  return 0;

}

  1. 结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”,2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。

  #include<iostream>

#include<string>

using namespace std;

int main() {

  struct CandyBar

  {

      string name;

      float weight;

      int calorie;

  };

  CandyBar snack = { "Mocha Munch",2.3,350 };

  cout << "name :" << snack.name << endl;

  cout << "weight :" << snack.weight << endl;

  cout << "calorie :" << snack.calorie << endl;

  return 0;

}

  1. 结构CandyBar包含三个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。

 #include<iostream>

#include<string>

using namespace std;

int main() {

  struct CandyBar

  {

      string name;

      float weight;

      int calorie;

  };

  CandyBar snack[3] = { { "Mocha Munch",2.3,350 },{"asdjg",3.9,500},{"askjhd",4.9,600} };

  cout << "name :" << snack[0].name << endl;

  cout << "weight :" << snack[0].weight << endl;

cout << "calorie :" << snack[0].calorie << endl;

  cout << "name :" << snack[1].name << endl;

  cout << "weight :" << snack[1].weight << endl;

cout << "calorie :" << snack[1].calorie << endl;

  cout << "name :" << snack[2].name << endl;

  cout << "weight :" << snack[2].weight << endl;

cout << "calorie :" << snack[2].calorie << endl;

  return 0;

}

8.William Wingate从事比萨饼分析服务。对于每个披萨饼,它都需要记录下列信息:

披萨饼公司的名称,可以有多个单词组成。

披萨饼的直径。

披萨饼的重量。

请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

用动态存储分配内存。

 #include<iostream>

#include<string>

using namespace std;

int main() {

  struct Pizza

  {

      string corporation;

      float diameter;

      float weight;

  };

  Pizza *pizza = new Pizza;

  cout << "Enter the corporation" << endl;

  getline(cin, pizza->corporation);

  cout << "Enter the diameter" << endl;

  cin >> pizza->diameter;

  cout << "Enter the weight" << endl;

  cin >> pizza->weight;

  cout << "corporation :" << pizza->corporation << endl;

  cout << "diameter :" << pizza->diameter << endl;

  cout << "weight :" << pizza->weight << endl;

  delete pizza;

  return 0;

}

9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

 #include<iostream>

#include<string>

using namespace std;

int main() {

  struct CandyBar

  {

      string name;

      float weight;

      int calorie;

  };

  CandyBar *snack =new CandyBar ;

  cout << "Enter name" << endl;

  getline(cin, snack->name);

  cout << "Enter weight" << endl;

  cin >> snack->weight;

  cout << "Enter calorie" << endl;

  cin >> snack->calorie;

  cout << "name :" << snack->name << endl;

cout << "weight :" << snack->weight << endl;

  cout << "calorie :" << snack->calorie;

  delete snack;

  return 0;

}

10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。

 #include<iostream>

#include<array>

using namespace std;

int main() {

  array<float, 3> score;

  float average;

  cout << "Enter the first" << endl;

  cin >> score[0];

  cout << "Enter the second" << endl;

  cin >> score[1];

  cout << "Enter the third" << endl;

  cin >> score[2];

  cout << "first:" << score[0] << endl;

  cout << "second:" << score[1] << endl;

  cout << "third:" << score[2] << endl;

  cout << "average:" << (score[0] + score[1] + score[2]) / 3;

  return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值