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

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

4.1

//programming exercise 4.1
#include 
#include //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 
#include //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 
#include //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 
#include //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 
#include //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 
#include //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 

#include //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 
#include //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 
#include //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 
#include 
int main()
{
	using namespace std;
	cout << "Enter three results time for the 40-yd dash: " << endl;
	array 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;
}

你可能感兴趣的:(c++编程练习)