C++ Primer Plus(第六版)编程练习 第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之间的空档。

本题主要有两点需要注意,第一点是输入的名字要包含多个单词,这就说明我们定义的用来储存名字char类型变量要足够长,而且输入函数要能够包含空格,然后以回车作为分隔,所以这里输入函数我选择使用cin.getline()函数。这个函数最普遍的用法就是cin.getline(a, b),其中a表示要输入的变量名称,b表示输入的长度;但是只要你敲回车,没有达到b的数值也会使输入结束,而如果你输入的长度超过b,则只有前b个输入有效。

第二个要注意的问题是将成绩的输入向下调整,这一点其实很简单,因为输入的成绩是ABCD这种,所以是char类型的变量,而对char类型变量直接做加减法,其实就是将字母往前后移,比如

char a = A;

 a = a + 1; 

cout << a; 

输出的a是“B”,因为a = a + 1;将A往后移了一位变成了B。

代码如下:

// 4.1.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

int main()
{
    using namespace std;

    char firstname[80];
    char lastname[80];
    char grade;
    int age;
    cout << "What is your first name? ";
    cin.getline(firstname, 80);
    cout << "What is your last name? ";
    cin.getline(lastname, 80);
    cout << "What letter do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;
	
    grade = grade + 1;

    cout << "Name: " << lastname << ", " << firstname << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;

    system("pause");
    return 0;
}

代码的运行结果如下图所示:



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

将char数组修改为string类,还是比较简单的,只需要在程序开头include一下包含string的头文件,后面的操作对应为使用string类就可以了。string类的变量不需要在意长度,所以输入的时候我们使用getline()函数,使用该函数必须头文件包含#include<string>,该函数也可以接受空格并输出,而且不在乎长度,以回车作为输入终止,非常好用。

string str; 

getline(cin, str); 

这是最普遍的使用getline()函数的方法,该函数基本上都是针对string类使用的。

代码如下:

// 4.2.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string name;
    string dessert;

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

    system("pause");
    return 0;
}

运行结果如下图所示:



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

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming, Flip

本题要求使用char数组,而且要求存储一个包含有逗号和空格的组合结果,那么我们就肯定要使用cin.getline()函数和strcat()函数了。cin.getline()函数相信大家到这里已经很熟悉了,所以我来简单讲一下strcat()函数。strcat()函数是专门用来将两个char类型变量连接起来形成新的char类型变量的函数,该函数在头文件cstring中被定义,比如

char a[20] = "one";

char b[20] = "two";

strcat(a, b);

cout << a;

以上代码输出“onetwo”,中间不会有间隔,将b连接到a的后面,形成新的char数组赋给a。

在这里因为我使用的是vs2017,所以直接使用strcat()函数无法通过编译,要使用微软改良过的strcat_s()函数,该函数相比strcat()函数增加了安全性考量,当出现缓冲区溢出问题时,使用strcat()函数就无法预测结果,因为它可能会错误地改变程序中其他部分的内存数据,而使用strcat_s()函数时程序会抛出异常,便于发现问题。

本题的代码如下:

// 4.3.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstring>

int main()
{
    using namespace std;

    char firstname[80];
    char lastname[80];
    char wholename[80];

    cout << "Enter your first name: ";
    cin.getline(firstname, 80);
    cout << "Enter your last name: ";
    cin.getline(lastname, 80);
    strcat_s(lastname, ", ");
    strcat_s(lastname, firstname);

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

    system("pause");
    return 0;
}

运行结果如下图所示:



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

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming, Flip

本题和第三题可以对应起来看,第三题是使用char数组和cstring头文件,本题是使用string类和string头文件。

其实对于使用string类和string头文件,就不需要和第三题一样考虑长度和使用连接函数了,直接就定义两个string类的对象,直接把它们和逗号、空格按顺序加起来就可以了。

代码如下:

// 4.4.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string firstname;
    string lastname;
    string wholename;

    cout << "Enter your first name: ";
    getline(cin, firstname);
    cout << "Enter your last name: ";
    getline(cin, lastname);
    wholename = lastname + ", " + firstname;

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

    system("pause");
    return 0;
}

运行结果如下图所示:



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

本题就是创建一个结构体,注意在声明结构体时就初始其中的成员变量,然后显示就可以了。

代码如下:

// 4.5.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

struct CandyBar
{
    char brand[20];
    float weight;
    int caloie;
};

int main()
{
    using namespace std;

    CandyBar snack =
    {
	"Mocha Munch",
	2.3,
	350
    };
    cout << "The snack is following:" << endl;
    cout << "Brand: " << snack.brand << endl;
    cout << "Wight: " << snack.weight << endl;
    cout << "Calorie: " << snack.caloie << endl;

    system("pause");
    return 0;
}

运行结果如下图所示:



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

本题就是在第五题基础上的延伸,将单纯的结构体定义和声明,延伸为结构体数组的定义和声明,具体的操作和单纯的结构体定义和声明类似,使用{ …… }大括号将一个结构体的成员变量括起来,在括号内去具体声明变量的值。

代码如下:

// 4.6.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

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

