《C++ Primer Plus》第四章习题与参考答案

1,内容选自《C++ Primer Plus》(第6版)中文版,2017年1月河北第21次印刷版本
2,文章系笔者学习笔记,若有错误,欢迎指正
3,如有雷同,纯属巧合

4.12 复习题

1.如何声明下述数据?
a. actor是由30个char组成的数组
b. betsie是由100个short组成的数组
c. chuck是由13个float组成的数组
d. dipsea是由64个long double组成的数组

a. char actor[30];
b. short betsie[100];
c. float chuck[13];
d. long double dipsea[64];

2.使用模板类array而不是数组来完成1的问题。

#include <array>
...
using namespace std;
a. array<char,30> actor;
b. array<short,100> betsie;
c. array<float,13> chuck;
d. array<long double,64> dipsea;

3.声明一个包含5个元素的int数组,并将他们初始化为前5个正奇数。

int oddly[5] = {1, 3, 5, 7, 9};

4.编写一条语句,将问题3中数组第一个元素和最后一个元素的和赋值给变量even。

int even = oddly[0] + oddly[4];

5.编写一条语句,显示float数组ideas中的第二个元素的值。

cout << ideas[1] << endl;

6.声明一个char的数组,并将其初始化为字符串“cheeseburger”。

char lunch[13] = "cheeseburger";//number of characters + 1char lunch[] = "cheeseburger";//let the compiler count elements

7.声明一个string对象,并将其初始化为字符串“Waldorf Salad”。

#include <string>
....
using namespace std;
string lunch = "Waldorf Salad";
如果没有using编译指令,则为:
std::string lunch = "Waldorf Salad";

8.设计一个描述鱼的结构声明,结构中应当包括品种、重量(整数,单位为盎司)和长度(英寸,包括小数)。

struct fish
{
    char kind[20];
    int weight;
    float length;
};

9.声明8中定义的结构变量,并对它进行初始化。

fish goldfish = 
{
    "bubbe_eye",
    2,
    5.4
};

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

enum Response
{
    No,
    Yes,
    Maybe
};

11.假设ted是一个double变量,请声明一个指向ted的指针,并使用该指针来显示ted的值。

double *ptr = &ted;
cout << *ptr << endl;

12.假设treacle是一个包含10个元素的float数组,请声明一个指向treacle的第一个元素的指针,并使用该指针来显示数组的第一元素和最后一个元素。

float *ptr = &treacle[0];//or float *ptr = treacle;
cout << *ptr << " " << *(ptr + 9) << endl;

13.编写一段代码,要求用户输入一个正整数,然后创建一个动态int数组,其中包含的元素数目等于用户的输入值,首先使用new来完成这项任务,再使用vector对象完成这项任务。

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int size;
    cout << "Enter a positive integer:";
    cin >> size;
    int *ptr = new int[size];
    vector<int> vi;
    //...
    delete[] ptr;
    return 0;
}

14.下面的代码是否有效,如果有效,他将输出社么结果。

cout << (int*) "Home of the jolly bytes";

是的,它是有效的。表达式"Home of the jolly bytes"是一个字符串常量,因此,它将判定为字符串开始的地址。cout对象将char地址解释为打印字符串,但类型转换(int*)将地址转换为int指针,然后作为地址被打印。总之该语句打印字符串的地址,只要int类型足够宽,能够存储该地址。

15.编写一段代码,给问题8中的结构动态分配内存,再读取该结构的成员的值。

fish *ptr = new fish;
cout << ptr->kind << ptr->weight << ptr->length;

16.程序清单4.6指出了混合输入数字和一行字符串存储的问题,如果将下面的代码:
cin.getline(adress,80);替换为:cin>>address;将对程序带来什么影响?

使用cin>>address将使得程序跳过空白,直到找到非空白字符为止。然后它将读取字符,直到再次遇到空白为止。因此,它将跳过数字输入后的换行符,从而避免这种问题。另一方面,它只读取一个单词,而不是整行。

17.声明一个vector对象和一个array对象,他们都包含10个string对象。指出所需的头文件,但不要使用using,使用const来指定要包含的string对象数。

#include <vector>
#include <array>
#include <string>
...
const unsigned int size = 10;
std::vector<std::string> vs(size);
std::array<std::string, size> va;

4.13 编程练习

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之间的空档。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string first_name, last_name;
    cout << "What is your first name? ";
    getline(cin, first_name);
    cout << "What is your last name? ";
    getline(cin, last_name);

    char grade;
    int age;
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;

    cout << "Name:" << first_name << " " << last_name << endl;
    cout << "Grade:" << ++grade << endl;
    cout << "Age:" << age << endl;
    return 0;
}

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

