C++ Primer Plus 第六版 第四章练习答案

C++ Primer Plus 第六版 第四章练习答案

  1. 数组初始化;

  2. 字符串操作;

  3. string用法;

  4. 结构数组;

  5. 指针;

  6. vector用法;

  7. array


#include 
#include
using namespace std;

//practice 4.1
void p4_1(void) {
	string first;
	char* last = new char;
	int age;
	char grade;
	cout << "What is your first name? ";
	getline(cin, first);
	cout << "What is your last name? ";
	cin.getline(last, 64);
	cout << "What letter grade do you deserve? ";
	cin >> grade;
	cout << "What is your age? ";
	cin >> age;
	cin.get();
	cout << "Name: " << last << ", " << first << endl;
	cout << "Grade: " << char(grade + 1) << endl;
	cout << "Age: " << age << endl;
	return;
}


// practice 4.2
int p4_2(){
	string name, dessert;

	cout << "Enter your name:\n";
	getline(cin, name);
	cout << "Enter your favourite dessert:\n";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}


// practice 4.3
#include
void p4_3(void){
	const int arr_size = 100;
	char first_name[arr_size], last_name[arr_size];

	printf("Enter your fitst name:");
	cin.getline(first_name, arr_size);  
	printf("Enter your last name:");
	cin.getline(last_name, arr_size);
	cout << "Here's your infomation is a single string: " << strcat(strcat(last_name, ", "), first_name) << endl;
	return;
}


// practice 4.4
void p4_4(void) {	
	string first_name, last_name;

	cout << "Enter your first name:";
	getline(cin, first_name);
	cout << "Enter your last name:";
	getline(cin, last_name);
	cout << "Here's your infomation is a single string: " << last_name + ", " + first_name << endl;
	return;
}


// practice 4.5
void p4_5(void){
	struct CandyBar {
		string branch;
		double weight;
		int calories;
	}snack = { "Mocha Munch", 2.3, 350 };
	cout << "Branch: " << snack.branch << ", Weight: " << snack.weight << ", Calories: " << snack.calories << "." << endl;
	return;
}


 //practice 4.6
void p4_6(void) {
	struct CandyBar {
		string branch;
		double weight;
		int calories;
	};
	CandyBar snack[3] = { 
		{ "Mocha Munch", 2.3, 350}, 
		{ "Mocha", 2.4, 360}, 
		{ "Munch", 2.5, 370} 
	};
	for(int i=0; i<3; i++)
		cout << "Branch: " << snack[i].branch << ", Weight: " << snack[i].weight << ", Calories: " << snack[i].calories << "." << endl;
	return;
}


// practice 4.7
void p4_7(void){
	struct PIZZA {
		string name;
		double diameter, weight;
	};
	PIZZA pizza_info;

	cout << "Enter the pizza name: ";
	getline(cin, pizza_info.name);
	cout << "Enter the pizza diameter: ";
	cin >> pizza_info.diameter;
	cout << "Enter the pizza weight: ";
	cin >> pizza_info.weight;
	cout << "Pizza's name: " << pizza_info.name << ", diameter: " << pizza_info.diameter << ", weight: " << pizza_info.weight << endl;
	return;
}

// practice 4.8
void p4_8(void){
	struct PIZZA {
		string name;
		double diameter, weight;
	};
	PIZZA* pizza_info = new PIZZA;
	cout << "Enter the pizza diameter: ";
	cin >> pizza_info->diameter;
	cin.get();
	cout << "Enter the pizza name: ";
	getline(cin, pizza_info->name);
	cout << "Enter the pizza weight: ";
	cin >> pizza_info->weight;
	cout << "Pizza's name: " << pizza_info->name << ", diameter: " << pizza_info->diameter << ", weight: " << pizza_info->weight << endl;
	delete pizza_info;
}


// practice 4.9
void p4_9(void){
	struct CandyBar {
		string branch;
		double weight;
		int calories;
	};
	CandyBar* snack = new CandyBar[3]{
		{ "Mocha Munch", 2.3, 350},
		{ "Mocha", 2.4, 360},
		{ "Munch", 2.5, 370}
	};
	for (int i = 0; i < 3; i++)
		cout << "Branch: " << snack[i].branch << ", Weight: " << snack[i].weight << ", Calories: " << snack[i].calories << "." << endl;
		//或者写成 cout << "Branch: " << (snack + i)->branch 等也可以。
	delete[] snack;
}


// practice 4.10
#include
void p4_10(void){
	array<double, 3> time;

	cout << "Enter three grades of 40 meters: ";
	cin >> time[0] >> time[1] >> time[2];
	cout << "Time: " << time[0] << ", " << time[1] << ", " << time[2] << endl;
	cout << "Average time: " << (time[0] + time[1] + time[2]) / 3;
}

int main(int argc, char **argv) {
	cout << "=====================================\n"
		<< "============  Chapter4:  ============\n"
		<< "=====================================\n\n";
	cout << "==>> Practice 4_1:\n";
	p4_1();
	cout << endl;

	cout << "==>> Practice 4_2:\n";
	p4_2();
	cout << endl;

	cout << "==>> Practice 4_3:\n";
	p4_3();
	cout << endl;

	cout << "==>> Practice 4_4:\n";
	p4_4();
	cout << endl;

	cout << "==>> Practice 4_5:\n";
	p4_5();
	cout << endl;

	cout << "==>> Practice 4_6:\n";
	p4_6();
	cout << endl;

	cout << "==>> Practice 4_7:\n";
	p4_7();
	cout << endl;

	cout << "==>> Practice 4_8:\n";
	p4_8();
	cout << endl;

	cout << "==>> Practice 4_9:\n";
	p4_9();
	cout << endl;

	cout << "==>> Practice 4_10:\n";
	p4_10();
	cout << endl;
}

你可能感兴趣的:(#,C++_Prime_Plus)