《C++ Primer Plus》(第6版)第4章编程练习

《C++ Primer Plus》(第6版)第4章编程练习

  • 《C++ Primer Plus》(第6版)第4章编程练习
    • 1. 输出所示请求并显示
    • 2. 修改程序清单4.4,使用C++ string类而不是char数组。
    • 3. 使用char数组显示名字
    • 4. 使用string类显示名字
    • 5. 结构CandyBar
    • 6. CandyBar结构数组
    • 7. pissa结构体
    • 8. 对第七题的pissa结构体使用new
    • 9. 对第六题使用new
    • 10. 40米跑分数

《C++ Primer Plus》(第6版)第4章编程练习

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 
using namespace std;
int main()
{
    char firstName[20];
    char lastName[20];
    char grade;
    int age;

    cout << "What is your first name? ";
    cin.getline(firstName, 20);
    cout << "What is your last name? ";
    cin.getline(lastName, 20);
    cout << "What letter grade do you deserve? ";
    grade = getchar();
    cout << "What is your age? ";
    cin >> age;

    cout << "Name: " << lastName << ", " << firstName << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;

    system("pause");
    return 0;
}

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

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

代码:

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

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

    system("pause");
    return 0;
}

3. 使用char数组显示名字

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

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

代码:

#include 
#include 
using namespace std;
int main()
{
    char firstName[20];
    char lastName[20];
    char information[50];
    cout << "Enter your first name: ";
    cin.getline(firstName, 20);
    cout << "Enter your last name: ";
    cin.getline(lastName, 20);

    strcpy(information, lastName);
    strcat(information, ", ");
    strcat(information, firstName);

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

    system("pause");
    return 0;
}

4. 使用string类显示名字

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

下面是该程序运行时的情形:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

代码:

#include 
#include 
using namespace std;
int main()
{
    string firstName, lastName;
    string information;

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

    system("pause");
    return 0;
}

5. 结构CandyBar

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

代码:

#include 
using namespace std;

struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

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

    cout << "The name of this CandyBar is: " << snack.brand << endl;
    cout << "The weight of this CandyBar is: " << snack.weight << endl;
    cout << "The calories of this CandyBar is: " << snack.calorie << endl;

    system("pause");
    return 0;
}

6. CandyBar结构数组

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

代码:

#include 
using namespace std;

struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    CandyBar snack[3] = {
        {"Mocha Munch", 2.3, 350},
        {"Month Date", 3.6, 400},
        {"XuFuJi", 5.2, 300},
    };
    
    for (int i = 0; i < 3; i++)
    {
        cout << snack[i].brand << "'s weight is " << snack[i].weight << ", and it includes " << snack[i].calorie << " calories.\n";
    }

    system("pause");
    return 0;
}

7. pissa结构体

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

  • 披萨饼公司的名称,可以有多个单词组成。
  • 披萨饼的直径。
  • 披萨饼的重量。

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

代码:

#include 
using namespace std;

struct Pizza
{
    char company[20];
    double diameter;
    double weight;
};

int main()
{
    Pizza pizza;
    
    cout << "Enter the company name of this pizza: ";
    cin.getline(pizza.company, 20);
    cout << "Enter the diameter of this pizza: ";
    cin >> pizza.diameter;
    cout << "Enter the weight of this pizza: ";
    cin >> pizza.weight;

    cout << "The company name of this pizza: " << pizza.company << endl;
    cout << "The diameter of this pizza: " << pizza.diameter << endl;
    cout << "The weight of this pizza: " << pizza.weight << endl;

    system("pause");
    return 0;
}

8. 对第七题的pissa结构体使用new

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

代码:

#include 
using namespace std;

struct Pizza
{
    char company[20];
    double diameter;
    double weight;
};

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

    cout << "Enter the diameter of this pizza: ";
    // cin >> (*pizza).diameter;
    cin >> pizza->diameter;
    // 使用不带任何参数的cin.get()调用可读取下一个字符
    cin.get();
    cout << "Enter the company name of this pizza: ";
    // cin.getline((*pizza).company, 20);
    cin.getline(pizza->company, 20);
    cout << "Enter the weight of this pizza: ";
    // cin >> (*pizza).weight;
    cin >> pizza->weight;

    cout << "The company name of this pizza: " << (*pizza).company << endl;
    cout << "The diameter of this pizza: " << (*pizza).diameter << endl;
    cout << "The weight of this pizza: " << (*pizza).weight << endl;

	delete pizza;	

    system("pause");
    return 0;
}

9. 对第六题使用new

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

代码:

#include 
#include 
using namespace std;

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

int main()
{
    CandyBar *snack = new CandyBar[3];
    snack[0] = {"Mocha Munch1", 2.3, 350};
    snack[1] = {"Mocha Munch2", 2.5, 360};
    snack[2] = {"Mocha Munch3", 2.7, 390};

    for (int i = 0; i < 3; i++)
    {
        cout << snack[i].brand << "'s weight is " << snack[i].weight << ", and it includes " << snack[i].calorie << " calories.\n";
    }

    delete[] snack;

    system("pause");
    return 0;
}

问题:结构体CandyBar中,若string brand换成char brand[20],则程序报错。

错误信息:

no match for 'operator=' (operand types are 'CandyBar' and

如果你知道原因的话,欢迎在评论里解答!

10. 40米跑分数

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

代码:

#include 
#include 
using namespace std;
int main()
{
    const int time = 3;
    array<double, time> grade;
    double average = 0.0;

    cout << "Enter your grade for " << time << " times: ";
    for (int i = 0; i < 3; i++)
    {
        cin >> grade[i];
        average += grade[i] / time;
    }

    cout << "The average grade of " << time << " times is " << average << endl;

    system("pause");
    return 0;
}

你可能感兴趣的:(C++,c++)