C++Primer Plus第六版第四章复合类型编程练习答案

1.

#include 
using namespace std;

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

	cout << "what is your first name? ";
	cin.getline(fname, 20);
	cout << "what is your last name? ";
	cin.getline(lname, 20);
	cout << "what letter grade do you deserve? ";
	cin >> grade;
	cout << "what is your age? ";
	cin >> age;
	cout << "Name: " << lname << ", " << fname << '\n'
		<< "Grade: " << (char)(grade + 1) << '\n' << "Age: " << age << endl;

	return 0;
}

2.

#include 
#include 
using namespace std;

int main(void)
{
	string name, 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.

注意由于使用的环境是vs2017,使用了_s后缀版本的函数.

#include 
#include 
#include 
using namespace std;

int main(void)
{
	char fname[20], lname[20], name[40];

	cout << "Enter your first name: ";
	cin.getline(fname, 20);
	cout << "Enter your last name: ";
	cin.getline(lname, 20);
	strcpy_s(name, lname);
	strcat_s(name, ", ");
	strcat_s(name, fname);
	cout << "Here's the information in a single string: " << name << endl;

	return 0;
}

4.

#include 
#include 
using namespace std;

int main(void)
{
	string fname, lname, name;

	cout << "Enter your first name: ";
	getline(cin, fname);
	cout << "Enter your last name: ";
	getline(cin, lname);
	name = lname + ", " + fname;
	cout << "Here's the information in a single string: " << name << endl;

	return 0;
}

5.

#include 
#include 
using namespace std;
struct snack {
	string brand;
	double weight;
	int Calorie;
}CandyBar = {
	"Mocha Munch",
	2.3,
	350
};

int main(void)
{
	cout << "品牌名称是: " << CandyBar.brand << endl;
	cout << "重量是: " << CandyBar.weight << endl;
	cout << "卡路里是: " << CandyBar.Calorie << endl;

	return 0;
}

6.

#include 
#include 
using namespace std;
struct snack {
	string brand;
	double weight;
	int Calorie;
};

int main(void)
{
	snack snacks[3] = { {"阿尔卑斯",100,1000},{"德芙",200,2000},{"炫迈",50,50} };
	for (int i = 0; i < 3; i++)
	{
		cout << "品牌名称: " << snacks[i].brand << endl;
		cout << "重量: " << snacks[i].brand << endl;
		cout << "卡路里: " << snacks[i].Calorie << endl;
		cout << endl;
	}

	return 0;
}

7.

#include
#define MAX 20
using namespace std;
struct Pizza {
	char brand[MAX];
	double diameter;
	double weight;
};
int main()
{
	Pizza brand1;

	cout << "输入披萨公司的名称: ";
	cin.getline(brand1.brand, MAX);
	cout << "输入披萨的直径: ";
	cin >> brand1.diameter;
	cout << "输入披萨的重量: ";
	cin >> brand1.weight;
	cout << "披萨公司的名称是: " << brand1.brand << endl;
	cout << "披萨的直径是: " << brand1.diameter << endl;
	cout << "披萨的重量是: " << brand1.weight << endl;

	return 0;
}

8.

#include
#define MAX 20
using namespace std;
struct Pizza {
	char brand[MAX];
	double diameter;
	double weight;
};
int main()
{
	Pizza *pf = new Pizza;

	cout << "输入披萨的直径: ";
	cin >> pf->diameter;
	cin.get();//去除\n
	cout << "输入披萨公司的名称: ";
	cin.getline(pf->brand, MAX);
	cout << "输入披萨的重量: ";
	cin >> pf->weight;
	cout << "披萨公司的名称是: " << pf->brand << endl;
	cout << "披萨的直径是: " << pf->diameter << endl;
	cout << "披萨的重量是: " << pf->weight << endl;

	delete pf;
	return 0;
}

9.

#include 
#include 
using namespace std;
struct snack {
	string brand;
	double weight;
	int Calorie;
};

int main(void)
{
	snack *snacks = new snack[3] { {"阿尔卑斯",100,1000},{"德芙",200,2000},{"炫迈",50,50} };
	for (int i = 0; i < 3; i++)
	{
		cout << "品牌名称: " << snacks[i].brand << endl;
		cout << "重量: " << snacks[i].brand << endl;
		cout << "卡路里: " << snacks[i].Calorie << endl;
		cout << endl;
	}
	delete [] snacks;
	return 0;
}

10.

#include 
#include 
using namespace std;
int main()
{
	array<double, 3> data;
	double averagevalue;

	cout << "输入三次成绩(40米): ";
	cin >> data[0] >> data[1] >> data[2];
	averagevalue = (data[0] + data[1] + data[2]) / 3;
	cout << "输入了三次成绩,平均成绩是: " << averagevalue << endl;

	return 0;
}

你可能感兴趣的:(c++,primer,plus,C++,C++,Primer,Plus,第六版,第四章,答案)