C++ primer plus(第六版)4.13复习题及其答案

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

What is your first name?Betty Sue

What is your last name?Yewe

What letter grade do you deserve?B

Name:Yewe,Betty Sue

Grade:C

Age:22

#include<iostream>
#include<string>
using namespace std;
int main()
{
	cout << "What is your first name?";
	string name1;
	getline(cin,name1);
	cout << "What is your last name?";
	string name2;
getline(cin,name2);
cout << "What letter grade do you desserve?";
char grade;
cin.get(grade);
cout << "What is your age?";
int age;
cin>>age;
cout << "Name:" << name1 << "," << name2 << endl;
cout << "Grade:" <<char( grade + 1) << endl;
cout << "Age:" << age << endl;
	return 0;
}

 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 favorite dessert:\n";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}

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

Enter your first name:Plip

Enter your last name:Fleming

Here is the information in a single string:Fleming,Flip

#include<iostream>
#include<string>
using namespace std;
int main()
{
	cout << "Enter your first name:";

	char name1[10];
	cin.getline(name1, 10);
	cout << "Enter your last name:";
	char name2[10];
	cin.getline(name2, 10);
	cout << "Here's the information in a string:"<<name2<<","<<name1;
	return 0;
}

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

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
	string name;
	float weight;
	int caloris;
};
int main()
{
	CandyBar snack={"Mocha Munch",2.3,350};
	cout << "糖块的品牌:" << snack.name << endl;
	cout << "糖块的重量:" << snack.weight << endl;
	cout << "糖块的卡路里:" << snack.caloris << endl;
	return 0;
}

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

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

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

#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
	string name;
	double dia;
	double weight;
};
int main()
{
	Pizza pia;
	cout << "请输入披萨公司的名称:";
getline(cin,pia.name);
	cout << "请输入披萨饼的直径:";
	cin >> pia.dia;
	cout << "请输入披萨饼的重量:";
	cin >> pia.weight;
	cout << "披萨公司的名称为:" << pia.name << endl;
	cout << "披萨饼的直径为:" << pia.dia<<endl;
	cout << "披萨饼的重量为:" << pia.weight<<endl;
	return 0;
}

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

 

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
	{
		string name;
		float weight;
		int caloris;
	};
int main()
{
	CandyBar snacks[3] = { {"candy1",2.3,350 },
							{"candy2",3.5,500},
								{"candy3",3.1,450} };
	cout << "the first candy's name:" << snacks[0].name<<endl;
	cout << "the first candy's weight:" << snacks[0].weight<<endl;
	cout << "the first candy's caloris:" << snacks[0].caloris<<endl;
	cout << endl;
	cout << "the first candy's name:" << snacks[1].name<<endl;
	cout << "the first candy's weight:" << snacks[1].weight<<endl;
	cout << "the first candy's caloris:" << snacks[1].caloris<<endl;
	cout << endl;
	cout << "the first candy's name:" << snacks[2].name<<endl;
	cout << "the first candy's weight:" << snacks[2].weight<<endl;
	cout << "the first candy's caloris:" << snacks[2].caloris<<endl;
	return 0;
}

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

 

#include<iostream>
#include<string>
using namespace std;
struct Pizza
{	
	double dia;
	string name;
	double weight;
};
int main()
{
	Pizza* pt = new Pizza;
	cout << "请输入披萨饼的直径:";
	cin>>pt->dia;
	cin.get();
	cout << "请输入披萨公司的名称:";
	cin >> pt->name;
	cout << "请输入披萨饼的重量:";
	cin >> pt->weight;
	cout << "披萨饼的直径为:" << pt->dia << endl;
	cout << "披萨公司的名称为:" << pt->name << endl;
	cout << "披萨饼的重量为:" << pt->weight << endl;
delete [] pt;
	return 0;
}

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

 

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
struct CandyBar
{
	string name;
	float weight;
	int caloris;
};
int main()
{
	CandyBar *pt=new CandyBar[3];
	cout << "Enter the first candy's name:";
	cin >>pt[0].name;
	cout << "Enter the first candy's weight:";
	cin >> pt[0].weight;
	cout << "Enter the first candy's caloris:";
	cin>>pt[0].caloris;
	cout << endl;
	cout << "Enter the second candy's name:";
	cin >> pt[1].name;
	cout << "Enter the second candy's weight:";
	cin >> pt[1].weight;
	cout << "Enter the second candy's caloris:";
	cin >> pt[1].caloris;
	cout << endl;
	cout << "Enter the third candy's name:";
	cin >> pt[2].name;
	cout << "Enter the third candy's weight:";
	cin >> pt[2].weight;
	cout << "Enter the third candy's caloris:";
	cin >> pt[2].caloris;
	cout << endl;
	cout << "the first candy's name:" << pt[0].name << endl;
	cout << "the first candy's weight:" << pt[0].weight << endl;
	cout << "the first candy's caloris:" << pt[0].caloris << endl;
	cout << endl;
	cout << "the first candy's name:" <<pt[1].name << endl;
	cout << "the first candy's weight:" <<pt[1].weight << endl;
	cout << "the first candy's caloris:" << pt[1].caloris << endl;
	cout << endl;
	cout << "the first candy's name:" <<pt[2].name << endl;
	cout << "the first candy's weight:" << pt[2].weight << endl;
	cout << "the first candy's caloris:" <<pt[2].caloris << endl;
	delete[]pt;
	return 0;
}

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

#include<iostream>
#include<array>
using namespace std;
int main()
{
	array<int, 3>grade;
	cout << "Enter the grade:";
	cin >> grade[0] >>grade[1] >> grade[2];
	int sum = grade[0] + grade[1] + grade[2];
	int times = sizeof(grade) / sizeof(int);
	cout << "The run times:" << times<<endl;
	cout << " The average grade:" << sum / times;
	return 0;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的老年人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值