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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-14 21:13  mfrbuaa  阅读(353)  评论(0编辑  收藏  举报