《C++ Primer Plus 第六版》编程练习参考答案(第四章)

本文通过多个实例,深入探讨了C++中结构体与数组的使用方法,包括数据输入输出、动态内存分配及成员变量的访问。从简单的结构体定义到复杂的数据集合处理,全面展示了C++编程的基本技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第四章

第一题:

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

int main()
{
	struct info
	{
		string FirstName;
		string LastName;
		char LetterGrade;
		int age;
	};
	
	info stu {};//Only available with -std = c++11
	cout<<"What is your first name? ";
	getline(cin,stu.FirstName);
	cout<<"What is your last name? ";
	getline(cin,stu.LastName);
	cout<<"What letter grade do you deserve? ";
	cin>>stu.LetterGrade;
	cout<<"What is your age? ";
	cin>>stu.age;
	
	cout<<"Name: "<<stu.LastName<<", "<<stu.FirstName<<endl;
	cout<<"Grade: "<<(char)(stu.LetterGrade+1)<<endl;
	cout<<"Age: "<<stu.age;
}

第二题:

#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;
}

第三题:

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

int main()
{
	char FirstName[20];
	char LastName[20];
	
	cout<<"Enter your first name: ";
	cin.getline(FirstName,20);
	cout<<"Enter your last name: ";
	cin.getline(LastName,20);
	
	cout<<"Here's the information in a single string: "
	<<LastName<<", "<<FirstName;
}

第四题:

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

int main()
{
	string FirstName;
	string LastName;
	
	cout<<"Enter your first name: ";
	getline(cin,FirstName);
	cout<<"Enter your last name: ";
	getline(cin,LastName);
	
	cout<<"Here's the information in a single string: "
	<<LastName<<", "<<FirstName;
}

第五题:

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

int main()
{
	struct CandyBar
	{
		string brand;
		double weight;
		int calorie;
	};
	
	CandyBar snack{"Mocha Munch",2.3,350};//only available with -std = c++11
	cout<<"Name: snack"<<endl;
	cout<<"Brand: "<<snack.brand<<endl;
	cout<<"Weight: "<<snack.weight<<endl;
	cout<<"Calorie: "<<snack.calorie<<endl;
}

第六题:

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

int main()
{
	struct CandyBar
	{
		string brand;
		double weight;
		int calorie;
	};
	
	CandyBar snack[3]=
	{
		{"Mocha Munch",2.3,350},
		{"Cheese Cake",2.5,450},
		{"Nougat",1.7,265}
	};
	
	cout<<"Name: snack"<<endl;
//The following code can be realized by loop which will be talked about in Ch.5
	cout<<"Brand: "<<snack[0].brand<<endl;
	cout<<"Weight: "<<snack[0].weight<<endl;
	cout<<"Calorie: "<<snack[0].calorie<<endl<<endl;
	
	cout<<"Brand: "<<snack[1].brand<<endl;
	cout<<"Weight: "<<snack[1].weight<<endl;
	cout<<"Calorie: "<<snack[1].calorie<<endl<<endl;
	
	cout<<"Brand: "<<snack[2].brand<<endl;
	cout<<"Weight: "<<snack[2].weight<<endl;
	cout<<"Calorie: "<<snack[2].calorie<<endl;
}

第七题:

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

int main()
{
	struct info
	{
		string company;
		double diameter;
		double weight;
	};
	
	info Pizza;
	cout<<"Please enter the name of the company: ";
	getline(cin,Pizza.company);
	cout<<"Please enter the diameter: ";
	cin>>Pizza.diameter;
	cout<<"Please enter the weight: ";
	cin>>Pizza.weight;
	
	cout<<"Company: "<<Pizza.company<<endl;
	cout<<"Diameter: "<<Pizza.diameter<<endl;
	cout<<"Weight: "<<Pizza.weight<<endl;
}

第八题:

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

int main()
{
	struct info
	{
		string company;
		double diameter;
		double weight;
	};
	
	info *Pizza=new info;
	//In the following part, (*Pizza).xxxx can be replaced by Pizza->xxxx
	cout<<"Please enter the diameter: ";
	cin>>(*Pizza).diameter;
	cin.get();//Necessary! P77 explains this problem.
	cout<<"Please enter the name of the company: ";
	getline(cin,(*Pizza).company);
	cout<<"Please enter the weight: ";
	cin>>(*Pizza).weight;
	
	cout<<"Company: "<<(*Pizza).company<<endl;
	cout<<"Diameter: "<<(*Pizza).diameter<<endl;
	cout<<"Weight: "<<(*Pizza).weight<<endl;
	
	delete Pizza;
}

第九题:

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

int main()
{
	struct CandyBar
	{
		string brand;
		double weight;
		int calorie;
	};
	
	CandyBar *snack=new CandyBar[3];
	
	cout<<"Name: snack"<<endl;
	
	cout<<"Please enter the brand: "; 
	getline(cin,snack[0].brand);
	cout<<"Please enter the weight: ";
	cin>>snack[0].weight;
	cout<<"Please enter the calorie: ";
	cin>>snack[0].calorie;
	cin.get();
	
	cout<<"Please enter the brand: "; 
	getline(cin,snack[1].brand);
	cout<<"Please enter the weight: ";
	cin>>snack[1].weight;
	cout<<"Please enter the calorie: ";
	cin>>snack[1].calorie;
	cin.get();
	
	cout<<"Please enter the brand: "; 
	getline(cin,snack[2].brand);
	cout<<"Please enter the weight: ";
	cin>>snack[2].weight;
	cout<<"Please enter the calorie: ";
	cin>>snack[2].calorie;
	cin.get();
	
	cout<<"Brand: "<<snack[0].brand<<endl;
	cout<<"Weight: "<<snack[0].weight<<endl;
	cout<<"Calorie: "<<snack[0].calorie<<endl<<endl;
	cout<<"Brand: "<<snack[1].brand<<endl;
	cout<<"Weight: "<<snack[1].weight<<endl;
	cout<<"Calorie: "<<snack[1].calorie<<endl<<endl;
	cout<<"Brand: "<<snack[2].brand<<endl;
	cout<<"Weight: "<<snack[2].weight<<endl;
	cout<<"Calorie: "<<snack[2].calorie<<endl;
}

第十题:

//Tips: If you use dev c++ as the IDE, 
//click Tools-Compile Options-Add the command when compiling: -std=c++11
#include <iostream>
#include <array>
using namespace std;

int main()
{
	array<double,3> FortyMeters;
	
	cout<<"Please enter the time of 40m running(three time): "<<endl;
	cin>>FortyMeters[0];
	cin>>FortyMeters[1];
	cin>>FortyMeters[2];
	
	double ave=0;
	ave=(FortyMeters[0]+FortyMeters[1]+FortyMeters[2])/3;
	cout<<"1: "<<FortyMeters[0]<<endl;
	cout<<"2: "<<FortyMeters[1]<<endl;
	cout<<"3: "<<FortyMeters[2]<<endl;
	cout<<"Avergae: "<<ave<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值