C++ Primer Plus(第6版)编程练习答案——第4章

第四章练习题答案
程序集中在一起,每次切换main函数中的test即可。
PS:自学C++,如有疑问欢迎一起讨论。

第五章练习题答案

#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<iomanip>
#include<cstring>
#include<array>
using namespace std;

void test1() 
{
	struct MyInfo
	{
		string first_name;
		string last_name;
		char grade{'A'};
		int age=0;
	}LiMing;

	cout << "What is your first name? ";
	getline(cin, LiMing.first_name);
	cout << "What is your last name? ";
	getline(cin, LiMing.last_name);
	cout << "What letter grade do you deserve? ";
	cin >> LiMing.grade;
	cout << "What is your age? ";
	cin >> LiMing.age;

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

void test2()
{
	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 << " for you, " << name << ".\n";
}

void test3_4()
{
	//char first_name1[30];
	//char last_name1[30];
	string first_name2;
	string last_name2;
	cout << "Enter your first name: ";
	//cin.getline(first_name1,30);
	getline(cin, first_name2);
	cout << "Enter your last name: ";
	//cin.getline(last_name1,30);
	getline(cin, last_name2);
	//char name1[65] = {};
	//char connect[3] = { ',', ' ','\0' };
	string name2 = "";
	//strcpy_s(name1, sizeof(name1), last_name1);
	//strcat_s(name1, sizeof(name1), connect);
	//strcat_s(name1, sizeof(name1), first_name1);

	name2 = last_name2 + ", " + first_name2;

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

void test5_6_9()
{
	struct CandyBar {
		string brand = "Mocha Munch";
		double weight = 2.3;
		unsigned int calorie = 350;
	}snack;

	cout << snack.brand << endl;
	cout << snack.weight << endl;
	cout << snack.calorie << endl;


	CandyBar candy[3];
	candy[0].brand = "a";
	candy[0].weight = 0.5;
	candy[0].calorie = 120;
	cout << candy[0].brand << ", " << candy[0].weight << ", " << candy[0].calorie << endl;
	candy[1].brand = "b";
	candy[1].weight = 0.8;
	candy[1].calorie = 200;
	cout << candy[1].brand << ", " << candy[1].weight << ", " << candy[1].calorie << endl;
	candy[2].brand = "c";
	candy[2].weight = 0.9;
	candy[2].calorie = 340;
	cout << candy[2].brand << ", " << candy[2].weight << ", " << candy[2].calorie << endl;

	cout << "----------------test9----------------" << endl;
	CandyBar* pt = new CandyBar[3];
	pt->brand = "a1";
	pt->weight = 0.5;
	pt->calorie = 120;
	cout << pt->brand << ", " << pt->weight << ", " << pt->calorie << endl;
	(pt+1)->brand = "b1";
	(pt + 1)->weight = 0.8;
	(pt + 1)->calorie = 200;
	cout << (pt + 1)->brand << ", " << (pt + 1)->weight << ", " << (pt + 1)->calorie << endl;
	(pt + 2)->brand = "c1";
	(pt + 2)->weight = 0.9;
	(pt + 2)->calorie = 340;
	cout << (pt + 2)->brand << ", " << (pt + 2)->weight << ", " << (pt + 2)->calorie << endl;

	delete[] pt;
}
void test7_8() 
{
	struct piza
	{
		string company = "a";
		double diameter = 1;
		double weight = 2.5;
	}piza1;

	cout << "Enter the company of piza1: ";
	cin >> piza1.company;
	cout << "Enter the diameter: ";
	cin >> piza1.diameter;
	cout << "Enter the weight: ";
	cin >> piza1.weight;
	cout << "You enter:" << piza1.company << ", " << piza1.diameter << ", " << piza1.weight << endl;


	cout << "-------------test8--------------" << endl;
	piza* p = new piza;
	cout << "Enter the diameter: ";
	cin >> p->diameter;
	cout << "Enter the company of piza1: ";
	cin >> p->company;
	cout << "Enter the weight: ";
	cin >> p->weight;
	cout << "You enter:" << p->diameter << ", " << p->company << ", " << p->weight << endl;
	delete p;
}

void test10() 
{
	array<int, 3> score;
	cout << "Enter the first score: ";
	cin >> score[0];
	cout << "Enter the second score: ";
	cin >> score[1];
	cout << "Enter the third score: ";
	cin >> score[2];

	cout << score.size() << "次的平均分为:" << (score[0] + score[1] + score[2]) / score.size() << endl;

}

int main()
{
	//test1();
	//test2();
	//test3_4()
	//test5_6_9();
	//test7_8();
	test10();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值