C++ Primer Plus 第六版(中文版)课后编程题----第四章

4.1

<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
	char name_first[20], name_last[20];
	char grade;
	int age;

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

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

	cin.get();
	cin.get();
	return 0;
}</span>


上面是采用了字符数组的形式进行行输入,下面的代码是采用 string 类型的字符串进行行输入,个人觉得这个办法更好,不必担心字符串的长度:

<span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

int main()
{
	string name_first, name_last;
	char grade;
	int age;

	cout << "What is your first name? ";
	getline(cin, name_first);
	cout << "What is your last name? ";
	getline(cin, name_last);
	cout << "What letter grade do you deserve? ";
	cin >> grade;
	cout << "What is your age? ";
	cin >> age;

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

	cin.get();
	cin.get();
	return 0;
}</span>

4.2

#include <iostream>
#include <string>
using namespace std;

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

	cin.get();
	return 0;
}


4.3

如果所采用的不是带 _s 的字符串函数,则会出现错误 C4996,消除错误的办法就是设置预处理器或者添加代码:

#pragma warning(disable:4996)

#include <iostream>
#include <cstring>
using namespace std;

//消除错误error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
//#pragma warning(disable:4996)

int main()
{
	char f_name[20];
	char l_name[20];
	char full_name[50];

	cout << "Enter your first name: ";
	cin.getline(f_name, 19);
	cout << "Enter your last name: ";
	cin.getline(l_name, 19);

	strcpy_s(full_name, l_name); 
	strcat_s(full_name, ", ");
	strcat_s(full_name, f_name);

	cout << "Here's the information in a single string: " << full_name << ".\n";

	cin.get();
	return 0;
}


4.4

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string f_name;
	string l_name;
	string full_name;

	cout << "Enter your first name: ";
	getline(cin, f_name);
	cout << "Enter your last name: ";
	getline(cin, l_name);

	full_name = l_name;
	full_name += ", ";
	full_name += f_name;

	cout << "Here's the information in a single string: " << full_name << ".\n";

	cin.get();
	return 0;
}


4.5

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	CandyBar snack = { "Mocha Munch", 2.3, 350 };
	cout << "The brand of snack is " << snack.brand << endl
		<< "The weight of snack is " << snack.weight << endl
		<< "The calorie of snack is " << snack.calorie << endl;

	cin.get();
	return 0;
}


4.6

我并不是十分理解题目里所说的“初始化为所选择的值”,所以就只好三个元素均初始化并显示:

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	CandyBar bars[3] = {
		{ "Mocha Munch", 2.3, 350 },
		{ "Clover", 3.3, 250 },
		{ "Lucky", 5.2, 630 } 
	};

	cout << "The first bar: " << endl
		<< bars[0].brand << endl
		<< bars[0].weight << endl
		<< bars[0].calorie << endl
		<< endl;
	cout << "The second bar: " << endl
		<< bars[1].brand << endl
		<< bars[1].weight << endl
		<< bars[1].calorie << endl
		<< endl;
	cout << "The third bar: " << endl
		<< bars[2].brand << endl
		<< bars[2].weight << endl
		<< bars[2].calorie << endl;	

	cin.get();
	return 0;
}


4.7

#include <iostream>
#include <string>
using namespace std;

struct pizas
{
	string company;
	float diameter;
	float weight;
};

int main()
{
	pizas T;

	cout << "Please enter the company of the piza comes from: ";
	getline(cin, T.company);
	cout << "Enter the diameter of piza: ";
	cin >> T.diameter;
	cout << "Enter the weight of piza: ";
	cin >> T.weight;

	cout << endl;
	cout << "The information of the piza is: " << endl
		<< "Company: " << T.company << endl
		<< "Diameter: " << T.diameter << endl
		<< "Weight: " << T.weight << endl;

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


4.8

#include <iostream>
#include <string>
using namespace std;

struct pizas
{
	string company;
	float diameter;
	float weight;
};

int main()
{
	pizas *T = new pizas;
	cout << "Enter the diameter of piza: ";
	cin >> T->diameter;
	cin.get();
	cout << "Please enter the company of the piza comes from: ";
	getline(cin, T->company);
	cout << "Enter the weight of piza: ";
	cin >> T->weight;

	cout << endl;
	cout << "The information of the piza is: " << endl
		<< "Company: " << T->company << endl
		<< "Diameter: " << T->diameter << endl
		<< "Weight: " << T->weight << endl;

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


4.9

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	CandyBar *bars = new CandyBar[3];
	bars[0] = { "Mocha Munch", 2.3, 350 };
	bars[1] = { "Clover", 3.3, 250 };
	bars[2] = { "Lucky", 5.2, 630 };

	cout << "The first bar: " << endl
		<< bars[0].brand << endl
		<< bars[0].weight << endl
		<< bars[0].calorie << endl
		<< endl;
	cout << "The second bar: " << endl
		<< bars[1].brand << endl
		<< bars[1].weight << endl
		<< bars[1].calorie << endl
		<< endl;
	cout << "The third bar: " << endl
		<< bars[2].brand << endl
		<< bars[2].weight << endl
		<< bars[2].calorie << endl;

	cin.get();
	return 0;
}


4.10

#include <iostream>
#include <array>
using namespace std;

int main()
{
	array<float, 3> arr;
	float ave = 0.0;
	cout << "Please enter the scores:" << endl;
	for (int i = 0; i < arr.size(); i++)
	{
		cin >> arr[i];
		ave += arr[i];
	}
	ave = ave / arr.size();
	
	cout << "The total times is: " << arr.size() << endl;
	cout << "The average of the score is: " << ave << endl;

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











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值