C++ Primer Plus第六版第八章函数探幽编程练习答案

1.

#include
using namespace std;
void show(const char *str, int n = 0);
int main()
{
	char str[20];
	int n;
	cout << "输入一个句子: ";
	cin.getline(str, 20);
	cout << "输入打印次数: ";
	cin >> n;
	show(str, n);
	return 0;
}
void show(const char *str, int n)
{
	cout << str << endl;
	if (n > 1)
		show(str, n - 1);
}

2.

#include
#include
const int SIZE = 30;
using namespace std;
struct CandyBar {
	char brand[SIZE] = "Millennium Munch";
	double weight = 2.85;
	int cal = 350;
};
void show(const CandyBar & candy);
void setcandybar(CandyBar & candy, char *str, double n, int m);
int main()
{
	CandyBar candy;
	char name[SIZE];
	double n;
	int m;
	cout << "输入品牌名: ";
	cin.getline(name, SIZE);
	cout << "输入重量: ";
	cin >> n;
	cin.get();
	cout << "输入卡路里: ";
	cin >> m;
	setcandybar(candy, name, n, m);
	show(candy);

	return 0;
}
void setcandybar(CandyBar & candy, char *str, double n, int m)
{
	strcpy_s(candy.brand, str);
	candy.weight = n;
	candy.cal = m;
}
void show(const CandyBar & candy)
{
	cout << "品牌: " << candy.brand << endl;
	cout << "重量: " << candy.weight << endl;
	cout << "卡路里: " << candy.cal << endl;
}

3.

#include
#include
#include
using namespace std;
void MyToupper(string &str);
int main()
{
	string str;
	cout << "Enter a string(q to quit): ";
	while (getline(cin, str) && str != "q")
	{
		MyToupper(str);
		cout << str << endl;
		cout << "Next string (q to quit) : ";
	}
	return 0;
}
void MyToupper(string &str)
{
	for (auto &a : str)
		a = toupper(a);
}

4.

#include

using namespace std;
struct stringy {
	char * str;
	int ct;
};
void show(const char * str, int n = 1);
void show(const stringy & str, int n = 1);
void Set(stringy & str, char * test);
int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	Set(beany, testing);
	show(beany);
	show(beany, 2);
	testing[0] = 'D';
	testing[1] = 'u';
	show(testing);
	show(testing, 3);
	show("Done!");
	return 0;

}
void Set(stringy & st, char * test)
{
	st.str = test;
	st.ct = strlen(test);
}
void show(const char * str, int n)
{
	for (int i = 0; i < n; ++i)
		cout << str << endl;
}
void show(const stringy & str, int n)
{
	for (int i = 0; i < n; ++i)
		cout << str.str << endl;
}

5.

#include
#include
#include
using namespace std;
template <typename T>
T max5(T arr[]);
int main()
{
	int data[5] = { 1, 2, 3, 4, 5 };
	double data1[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
	cout << "max int is " << max5(data) << endl;
	cout << "max double is " << max5(data1) << endl;
	return 0;
}
template <typename T>
T max5(T arr[])
{
	T Max = arr[0];
	for (int i = 0; i < 5; ++i)
		if (arr[i] > Max)
			Max = arr[i];
	return Max;
}

6.

#include
#include
using namespace std;
template <typename T>
T maxn(T arr[], int n);
template <> const char * maxn(const char * arr[], int n);
int main()
{
	int data[6] = { 1, 2, 3, 4, 5, 6 };
	double data1[4] = { 1.1, 2.2, 3.3, 4.4 };
	const char *pf[5] = { "asd", "qwasde", "zxc", "bnm", "tyu" };
	cout << "max int is " << maxn(data, 6) << endl;
	cout << "max double is " << maxn(data1, 4) << endl;
	cout << "max string is " << maxn(pf, 5) << endl;
	return 0;
}
template <typename T>
T maxn(T arr[], int n)
{
	T Max = arr[0];
	for (int i = 1; i < n; ++i)
		if (arr[i] > Max)
			Max = arr[i];
	return Max;
}
template <> const char * maxn(const char * arr[], int n)
{
	const char * m = arr[0];
	for (int i = 1; i < n; ++i)
	{
		if (strlen(arr[i]) > strlen(m))
			m = arr[i];
	}
	return m;
}

7.

#include
using namespace std;
template <typename T>
T SumArray(T arr[], int n);
struct debts
{
	char name[50];
	double amount;
};
template <typename T>
T SumArray(T *arr[], int n);
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 << "sum of things is " << SumArray(things, 6) << endl;
	cout << "sum of mr_E is " << SumArray(pd, 3) << endl;

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

你可能感兴趣的:(c++,primer,plus,C++,Primer,Plus,第六版,第八章函数探幽,答案)