int main()
{
    using namespace std;

    CandyBar snack[3] =
    {
	{"Mocha Munch",2.3,350},
	{"Fruit Salad",1.6,140},
	{"Fried Chicken",2.4,880}
    };

    cout << "The snack is following: " << endl;
    cout << "Brand: " << snack[0].brand << endl;
    cout << "Weight: " << snack[0].weight << endl;
    cout << "Calorie: " << snack[0].calorie << endl;
    cout << "Brand: " << snack[1].brand << endl;
    cout << "Weight: " << snack[1].weight << endl;
    cout << "Calorie: " << snack[1].calorie << endl;
    cout << "Brand: " << snack[2].brand << endl;
    cout << "Weight: " << snack[2].weight << endl;
    cout << "Calorie: " << snack[2].calorie << endl;

    system("pause");
    return 0;
}

运行结果如下图所示:



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

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

- 披萨饼的直径。

- 披萨饼的重量。

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

本题要求我们定义一个结构体用来存储披萨饼的各种信息,然后显示出来,具体披萨饼信息需要我们自己输入,在这里我就写了一个仅输入一套披萨饼信息的情况。具体输入的时候只需要注意披萨饼公司名称是要求可以有多个单词组成就可以了,使用cin.getline()函数;其他的就和普通的结构体声明相同,然后将初始化换成请求用户输入就好。

代码如下:

// 4.7.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

struct William
{
    char brand[20];
    float diameter;
    float weight;
};

int main()
{
    using namespace std;

    William example;
    cout << "Please enter your pizza's information: " << endl;
    cout << "Brand: ";
    cin.getline(example.brand, 20);
    cout << "Diameter: ";
    cin >> example.diameter;
    cout << "Weight: ";
    cin >> example.weight;

    cout << "So the following is your pizza's information:" << endl;
    cout << "Brand: " << example.brand << ".\n";
    cout << "Diameter: " << example.diameter << "cm.\n";
    cout << "Weight: " << example.weight << "kg.\n";

    system("pause");
    return 0;
}

运行结果如下图所示:



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

第七题是使用结构体来储存披萨饼公司的各种信息,但是我们是仅输入一套披萨饼信息,但本题要求使用new来为结构分配内存,我们就没有结构体声明了,使用new来对结构体分配,记得在最后delete掉占用的内存就可以了。

代码如下:

// 4.8.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

struct William
{
    char brand[20];
    float diameter;
    float weight;
};

int main()
{
    using namespace std;

    William * example;
    example = new William;
    cout << "Please Enter your pizza's information: " << endl;
    cout << "Brand: ";
    cin.getline(example->brand, 20);
    cout << "Diameter: ";
    cin >> example->diameter;
    cin.get();
    cout << "Weight: ";
    cin >> example->weight;
    cout << "So the folowing is your pizza's  information:" << endl;
    cout << "Brand: " << example->brand << ".\n";
    cout << "Diameter: " << example->diameter << "cm.\n";
    cout << "Weight: " << example->weight << "kg.\n";
    delete[] example;

    system("pause");
    return 0;
}

运行结果如下图所示:



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

第六题使用结构体来储存糖块信息,指明了一共有3个元素,每个结构体元素内部有3个成员变量;本题要求我们使用new来动态分配数组,再在最后记得使用delete来释放内存。

代码如下:

// 4.9.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

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

int main()
{
    using namespace std;

    CandyBar * snack = new CandyBar[3];
    snack[0] = { "Mocha Munch",2.3,350 };
    snack[1] = { "Fruit Salad", 1.6, 140 };
    snack[2] = { "Fried Chicken", 2.4, 880 };
		
    cout << "The snack is following: " << endl;
    cout << "Brand: " << snack[0].brand << endl;
    cout << "Weight: " << snack[0].weight << endl;
    cout << "Calorie: " << snack[0].calorie << endl;
    cout << "Brand: " << snack[1].brand << endl;
    cout << "Weight: " << snack[1].weight << endl;
    cout << "Calorie: " << snack[1].calorie << endl;
    cout << "Brand: " << snack[2].brand << endl;
    cout << "Weight: " << snack[2].weight << endl;
    cout << "Calorie: " << snack[2].calorie << endl;
    delete[] snack;
	
    system("pause");
    return 0;
}

运行结果如下图所示:



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

本题要求使用array对象来储存数据,我们就按照array对象的语法规则来写就行了,最简单的array对象就比如:

array<int, 3> a = {1, 2, 3};

一般化来说,就是创建有一个名为arr的array对象,包含number个类型为typename的元素,即为:

array<typename, number> arr;

代码如下:

// 4.10.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <array>

int main()
{
    using namespace std;

    const int number = 3;
    array<double, number> time;
    cout << "Enter the 1st grade: ";
    cin >> time[0];
    cout << "Enter the 2nd grade: ";
    cin >> time[1];
    cout << "Enter the 3rd grade: ";
    cin >> time[2];

    cout << "So the whole numbers of the grade is " << number << ".\n";
    cout << "The grades are: " << time[0] << ", " << time[1] << ", " << time[2] << ".\n";
    cout << "The average grade is " << (time[0] + time[1] + time[2]) / 3 << ".\n";

    system("pause");
    return 0;
}

运行结果如下图所示:


猜你喜欢

转载自blog.csdn.net/leowinbow/article/details/80922394
今日推荐