#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";
    return 0;
}

3.编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示结合效果,请使用char数组和头文件cstring中的函数。下面是该程序的运行时的情形:
Enter your first name: Flip
Enter your last name: Fleaming
Here’s the information in a single string: Fleming, Flip

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    const int name_size = 20;
    char first_name[name_size];
    char last_name[name_size];
    char full_name[name_size * 2];

    cout << "Enter your first name:";
    cin.get(first_name, name_size).get();
    cout << "Enter your last name:";
    cin.get(last_name, name_size).get();

    strcpy(full_name, last_name);
    strcat(full_name, ",");
    strcat(full_name, first_name);
    cout << "Here's the information in a single string:" << full_name << endl;
    return 0;
}

4.编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示结合效果,请使用string对象和文件string中的函数。下面是该程序的运行时的情形:
Enter your first name: Flip
Enter your last name: Fleaming
Here’s the information in a single string: Fleming, Flip

#include <iostream>
#include <string>

using namespace std;

int main()
{
    const int name_size = 20;
    string first_name, last_name, full_name;

    cout << "Enter your first name:";
    getline(cin, first_name);
    cout << "Enter your last name:";
    getline(cin, last_name);

    full_name = last_name + "," + first_name;
    cout << "Here's the information in a single string:" << full_name << endl;
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    struct CandyBar
    {
        string brand;
        float weight;
        int calorie;
    };
    CandyBar snack = {"Mocha Munch", 2.3, 350};
    cout << "brand:" << snack.brand << " weight:" << snack.weight << " calorie:" << snack.calorie << endl;
    system("PAUSE");
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    struct CandyBar
    {
        string brand;
        float weight;
        int calorie;
    };
    CandyBar snack[3] = {
        {"Mocha Munch", 2.3, 350},
        {"Cabdury", 1.5, 220},
        {"Truffles", 2.1, 160}};
    for (int i = 0; i < 3; i++)
    {
        cout << "brand:" << snack[i].brand << " weight:" << snack[i].weight << " calorie:" << snack[i].calorie << endl;
    }
    return 0;
}

7.William Wingate从事披萨分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成。
披萨饼的直径
披萨饼的重量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    struct Pizza
    {
        string company;
        int diameter;
        float weight;
    } pizza;

    cout << "Enter the company:";
    getline(cin, pizza.company);
    cout << "Enter the diameter:";
    cin >> pizza.diameter;
    cout << "Enter the weight:";
    cin >> pizza.weight;

    cout << "company:" << pizza.company << " diameter:" << pizza.diameter << " weight:" << pizza.weight << endl;
    return 0;
}

8.完成编程练习7,但使用new来为结构动态分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨名称之前输入披萨直径。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    struct Pizza
    {
        string company;
        int diameter;
        float weight;
    };

    Pizza *pizza = new Pizza;
    cout << "Enter the diameter:";
    cin >> pizza->diameter;
    cin.get();
    cout << "Enter the company:";
    getline(cin, pizza->company);
    cout << "Enter the weight:";
    cin >> pizza->weight;

    cout << "diameter:" << pizza->diameter << " company:" << pizza->company << " weight:" << pizza->weight << endl;
    delete pizza;
    system("pause");
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    struct CandyBar
    {
        string brand;
        float weight;
        int calorie;
    };
    CandyBar *bars = new CandyBar[3];
    bars[0] = {"Mocha Munch", 2.3, 350};
    bars[1] = {"Cabdury", 1.5, 220};
    bars[2] = {"Truffles", 2.1, 160};
    for (int i = 0; i < 3; i++)
    {
        cout << "brand:" << bars[i].brand << " weight:" << bars[i].weight << " calorie:" << bars[i].calorie << endl;
    }
    system("PAUSE");
    return 0;
}

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

#include <iostream>
#include <array>
using namespace std;

int main()
{
    array<double, 3> ad;
    cout << "Please enter the first grade:";
    cin >> ad[0];
    cout << "Please enter the second grade:";
    cin >> ad[1];
    cout << "Please enter the third grade:";
    cin >> ad[2];
    cout << "the average grade is:" << (ad[0] + ad[1] + ad[2]) / 3 << endl;
    system("PAUSE");
    return 0;
}
发布了8 篇原创文章 · 获赞 1 · 访问量 687

猜你喜欢

转载自blog.csdn.net/Heisenberg_Li/article/details/102609777