跟着c++Primer Plus学编程--- 4.13编程练习答案详解】

跟着c++Primer Plus学编程--- 4.13编程练习答案详解

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

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
    int age;
    char grade;
    char fname[20], lname[20];

    cout << "What is your first name? ";
    cin.getline(fname,20);
    cout << "What is your last name? ";
    cin.get(lname,20);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;

    cout << "Name: " << lname << ", " << fname << endl;
    cout << "Grade: " << ++grade << endl;
    cout << "Age: " << age << endl;

    return 0;
}

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

#include <iostream>
#include <string>
#include <cstring>
int main()
{
	using namespace std;
	string name, dessert;
	cout << "Enter your name: \n";
	getline(cin, name);
	cout << "Enter your favorite delicious: \n";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}

在这里插入图片描述

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

在这里插入图片描述

#include <iostream>
#include <cstring>

int main()
{
	using namespace std;
	const int n = 20;
	char fname[n];
	char lname[n];
	cout << "Enter your first name: ";
	cin.getline(fname,n);
	cout << "\nEnter your last name: ";
	cin.get(lname,n);
	cout << "\nHere's the formation in a single string: " << lname << " ," << fname << endl;
	return 0;
}

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

在这里插入图片描述

#include <iostream>
#include <string>

int main()
{
	using namespace std;
	string fname, lname;
	cout << "Enter your first name: ";
	getline(cin,fname);
	cout << "Enter your last name: ";
	getline(cin,lname);
	cout << "Here's the formation in a single string: " << lname << " ," << fname << endl;
	return 0;
}

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

#include <iostream>
int main()
{
	using namespace std;
	struct CandyBar
	{
		char name[20];
		double weight;
		int carorie;
	};
	CandyBar snack = {
		"Mocha Munch",
		2.3,
		350
	};
	cout << "Name: " << snack.name << endl;
	cout << "weight: " << snack.weight << endl;
	cout << "carorie: " << snack.carorie << endl;
	return 0;
}

在这里插入图片描述

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

#include <iostream>
int main()
{
	using namespace std;
	struct CandyBar
	{
		char name[20];
		double weight;
		int carorie;
	};
	CandyBar snack[3] =
	{
		{"mocha",2.3,2},
		{"moch",3.3,3},
		{"moc",4.3,4}
	};
	cout << "Name: " << snack[0].name << endl;
	cout << "weight: " << snack[0].weight << endl;
	cout << "carorie: " << snack[0].carorie << endl;
	cout << "Name: " << snack[1].name << endl;
	cout << "weight: " << snack[1].weight << endl;
	cout << "carorie: " << snack[1].carorie << endl;
	cout << "Name: " << snack[2].name << endl;
	cout << "weight: " << snack[2].weight << endl;
	cout << "carorie: " << snack[2].carorie << endl;
	return 0;
}

在这里插入图片描述

7.William Wingate 从事比萨饼分析服务,对于每个披萨饼,它们都需要记录信息,请设计一个能够储存这些信息的结构,并编写结构变量程序。程序将请求用户输入以下信息,然后显示这些信息。请用 cin 和 cout。

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

#include <iostream>
int main()
{
	using namespace std;
	struct William
	{
		char name[20];
		double lenth;
		double weight;
	}WW;
	cout << "please enter complay name: ";
	cin.getline(WW.name,20);
	cout << "\nplease enter lenth: ";
	cin >> WW.lenth;
	cout << "\nplease enter weight: ";
	cin >> WW.weight;
	cout << "\ncomplay name: " << WW.name << endl;
	cout << "lenth: " << WW.lenth << endl;
	cout << "weight: " << WW.weight << endl;
	return 0;
}

在这里插入图片描述

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

#include <iostream>
int main()
{
	using namespace std;
	struct William
	{
		char name[20];
		double lenth;
		double weight;
	};
	William* WW = new William;
	
	cout << "\nplease enter lenth: ";
	cin >> WW->lenth;
	cin.get();//这行代码为了读取输入lenth 时候输入的转行符,不然代码会直接跳过输入公司名称
	cout << "please enter complay name: ";
	cin.getline(WW->name, 20);
	cout << "\nplease enter weight: ";
	cin >> WW->weight;
	cout << "\ncomplay name: " << WW->name << endl;
	cout << "lenth: " << WW->lenth << endl;
	cout << "weight: " << WW->weight << endl;
	return 0;
}

在这里插入图片描述

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

#include <iostream>
int main()
{
	using namespace std;
	struct CandyBar
	{
		char name[20];
		double weight;
		int carorie;
	};

	CandyBar* snack = new CandyBar[3];
	snack[0] = { "mocha",2.3,2 };
	snack[1] = { "moch",3.3,3 };
	snack[2] = { "moc",4.3,4 };
	cout << "Name: " << snack[0].name << endl;
	cout << "weight: " << snack[0].weight << endl;
	cout << "carorie: " << snack[0].carorie << endl;
	cout << "Name: " << snack[1].name << endl;
	cout << "weight: " << snack[1].weight << endl;
	cout << "carorie: " << snack[1].carorie << endl;
	cout << "Name: " << snack[2].name << endl;
	cout << "weight: " << snack[2].weight << endl;
	cout << "carorie: " << snack[2].carorie << endl;
	return 0;
}

在这里插入图片描述

10.编写一个程序,让用户输入三次40码跑的成绩,并显示次数和平均成绩。请使用一个 array 对象来储存数据。

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

int main()
{
    array<double, 3> scores;
    cout << "Please enter your the results of the first 40 meter race: ";
    cin >> scores[0];
    cout << "Please enter your the results of the second 40 meter race: ";
    cin >> scores[1];
    cout << "Please enter your the results of the third 40 meter race: ";
    cin >> scores[2];
    cout << "You ran three times in total." << endl;
    cout << "Your average score is ";
    cout << (scores[0] + scores[1] + scores[2]) / 3.0;
    cout << "." << endl;
    return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

太阳请了个假

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

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

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

打赏作者

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

抵扣说明:

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

余额充值