C++ Primer Plus章节编程练习(第四章)

第四章 复合类型

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

   What is your first name? Betty Sue

   What is your last name? Yewe

   What letter grade do you deserve? B

   What is your age? 22

   Name: Yewe, Betty Sue

   Grade: C

   Age: 22

  注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调整一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。

  分析:string类的基本操作

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main(){
 5     string firstName;
 6     string lastName;
 7     string name;
 8     char grade;
 9     int age;
10     cout << "What is your first name? ";
11     getline(cin, firstName);
12     cout << "What is your last name? ";
13     getline(cin, lastName);
14     cout << "What letter grade do you deserve? ";
15     cin >> grade;
16     cout << "What is your age? ";
17     cin >> age;
18     cout << endl;
19     name = lastName + "," + firstName;        //连接两个字符串 
20     grade += 1;        //向上调整字母 
21     cout << "Name: " << name << endl;
22     cout << "Grade: " << grade << endl;
23     cout << "Age: " << age << endl; 
24 }

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

   分析:getline()方法用来读取一整行输入(为什么不用cin)

   修改后:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main(){
 5     string name;
 6     string dessert;
 7     
 8     cout << "Enter your name:
";
 9     getline(cin, name);        //读取一整行
10     cout << "Enter your favorite dessert:
";
11     getline(cin, dessert); 
12     cout << "I have some delicious " << dessert;
13     cout << " for you, " << name << ".
";
14     return 0;
15 }

3、编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。

   分析:考察C风格字符串中的strcat(str1, str2)方法

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int main(){
 5     char firstName[20];
 6     char lastName[20];
 7     char name[20];
 8     
 9     cout << "Enter your first name: ";
10     cin >> firstName;
11     cout << "Enter your lastName: ";
12     cin >> lastName;
13     strcat(strcat(strcat(name, lastName), ", "), firstName);
14     
15     cout << "Here's the information in a single string: "<< name << endl;
16     return 0;
17 }

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

  分析:string类的基本操作,同编程练习1类似

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main(){
 5     string firstName;
 6     string lastName;
 7     string name;
 8     
 9     cout << "Enter your first name: ";
10     cin >> firstName;
11     cout << "Enter your lastName: ";
12     cin >> lastName;
13     name = lastName + ", " + firstName;
14     
15     cout << "Here's the information in a single string: "<< name << endl;
16     return 0;
17 }

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

  分析:考察结构体变量的声明、初始化和使用

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar{
 5         string brand;
 6         double weight;
 7         int calorie;
 8     };
 9 int main(){
10     CandyBar snack = {"Mocha Munch", 2.3, 350};
11     cout << "Brand: " << snack.brand << endl;
12     cout << "Weight: " << snack.weight << endl;
13     cout << "Calorie: " << snack.calorie << endl;
14     return 0;
15 }

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

    分析:考察结构体数组的声明、初始化和使用

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar{
 5         string brand;
 6         double weight;
 7         int calorie;
 8     };
 9 int main(){
10     CandyBar s[3] = {{"Mocha", 2.3, 350}, {"Candy first", 1.6, 200}, {"Candy second", 3.4, 400}};
11     for(int i=0; i<3; i++){
12         cout << "Brand: " << s[i].brand << endl;
13         cout << "Weight: " << s[i].weight << endl;
14         cout << "Calorie: " << s[i].calorie << endl;
15         cout << "
";
16     }
17     return 0;
18 }

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

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

  ~披萨饼的直径

  ~披萨饼的重量 

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

    分析:类似于编程练习5

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct pizza{
 5     string name;
 6     double diameter;
 7     double weight;
 8 };
 9 int main(){
10     pizza p;
11     cout << "Enter name: ";
12     getline(cin, p.name);
13     cout << "Enter diameter: ";
14     cin >> p.diameter;
15     cout << "Enter weight: ";
16     cin >> p.weight;
17     cout << endl;
18     cout << "Name: " << p.name << endl;
19     cout << "Diameter: " << p.diameter << endl;
20     cout << "Weight: " << p.weight << endl;
21     return 0;
22 }

8、完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司之前输入比萨饼的直径。 

    分析:考察用new创建动态结构

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct pizza{
 5     string name;
 6     double diameter;
 7     double weight;
 8 };
 9 int main(){
10     pizza * p = new pizza;        //动态创建结构体
11     cout << "Enter diameter: ";
12     cin >> p->diameter;        //第一种键入方法
13     cout << "Enter name: ";
14     cin >> p->name;
15     cout << "Enter weight: ";
16     cin >> (*p).weight;        //第二种键入方法
17     cout << endl;
18     cout << "Name: " << p->name << endl; 
19     cout << "Diameter: " << p->diameter << endl; 
20     cout << "Weight: " << p->weight << endl; 
21     return 0;
22 }

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

    分析:考察用new创建动态结构数组

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar{
 5         string brand;
 6         double weight;
 7         int calorie;
 8     };
 9 int main(){
10     CandyBar * candy = new CandyBar[3];        //用new动态分配内存 
11     
12     *candy = {"Mocha", 2.3, 350};                //初始化 
13     *(candy + 1) = {"Candy first", 1.6, 200};
14     *(candy + 2) = {"Candy second", 3.4, 400};
15     
16     
17     for(int i=0; i<3; i++){
18         cout << "Brand: " << candy[i].brand << endl;
19         cout << "Weight: " << candy[i].weight << endl;
20         cout << "Calorie: " << candy[i].calorie << endl;
21         cout << "
";
22     }
23     return 0;
24 }

  (由于Dev-c++不支持C++ 11导致编译不能通过,所以改用VS2017)

10、编写一个程序,让用户输入三次40码跑的成绩,并显示次数和平均成绩。请使用一个array对象来存储数据。

  分析:考察array对象的使用

 1 #include<iostream>
 2 #include<vector>
 3 #include<array>
 4 using namespace std;
 5 int main(){
 6     array<double, 3> grade;
 7     for(int i=0, i<3, i++){
 8         cout << "" << i << "次成绩: ";
 9         cin >> grade[i]; 
10     }
11     cout << endl;
12     cout << "平均成绩:" << (grade[0] + grade[1] + greade[3])/3;
13     return 0;
14 }

   (由于Dev-c++不支持C++ 11导致编译不能通过,所以改用VS2017)

原文地址:https://www.cnblogs.com/SChenqi/p/9636117.html