C++ Primer Plus 第6版第八章課後習題

#1

// 1.cpp
#include <iostream>

// #1
template <class T>
void ShowStr(T str[], int n);

// #2
int ShowStr(char str[]);

// Show Call Times
void ShowTimes(char str[], int n = 0);

int main()
{
	char Hello[] = { "Hello My Friend" };
	ShowStr(Hello, 5); // #1
	int len = ShowStr(Hello);    // #2
	std::cout << len * 4 << std::endl;

	// Testing 
	using namespace std;
	ShowTimes(Hello);
	ShowTimes(Hello, 15);
	ShowTimes(Hello, 10);
	ShowTimes(Hello);
}

template <class T>
void ShowStr(T str[], int n) {
	for (int i = 0; i < n; i++)
		std::cout << str << '\n';
}

int ShowStr(char str[]) {
	using namespace std;
	cout << str << '\n';
	return (sizeof str);
}

void ShowTimes(char str[], int n) {
	using namespace std;
	static int times = 0;
	if (n == 0)
		cout << "first call.\n";
	else {
		++times;
		cout << "call: " << times << ".\n";
	}
}

#2

// 2.cpp 
#include <iostream>
#include <string>
//#include <string>
using namespace std;

const int LEN { 20 };

struct CandyBar {
	char brand[LEN]; // 品牌的名稱
	double weight;   // 產品的重量
	int calorie;     // 產品的卡路里
};

void InitBar(char brand[], double &weight, int &calorie);

template <class SBar>
void ShowBar(SBar &Bar);

int main()
{
	CandyBar Bar;
	InitBar(Bar.brand, Bar.weight, Bar.calorie);
	ShowBar<CandyBar>(Bar);
	return 0;
}

void InitBar(char brand[], double &weight, int &calorie) {
	strcpy(brand, "Millennium Munch");
	weight = 2.85;
	calorie = 350;
}

template <class SBar>
void ShowBar(SBar &Bar) {
	cout << "Company: " << Bar.brand << endl;
	cout << "Weight: " << Bar.weight << endl;
	cout << "Calorie: " << Bar.calorie << endl;
}

#3

// 3.cpp 
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// template <class T>
// T ConvertBL(T &Str>
string ConvertBL(string &Str);

int main()
{
	string Str;
	cout << "Enter a string (q to quit): ";
	getline(cin, Str);
	while (Str != "q") {
		Str = ConvertBL(Str);
		cout << Str << endl;
		cout << "Next string (q to quit): ";
		getline(cin, Str);
	}
	cout << "Bye.\n";
}

string ConvertBL(string &Str) {
	for (int i = 0; i < Str.size(); i++) {
		if (Str[i] >= 'a' && Str[i] <= 'z')
			Str[i] = toupper(Str[i]);
	}
	return Str;
}

#4

// 4.cpp 
#include <iostream>
using namespace std;
#include <cstring> // for strlen(), strcpy()
struct stringy {
	char * str; // points of string
	int ct; // length of string (not '\0')
};

/* Copy source string to destion string */
void set(stringy &stry, char src[]);
/* Show string */
void show(stringy stry, const int n = 1);
void show(const char str[], const int n = 1);


int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	
	set(beany, testing);
	show(beany, 1);
	show("Hello");
	return 0;
}

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

void show(stringy stry, const int n) {
	for (int i = 0; i < n; i++)
		std::cout << stry.str << '\n';
}

void show(const char str[], const int n) {
	for (int i = 0; i < n; i++)
		std::cout << str << '\n';
}

#5

// 5.cpp 
#include <iostream>
using namespace std;

template <class T>
T max5(T element[]);

int main()
{
	int element1[5] = { 3, 1, -5, 7, 0 };
	double element2[5] = { 1.2, 3.2, -1.0, 4.5, 3.2 };
	cout << "Max Element1 is " << max5(element1) << endl;
	cout << "Max Element2 is " << max5(element2) << endl;
	return 0;
}

template <class T>
T max5(T element[]) {
	T max = element[0];
	for (int i = 1; i < 5; i++) {
		if (element[i] > max)
			max = element[i];
	}
	return max;
}

#6

// 5.cpp 
#include <iostream>
using namespace std;

template <class T>
T maxn(T element[], int n = 0);

template <> char * maxn<char *>(char *p[], const int n);

int main()
{
	int element1[6] = { 3, 1, -5, 7, 4, -10 };
	double element2[4] = { 1.2, 3.2, -1.0, 4.5 };

	cout << "Max Element1 is " << maxn(element1, (sizeof element1) / sizeof(int)) << endl;
	cout << "Max Element2 is " << maxn(element2, (sizeof element2) / sizeof(double)) << endl;

	const char *test_str[] = { "C", "C++", "Java", "Python", "C#" };
	//cout << "No param, is " << maxn(test_str) << endl;
	cout << "Have param, is " << maxn(test_str, (sizeof test_str) / sizeof(char *)) << endl;

	return 0;
}

template <class T>
T maxn(T element[], int n) {
	if (n == 0)
		return 0;
	T max = element[0];
	for (int i = 1; i < n; i++) {
		if (element[i] > max)
			max = element[i];
	}
	return max;
}

template <> char * maxn<char *>(char *p[], const int n) {
	if (n == 0)
		return 0;
	char * maxstr = p[0];
	for (int i = 0; i < n; i++) {
		if (strlen(p[i]) > strlen(maxstr))
				maxstr = p[i];
	}
	return maxstr;
}

#7

// 7.cpp 
#include <iostream>

template <typename T>
T SumArray(T arr[], int n);

template <typename T>
T SumArray(T * arr[], int n);

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

int main() {
	using namespace std;
	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 << "Listing Mr. E's counts of things:\n";
	cout << "Things sum is " << SumArray(things, 6) << endl;
	cout << "Listing Mr. E's debts:\n";
	cout << "Debts sum is " << SumArray(pd, 3) << endl;

	return 0;
}

template <typename T>
T SumArray(T arr[], int n) {
	using namespace std;
	T sum = 0;

	cout << "template A\n";
	for (int i = 0; i < n; i++)
		sum += arr[i];
	return sum;
}

template <typename T>
T SumArray(T * arr[], int n) {
	using namespace std;
	T sum = 0;

	cout << "template B\n";
	for (int i = 0; i < n; i++)
		sum += *arr[i];
	return sum;
}

如有錯誤,望指正,感謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章