C++PrimerPlus(第6版)中文版 第4章 复合类型 编程练习 参考答案

自己编写的参考答案,在VS2019中都可以编译通过,不是标准答案,也不是最优答案,仅供参考

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

What is your frist name? Betty Sue

What is your last name? Yewe

What letter grade do you deserve? B

What is your age? 22

Name: Yewe, Brtty Sue

Grade: C

Age: 22

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

#include < iostream >
#include< string >
using namespace std;
int main()
{
string fristName;
cout << "What is your frist name? ";
getline(cin, fristName);
string lastName;
cout << "What is your last name? ";
cin >> lastName;
char grade;
cout << "What letter grade do you deserve? ";
cin >> grade;
grade += 1;
int age;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << lastName << ", " << fristName<<endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
}

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

#include < iostream >
#include< string >
using namespace std;
int main()
{
string name;
string dessert;
cout << “Enter your name:\n”;
getline(cin, name);
cout << “Enter your favorute 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: Fleming

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

#include < iostream >
using namespace std;
int main()
{
const int size = 20;
char firstName[size];
char lastName[size];
char fullName[2 * size -1];
cout << "Enter your first name: ";
cin >> firstName;
cout << "Enter your last name: ";
cin >> lastName;
strcpy_s(fullName, lastName);
strcat_s(fullName, ", ");
strcat_s(fullName, firstName);
cout <<"Here’s the information in a single string: "<< fullName;
}

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

Enter your first name: Flip

Enter your last name: Fleming

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

#include < iostream >
using namespace std;
int main()
{
string firstName;
string lastName;
string fullName;
cout << "Enter your first name: ";
cin >> firstName;
cout << "Enter your last name: ";
cin >> lastName;
fullName = lastName + ", " + firstName;
cout << "Here’s the information in a single string: " << fullName;
}

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

#include < iostream >
using namespace std;
int main()
{
struct CandyBar
{
string brand;
float weight;
int calorie;
};
CandyBar snack = { “Mocha Muuch”,2.3,350 };
cout << “Brand:” << snack.brand << “\tWeight:” << snack.weight << “\tCalorie:” << snack.calorie;
}

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

#include < iostream >
using namespace std;
int main()
{
struct CandyBar
{
string brand;
float weight;
int calorie;
};
CandyBar snack[3] = { { “Mocha Muuch”,2.3,350 }, { “Big Rabbit”,3.1,300 }, { “Yoga”,1.8,288 } };
cout << “Brand:” << snack[0].brand << “\tWeight:” << snack[0].weight << “\tCalorie:” << snack[0].calorie << endl
<< “Brand:” << snack[1].brand << “\tWeight:” << snack[1].weight << “\tCalorie:” << snack[1].calorie << endl
<< “Brand:” << snack[2].brand << “\tWeight:” << snack[2].weight << “\tCalorie:” << snack[2].calorie << endl;
}

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

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

披萨饼的直径。

披萨饼的重量。

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

#include < iostream >
#include< string >
using namespace std;
int main()
{
struct Pizza
{
string companyName;
float diameter;
float weight;
};
Pizza pizza1;
cout << “请输入披萨饼公司的名称:” ;
getline(cin, pizza1.companyName);
cout << “请输入披萨饼的直径:” ;
cin >> pizza1.diameter;
cout << “请输入披萨饼的重量:” ;
cin >> pizza1.weight;
cout << “萨饼公司的名称:” << pizza1.companyName << “\t披萨饼的直径:” << pizza1.diameter << “\t萨饼的重量:” << pizza1.weight;
}

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

#include < iostream >
#include< string >
using namespace std;
int main()
{
float *diameter = new float;
string * companyName = new string;
float *weight = new float;
cout << “请输入披萨饼的直径:”;
(cin >> *diameter).get();
cout << “请输入披萨饼公司的名称:”;
getline(cin, *companyName);
cout << “请输入披萨饼的重量:”;
cin >> *weight;
cout << “萨饼公司的名称:” << *companyName << “\t披萨饼的直径:” << *diameter << “\t萨饼的重量:” << *weight;
delete diameter;
delete companyName;
delete weight;
}

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

#include < iostream >
using namespace std;
int main()
{
struct CandyBar
{
string brand;
float weight;
int calorie;
};
CandyBar* p = new CandyBar[3];
p[0] = { “Mocha Muuch”,2.3,350 };
p[1] = { “Big Rabbit”,3.1,300 };
p[2] = { “Yoga”,1.8,288 };
cout << “Brand:” << p[0].brand << “\tWeight:” << p[0].weight << “\tCalorie:” << p[0].calorie << endl
<< “Brand:” << p[1].brand << “\tWeight:” << p[1].weight << “\tCalorie:” << p[1].calorie << endl
<< “Brand:” << p[2].brand << “\tWeight:” << p[2].weight << “\tCalorie:” << p[2].calorie << endl;
delete[] p;
}

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

#include < iostream >
#include< array >
using namespace std;
int main()
{
array<float, 3>time;
cout << “请输入三次40码跑的成绩:”;
cin >> time[0] >> time[1] >> time[2];
cout << “共进行了” << time.size()<<“次40码跑”<<“平均成绩为”<<(time.at(0)+time.at(1)+time.at(2))/ time.size();
}

发布了4 篇原创文章 · 获赞 0 · 访问量 57

猜你喜欢

转载自blog.csdn.net/HuLaTangDuoLaZi/article/details/103911581