C++ Primer Plus(第六版) 第四章 编程练习

编程环境:Visual Studio 2017

1:

//practice 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string first_name;
	string last_name;
	char grade;
	int age;

	cout << "What is your first name? ";
	getline(cin, first_name);

	cout << "What is your last name? ";
	getline(cin, last_name);

	cout << "What letter grade do you deserve? ";
	cin >> grade;

	cout << "What is your age? ";
	cin >> age;

	cout << "Name : " << last_name << " , " << first_name << endl;
	cout << "Grade : " << char(grade + 1) << endl;
	cout << "Age : " << age << endl;

	system("pause");
	return 0;
}

2: 

//practice 2
#include<iostream>
#include<string>
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";

	system("pause");
	return 0;
}

3: 

//practice 3
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	char first_name[20];
	char last_name[20];
	char name[40];

	cout << "Enter your first name: ";
	cin.getline(first_name, 20);
	cout << "Enter your last name: ";
	cin.getline(last_name, 20);

	strcpy(name, last_name);
	strcat(name, ", ");
	strcat(name, first_name);

	cout << "Here's the information in a single string: " << name << endl;

	system("pause");
	return 0;
}

4: 

//practice 4
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string first_name;
	string last_name;
	string name;

	cout << "Enter your first name: ";
	getline(cin, first_name);
	cout << "Enter your last name: ";
	getline(cin, last_name);

	name = last_name + ", " + first_name;

	cout << "Here's the information in a single string: " << name << endl;
	system("pause");
	return 0;
}

5: 

//practice 5
#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};

int main()
{
	CandyBar snack = { "Mocha Munch",2.3,350 };

	cout<<"The infomation of snack"<<endl;
	cout << "Brand: " << snack.brand << endl;
	cout << "Weight: " << snack.weight << endl;
	cout << "Calorie: " << snack.calorie << endl;

	system("pause");
	return 0;
}

 6:

//practice 6
#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};

int main()
{
	CandyBar snack[3] =
	{
		{"Mocha Munch",2.3,350},
		{"balabalabala",4.4,666},
		{"aaaaaaaaa",88.88,777}
	};

	for (int i = 0; i < 3; i++)
	{
		cout << "The infomation of snack[" << i << "]" << endl;
		cout << "Brand: " << snack[i].brand << endl;
		cout << "Weight: " << snack[i].weight << endl;
		cout << "Calorie: " << snack[i].calorie << endl << endl;
	}
	system("pause");
	return 0;
}

7: 

//practice 7
#include<iostream>
#include<string>
using namespace std;

struct PIZZA
{
	string name;
	int diameter;
	double weight;
};

int main()
{
	PIZZA pizza;

	cout << "Please enter the pizza company name: ";
	getline(cin, pizza.name);

	cout << "Please enter the pizza diameter: ";
	cin >> pizza.diameter;

	cout << "Please enter the pizza weight: ";
	cin >> pizza.weight;

	cout << "\nThe information of pizza is\nCompany name: " << pizza.name << "\nPizza diameter: " << pizza.diameter << "\nPizza weight: " << pizza.weight << endl;

	system("pause");
	return 0;
}

 8:

//practice 8
#include<iostream>
#include<string>
using namespace std;

struct PIZZA
{
	string name;
	int diameter;
	double weight;
};

int main()
{
	PIZZA *pizza = new PIZZA;

	cout << "Please enter the pizza diameter: ";
	cin >> pizza->diameter;
	cin.get();

	cout << "Please enter the pizza company name: ";
	getline(cin, pizza->name);

	cout << "Please enter the pizza weight: ";
	cin >> pizza->weight;

	cout << "\nThe information of pizza is\nCompany name: " << pizza->name << "\nPizza diameter: " << pizza->diameter << "\nPizza weight: " << pizza->weight << endl;

	delete pizza;
	pizza = NULL;

	system("pause");
	return 0;
}

 9:

//practice 9
#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};

int main()
{
	CandyBar *snack = new CandyBar[3]
	{
		{"Mocha Munch",2.3,350},
		{"balabalabala",4.4,666},
		{"aaaaaaaaa",88.88,777}
	};

	for (int i = 0; i < 3; i++)
	{
		cout << "The infomation of snack[" << i << "]" << endl;
		cout << "Brand: " << snack[i].brand << endl;
		cout << "Weight: " << snack[i].weight << endl;
		cout << "Calorie: " << snack[i].calorie << endl << endl;
	}

	delete[] snack;
	system("pause");
	return 0;
}

10: 

//practice 10
#include<iostream>
#include<array>
using namespace std;

int main()
{
	array<double, 3> grade;
	double avg_grade = 0.0;
	
	cout << "Enter the grade of the first time: ";
	cin >> grade[0];

	cout << "Enter the grade of the second time: ";
	cin >> grade[1];

	cout << "Enter the grade of the last time: ";
	cin >> grade[2];

	avg_grade = (grade[0] + grade[1] + grade[2]) / 3.0;

	cout << "\nGrade: " << grade[0] << ", " << grade[1] << ", " << grade[2] << endl;
	cout << "The average grade: " << avg_grade << endl;

	system("pause");
	return 0;
}

才疏学浅,如有错误或者改进的建议还请大牛多多指出。

继续学习,慢慢更新。

猜你喜欢

转载自blog.csdn.net/qq_42212893/article/details/82318362