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

1.

#include <iostream>
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 <iostream>
#include <string>
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 <iostream>
#include <string>
#include <cstring>
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 <iostream>
#include <string>
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 <iostream>
#include <string>
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 <iostream>
#include <string>
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<iostream>
#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<iostream>
#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 <iostream>
#include <string>
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 <iostream>
#include <array>
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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值