c++ primer plus第四章programming exercises答案

c++ primer plus第四章programming exercises答案

4.1

//programming exercise 4.1
#include <iostream>
#include <cstring>//C-style string library
int main()
{
	using namespace std;
	cout << "What is your first name? ";
	char first_name[20];
	cin.getline(first_name, 19);
	cout << "What is your last name? ";
	char last_name[20];
	cin >> last_name;
	cout << "What letter grade do you deserve? ";
	char letter;
	cin >> letter;
	cout << "What is your age? ";
	int age;
	cin >> age;
	cout << "Name: " << last_name << ", " << first_name << endl;
	cout << "Grade; " << char(letter + 1) << endl;
	cout << "Age: " << age << endl;

	cin.get();
	cin.get();
	return 0;
}

4.2

//programming exercise 4.2
#include <iostream>
#include <string>//make string class available

int main()
{
	using namespace std;
	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";

	cin.get();
	cin.get();
	return 0;
}

4.3

//programming exercise 4.3
#include <iostream>
#include <cstring>//C-style string library

int main()
{
	using namespace std;
	cout << "Enter your first name: ";
	char first_name[20];
	cin.getline(first_name, 19);
	cout << "Enter your last name: ";
	char last_name[20];
	cin.getline(last_name, 19);
	cout << "Here's the information in a single string: " << last_name << ", " << first_name << endl;

	cin.get();
	cin.get();
	return 0;
}

4.4

//programming exercise 4.4
#include <iostream>
#include <string>//make string class available

int main()
{
	using namespace std;
	cout << "Enter your first name: ";
	string first_name;
	getline(cin, first_name);
	cout << "Enter your last name: ";
	string last_name;
	getline(cin, last_name);
	cout << "Here's the information in a single string: " << last_name 
	    << ", " << first_name << endl;

	cin.get();
	cin.get();
	return 0;
}

4.5

//programming exercise 4.5
#include <iostream>
#include <string>//make string class available
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int calories;
};
int main()
{
	CandyBar snack =
	{
		"Mocha Munch",
		2.3,
		350
	};
	cout << "The information of this candy bar:" << endl;
	cout << "name: " << snack.name 
		<< ", weight: " << snack.weight 
		<< ", calories: " << snack.calories << endl;
	
	cin.get();
	return 0;
}

4.6

//programming exercise 4.6
#include <iostream>
#include <string>//make string class available
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int calories;
};
int main()
{
	CandyBar candybars[3] = {
		{"Mocha Munch", 2.3, 350},
		{"Arbeisi", 1.0, 240},
		{"Sweet Love", 6.13,520}
	};
	
	cout << "The information of the first candy bar:" << endl;
	cout << "name: " << candybars[0].name 
		<< ", weight: " << candybars[0].weight
		<< ", calories: " << candybars[0].calories << endl;

	cout << "The information of the second candy bar:" << endl;
	cout << "name: " << candybars[1].name
		<< ", weight: " << candybars[1].weight
		<< ", calories: " << candybars[1].calories << endl;

	cout << "The information of the third candy bar:" << endl;
	cout << "name: " << candybars[2].name
		<< ", weight: " << candybars[2].weight
		<< ", calories: " << candybars[2].calories << endl;
	
	cin.get();
	return 0;
}

4.7

//programming exercise 4.7
#include <iostream>

#include <cstring>//C-style string library
struct Pizza_Inform
{
	char name[20];
	double diameter;
	double weight;
};
int main()
{
    using namespace std;
	Pizza_Inform yourpizza;
	cout << "Enter the company name of your pizza: ";
	cin.getline(yourpizza.name, 19);
	cout << "Enter the diameter of your pizza: ";
	cin >> yourpizza.diameter;
	cout << "Enter the weight of your pizza: ";
	cin >> yourpizza.weight;

	cout << "The information of your pizza:" << endl;
	cout << "Company name: " << yourpizza.name << ", diameter: " 
		<< yourpizza.diameter << ", weight: " << yourpizza.weight << endl;
	
	cin.get();
	cin.get();
	return 0;
}

4.8

//programming exercise 4.8
#include <iostream>
#include <cstring>//C-style string library
struct Pizza_Inform
{
	char name[20];
	double diameter;
	double weight;
};
int main()
{
    using namespace std;
	Pizza_Inform *yourpizza = new Pizza_Inform;
	cout << "Enter the company name of your pizza: ";
	cin.getline(yourpizza->name, 19);
	cout << "Enter the diameter of your pizza: ";
	cin >> yourpizza->diameter;
	cout << "Enter the weight of your pizza: ";
	cin >> yourpizza->weight;

	cout << "The information of your pizza:" << endl;
	cout << "Company name: " << yourpizza->name << ", diameter: " 
		<< yourpizza->diameter << ", weight: " << yourpizza->weight << endl;
	delete yourpizza;
	cin.get();
	cin.get();
	return 0;
}

4.9

//programming exercise 4.9
#include <iostream>
#include <string>//make string class available
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int calories;
};
int main()
{
	CandyBar *candybars = new CandyBar [3]{
		{ "Mocha Munch", 2.3, 350 },
		{ "Arbeisi", 1.0, 240 },
		{ "Sweet Love", 6.13,520 }
	};

	cout << "The information of the first candy bar:" << endl;
	cout << "name: " 
		<< candybars[0].name 
		<< ", weight: " << candybars[0].weight
		<< ", calories: " << candybars[0].calories << endl;

	cout << "The information of the second candy bar:" << endl;
	cout << "name: " << candybars[1].name
		<< ", weight: " << candybars[1].weight
		<< ", calories: " << candybars[1].calories << endl;

	cout << "The information of the third candy bar:" << endl;
	cout << "name: " << candybars[2].name
		<< ", weight: " << candybars[2].weight
		<< ", calories: " << candybars[2].calories << endl;
	delete[] candybars;
	cin.get();
	return 0;
}

4.10

//programming exercise 4.10
#include <iostream>
#include <array>
int main()
{
	using namespace std;
	cout << "Enter three results time for the 40-yd dash: " << endl;
	array<double, 3> time;
	cin >> time[0];
	cin >> time[1];
	cin >> time[2];

	double average_time = ( time[0] + time[1] + time[2] ) / 3;
	cout << "3 times' average result: " << average_time << endl;

	cin.get();
	cin.get();
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值