C++ Primer Plus 第八章答案 函数探幽

复习题加编程练习

//8.7
//1
只有一行代码的小型,非递归函数适合作为内联函数

//2
void song(const char* name, int times = 1);

只有原型包含默认值的信息

void song(const char* name = "Oh,My Papa!", int times = 1);

//3
void iquote(int n)
{
	cout << '"' << n << '"';
}

void iquote(double n)
{
	cout << "\"" << n << "\"";
}


void iquote(string str) 
{
	cout << '"' << str << '"';
}

//4
void display(const box& box)
{
	cout << "maker = " << box.maker << endl;
}

void calculate(box& box)
{
	box.volume = box.height * box.width * box.length;
}

//5
先改原型
void fill(std::array& pa);
void show(std::array& da);
改调用
fill(expenses);
改fill函数
cin >> (*pa)[i]; 改成cin >> pa[i];
改show的函数头
void show(std::array& da)

//6
默认参数和函数重载都可以完成
double mass(double density, double volume = 1);

double mass(double density, double volumn);
double mass(double density);

不能使用默认参数,因为必须从左到右指定参数, 可以用函数重载
void repeat(int times, const char* pstr);
void repeat(const char* pstr);

只能使用函数重载
int average(int a, int b);
double average(double x, double y);

不能这么做,因为两个版本特征标会相同             //没看懂题

//7
template 
T bigger(T a, T b)
{
	return(a > b ? a : b);
}

//8
template<>box bigger(const box& a, const box& b)
{
	return a.volume > b.volume ? a : b;
}

//9
float float& float& int double

practice 1
//#include
//using namespace std;
//
//void Print(char* str, int n = 0);
//
//int main()
//{
//	char str[20];
//	cout << "输入一个字符串:";
//	cin >> str;
//	Print(str);
//	int n = 0;
//	cout << "再输入一个字符串和一个数字:";
//	cin >> str >> n;
//	Print(str, n);
//
//	return 0;
//}
//
//void Print(char* str, int n)
//{
//	if (n != 0)
//	{
//		Print(str, n - 1);
//	}
//	cout << "第" << n + 1 << "次调用Print: " << str << endl;
//}






practice 2
//#include
//#include
//using namespace std;
//
//struct CandyBar
//{
//	char name[30];
//	double weight;
//	int calorie;
//};
//char name1[30] = "Millennium Munch";
//void setdata(CandyBar& candy, const char* name =name1,
//	const double weight = 2.85, const int calorie = 350);
//void display(const CandyBar& candy);
//
//int main()
//{
//	CandyBar candy1, candy2;
//	char name[30];
//	double weight = 0.0;
//	int calorie = 0;
//	cout << "输入第一个糖果的数据:";
//	cin.getline(name, 30);
//	cin >> weight >> calorie;
//	setdata(candy1, name, weight, calorie);
//	setdata(candy2);
//	cout << "两个糖果的信息显示:" << endl;
//	display(candy1);
//	display(candy2);
//	return 0;
//}
//
//void setdata(CandyBar& candy, const char* name, const double weight, const int calorie)
//{
//	strcpy_s(candy.name, name);			//candy.name = name1;地址不能赋给地址,用string或strcpy_s或strcpy)
//	candy.weight = weight;
//	candy.calorie = calorie;
//}
//
//void display(const CandyBar& candy)
//{
//	cout << "name=" << candy.name << endl;
//	cout << "weight=" << candy.weight << endl;
//	cout << "calorie=" << candy.calorie << endl;
//}






practice 3
//#include
//#include
//#include
//#include
//
//using namespace std;
//
//string transform(string& str);
//
//int main()
//{
//	cout << "Enter a string (q to quit): ";
//	string str1;
//	while (getline(cin,str1))
//	{
//		if (str1[0] == 'q')
//		{
//			cout << "Bye!" << endl;
//			break;
//		}
//		else
//		{
//			str1 = transform(str1);
//			cout << str1 << endl;
//			cout << "Next string (q to quit): ";
//		}
//	}
//
//	return 0;
//}
//
//string transform(string& str)
//{
//	for (int i = 0; i < str.size(); i++)
//		str[i] = toupper(str[i]);
//	return str;
//}




/*
//practice 4
#pragma warning( disable : 4996)  

#include 
#include 
                
using namespace std;

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

void set(stringy& str, const char*);
void show(const stringy&, int n = 1);
void show(const char*, int n = 1);

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& str, const char* t)
{
	char* copy_t= new char[strlen(t) + 1];      //strlen(str1)是计算字符串的长度,不包括字符串末尾的“\0”!!!
	//strcpy_s(str.str, t);                     //两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
	//链接https://blog.csdn.net/johnny710vip/article/details/6681160?ops_request_misc=&request_id=&biz_id=
	//102&utm_term=strcpy_s&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-8-.
	//pc_search_result_before_js&spm=1018.2226.3001.4187
	strcpy(copy_t, t);              //在include前加上#pragma warning( disable : 4996),否则会报错要求使用strcpy_s
	str.str = copy_t;
	str.ct = strlen(t) + 1;
}

void show(const stringy& str, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << str.str << endl;
	}
	cout << endl;
}

void show(const char* str, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << str << endl;
	}
	cout << endl;
}
*/



/*
//practice 5
#include

using namespace std;

template 
T max(const T*);

int main()
{
	int one[5] = { 5,8,2,4,5 };
	double two[5] = { 2.5,8.9,99.2,5.1,5.5 };
	cout << max(one) << endl;
	cout << max(two) << endl;
	return 0;
}

template
T max(const T* t)
{
	T temp = t[0];
	for (int i = 1; i < 5; i++)
		if (temp < t[i])
			temp = t[i];
	return temp;
}
*/




/*
//prcatice 6
#include
#include

using namespace std;

template
T maxn(T*, int);
template <>char* maxn(char**, int);

int main()
{
	int test1[6] = { 1,3,8,6,4, };
	double test2[4] = { 1.1,2.2,5.5,9.9 };
	const char* test3[5] =
	{
		"yuetian",
		"yue tiann",
		"yeutianyues",
		"yue",
		"yuetianyues",
	};
	cout << "The max number of test1: " << maxn(test1, 6) << endl;
	cout << "The max number of test2: " << maxn(test2, 4) << endl;
	cout << "The longgest member's address: " <<(void*) maxn(test3, 4) << endl;
	//需要转换为void*指针,重载的操作符<<遇到地址会自动输出字符串
	cout << "The adderss is the pointer of: " << maxn(test3, 4) << endl;
	cout << "Done!" << endl;
	return 0;
}

template
T maxn(T* array, int n)
{
	T temp = array[0];
	for (int i = 1; i < n; i++)
	{
		if (temp < array[i])
			temp = array[i];
	}
	return temp;
}

template <>char* maxn(char** str, int n)
{
	int i = 0;
	char* address=str[0];
	for (i = 1; i < n; i++)
	{
		if (strlen(address) < (strlen(str[i])))
			address = str[i];
	}
	return address;
}
*/






//practice 7
#include
using namespace std;

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

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

int main()
{
	int things[6] = { 13,31,103,310,130 };
	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 sum of things: " << SumArray(things, 6) << endl;
	cout << "The sum of amount of debt: " << SumArray(pd, 3) << endl;

	return 0;
}

template
T SumArray(T arr[], int n)
{
	T temp = 0;
	for (int i = 0 ; i < n; i++)
		temp += arr[i];

	return temp;
}

template
T SumArray(T* arr[], int n)
{
	T temp = 0;
	for (int i = 0; i < n; i++)
		temp += *arr[i];

	return temp;
}

你可能感兴趣的:(c++)