c++ primer plus(第6版)中文版 第八章编程练习答案

第八章编程练习答案

8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动)

//8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动)
#include <iostream>
using namespace std;

void show (const char* str, int time=1)
{
	unsigned	n=(time>0)?time:-time;
	for (unsigned i = 0; i < n; ++i) 
		cout << str << endl;
	cout << "-----------------" << endl;
}

int main () 
{     
	const char* str = "hello word!";
	show(str);
	show(str,0);
	show(str,1);
	show(str,-1);
} 

8.2CandyBar结构体有品牌,重量和热量3个成员,编写一个含有默认值的函数为成员赋值和一个显示内容函数

//8.2CandyBar结构体有品牌,重量和热量3个成员,编写一个含有默认值的函数为成员赋值和一个显示内容函数
//CandyBar& candbar, const char* strBrand = "Millennium Munch", double weight = 2.85, int calories = 350
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
	string Brand;
	double weight;
	int calories;
};

void input (CandyBar& candbar, const char* Brand = "Millennium Munch", double weight = 2.85, int calories = 350)
{
	candbar.Brand = Brand;
	candbar.weight = weight;
	candbar.calories = calories;
}

void show (const CandyBar& candbar)
{
	cout << candbar.Brand << '\t' << candbar.weight << '\t' << candbar.calories << endl;
}

int main () 
{     
	CandyBar	candbar1, candbar2;
	input(candbar1);
	show(candbar1);
	input(candbar2, "world", 2,3 );
	show(candbar2);
} 


8.3编写一个函数,接受string引用,并把string内容变大写,之后利用循环测试

//8.3编写一个函数,接受string引用,并把string内容变大写,之后利用循环测试
#include <iostream>
#include <cctype>
using namespace std;

string& upper (string& str)
{
	for (char& e : str)
		e = (char)toupper(e);
	return (str);
}

int main () 
{     
	while (true) {
		cout << "Enter a string (q to quit): ";
		string str;
		getline(cin, str);
		if (!cin || "q" == str || "Q" == str)
			break;
		cout << upper(str) << endl;
	}
} 

8.4按以下程序框架,完成函数

//8.4按以下程序框架,完成函数
#include <iostream>
#include <cstring>
using namespace std;

struct stringy {
	char * str;
	int ct;
};

void set (stringy& stry, const char* szTxt)
{
	stry.ct = (int)strlen(szTxt);
	stry.str = new char [stry.ct + 1];
	strcpy(stry.str, szTxt);
}

void show (const char* szTxt, unsigned times = 1)
{
	for (unsigned i = 0; i < times; ++i)
		cout << szTxt << endl;
}

void show (const stringy& stry, unsigned times = 1)
{
	for (unsigned i = 0; i < times; ++i)
		cout << stry.str << endl;
}

void destroy (stringy& stry)
{
	delete [] stry.str;
	stry.str = NULL;
}

int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	set(beany, testing);
	show(beany);
	show(beany, 2);
	destroy(beany);
	testing[0] = 'D';
	testing[1] = 'u';
	show(testing);
	show(testing, 3);
	show("Done!");
}

8.5利用一个模板函数max5(),返回给予的5个数的最大值(应用c++11的array)

//8.5利用一个模板函数max5(),返回给予的5个数的最大值(应用c++11的array)
#include <iostream>
#include <array>
using namespace std;

template<typename T>
const T& max5 (const array<T, 5>& arr)
{
	unsigned	max = 0;
	for (unsigned i = 0; i < 5; ++i)
		if (arr[i] > arr[max])
			max = i;
	return (arr[max]);
}

int main ()
{
	array<int, 5>	iarray = {32, 20, 99, 256, 9};
	for (const auto& e : iarray)
		cout << e << ' ';
	cout << " max: " << max5(iarray) << endl;
	
	array<double,5>  darray = {1.2,2.3,4.5,32.3,4.3};
	for (const auto& e : darray) 
		cout << e << ' ';
	cout << "max: " << max5(darray) << endl;
}

8.6编写模板函数maxn(),参数为一个数组和元素数目,返回数组最大值,然后具体化一个字符串为元素的字符串,返回最长字符串

//8.6编写模板函数maxn(),参数为一个数组和元素数目,返回数组最大值,
//然后具体化一个字符串为元素的字符串,返回最长字符串
#include <iostream>
#include <cstring>
using namespace std;

template <typename T>
const T& maxn (const T arr[], unsigned n)
{
	unsigned	max = 0;
	for (unsigned i = 0; i < n; ++i)
		if (arr[i] > arr[max])
			max = i;
	return (arr[max]);
}

char* maxn ( char* arr[], unsigned n)
{
	unsigned	max = 0;
	for (unsigned i = 0; i < n; ++i)
		if (strlen(arr[i]) > strlen(arr[max]))
			max = i;
	return (arr[max]);
}

int main ()
{
	int	iArray[] = {32, -1, 99, 0, 256, 9};
	for (const auto& e : iArray)
		cout << e << ' ';
	cout << " ----max: " << maxn(iArray, sizeof(iArray)/sizeof(iArray[0])) << endl;
	double	dArray[] = {-3.2, 221.22, 9.9, 0, 1};
	for (const auto& e : dArray)
		cout << e << ' ';
	cout << " ----max: " << maxn(dArray, sizeof(dArray)/sizeof(dArray[0])) << endl;
	const char*	szArray[] = {"aaaa","bbbbbb","cc","fffffffffff","kkkk"};
	for (const auto& e : szArray) 
		cout << '\"' << e << '\"' << ' ';
	cout << " ----max: " << '\"' << maxn(szArray, sizeof(szArray)/sizeof(szArray[0])) << '\"' << endl;
}

8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。程序返回thing以及debt的总和

//8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。程序返回thing以及debt的总和
#include <iostream>
using namespace std;

struct debts
{
	char name[50];
	double amount;
};

template <typename T>
double SumArray(T arr[], int n)
{	
	cout << "template A\n";
	double	sum = 0;
	for (int i = 0; i < n; i++)
		sum += arr[i];
	return (sum);
}

template <typename T>
double SumArray(T * arr[], int n)
{
	cout << "template B\n";
	double	sum = 0;
	for (int i = 0; i < n; i++)
		sum += *arr[i];
	return (sum);
}

int main()
{
	int things[6] = {13, 31, 103, 301, 310, 130};
	struct debts mr_E[3] = {	{"Ima Wolfe", 2400.0},{"Ura Foxe", 1300.0},{"Iby Stout", 1800.0}	};
	double * pd[3];
	for (int i = 0; i < 3; i++)
		pd[i] = &mr_E[i].amount;
	cout << "the total number of Mr. E's things: " << SumArray(things, 6) << endl;
	cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值