C++ Primer Plus第八章编程练习答案参考

这一章的题目相对来说较少,但是个人认为也比前面的稍难了,还有一道题我做的时候查过别人的答案,发现都是文不对题的,待会会标注出来。
1.

#include<iostream>
using namespace std;

int count=0;
void show(const char *str, int n=0);

int main()
{
	const char* str = "Good night~";
	show(str);
	show(str,10);
	show(str,-2);
	show(str);
	show(str,4);
	
	return 0;
}

void show(const char *str, int n)
{
	++count;
	if(n==0)
		cout << *str << endl;
	else
	{
		for(int i=0; i<count; i++)
		{
			cout << str << endl;
		}
	}
	cout << "-------------------" << endl;
}
#include<iostream>
#include<cstring>
using namespace std;

const int size = 40;

struct CandyBar
{
	char name[size];
	double wight;
	int heat;
};
void set(CandyBar& c, char* n="Millennium Munch", double w=2.85, int h=350);
void show(const CandyBar& c);

int main()
{
	CandyBar candy;
	set(candy, "Millennium Munch", 2.85, 350);
	show(candy);
	
	return 0;
}

void set(CandyBar& c, char* n, double w, int h)
{
	//字符串复制,不能直接将指针赋值给常量
	strcpy(c.name, n);
	c.wight = w;
	c.heat = h;
}
void show(const CandyBar& c)
{
	cout << "Name:" << c.name << endl
	<< "Wight:" << c.wight << endl
	<< "Heat:" << c.heat << endl;
}
#include<iostream>
#include<cctype>
using namespace std;

void show(string str);

int main()
{
	string str;
	cout << "Enter a string(q to quit): " ;
	while(getline(cin, str) && str[0]!= 'q')
	{
		show(str);
		cout << "Next string(q to quit): " ;
	}
	cout << "Bye." << endl;
	
	return 0;
}
void show(const string str)
{
	for(int i=0; str[i]; i++)
	{
		cout << (char)toupper(str[i]);
	}
	cout << endl;
}
#include<iostream>
#include<cstring>
using namespace std;
struct stringy
{
	char* str;
	int ct;
};

void set(stringy &str, char* ch);
void show(const stringy str, int n=1);
void show(const char* ch, 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 &s, char* ch)
{
	int len = strlen(ch);
	char* test = new char[len];
	s.str = test;
	strcpy(test, ch);
	s.ct = len;
}
void show(const stringy str, int n)
{
	for(int i=0; i<n; i++)
	{
		cout << str.str << endl
		<< str.ct << endl;
	}
}
void show(const char* ch, int n)
{
	int len = strlen(ch);
	while(n-->0)
	{
		for(int i=0; i<len; i++)
		{
			cout << ch[i];
		}
		cout << endl << len << endl;
	}

}
#include<iostream>
using namespace std;

template<typename T>
T max5(T arr[]);

int main()
{
	int in_arr[5] = {1,2,3,4,5};
	double dou_arr[5] = {1.5,2.5,3.5,4.5,5.5};
	
	int x = max5(in_arr);
	double y = max5(dou_arr);
	cout << x << endl << y << endl;
	return 0;
}

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

6.题目:编写模板函数maxn(),它将由一个T类型元素的数组作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中指针的数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回期中第一个字符串的地址。使用5个字符串指针组成的数组来测试该具体化。
我做的时候有点懵逼,因为我知道要返回地址,然后我自己写的一直都是返回字符串,就开始搜别人的。一搜发现,大家都是返回的字符串……然后我就只能自己整了

#include<iostream>
#include<cstring>
using namespace std;

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

template<> char* maxn(char* ch[], int n)
{
	char* address = ch[0];
	for(int i=1; i<n; i++)
	{
		if(strlen(address)<strlen(ch[i]))
		{
			address = ch[i];
		}
	}
	
	return address;//这里书中题目要求返回的是字符串的地址,而不是值
}

int main()
{
	int in_arr[6] = {6,5,4,3,7,2};
	double dou_arr[4] = {5.5,2.5,8.5,3.5};
	char* arr[] = {"abc","dasg","gagsd","dagsad","ajkgd","dsgsg","dkgjs","dsfd","dgasgss"};
	cout << "Int_arr_max: " << maxn(in_arr, 6) << endl
	<< "Double_arr_max: " << maxn(dou_arr, 4) << endl
	<< "The max length string's address is " ;
/*
	我一开始这里没有用一个char类型的指针去接收maxn()返回的指针
	然后也无法对函数取地址,所以在这里卡了好一会儿才折腾成了
	拖太久了,前面的知识遗忘得厉害
*/
	char* address = maxn(arr,3);
	cout << &address << endl;
}

#include<iostream>
using namespace std;
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;
	for(int i=0; i<n; i++)
	{
		sum += *arr[i];
	}
	
	return sum;
}

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 sum:\n"
	<< SumArray(things, 6) << endl;
	cout << "Listing Mr.E's debts sum:\n"
	<< SumArray(pd, 3) << endl;
	
	return 0;
}

以上,欢迎提问以及指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值