C++ primer plus习题及解析第八章(函数探幽)

题目:8.1

1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。

 代码:

void PrintChar(string ch, int count = 0)
{
	if (count <= 0)
	{
		return;
	}
	cout << ch << endl;
	PrintChar(ch, count - 1);
	
}

题目:8.2

CandyBar结构包含3个成员。第一个成员存储 candy bar 的品牌名称;第二个成员存储 candy bar 的重量(可能有小数); 第三个成员存储 candy bar 的热量(整数)。请编写一个程序,它使用一个这样的函数,即将 CandyBar 的引用、char指针、double 和 int 作为参数,并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为 "Millennium Munch"、2.85和350。另外,该程序还 包含一个以 CandyBar 的引用为参数,并显示结构内容的函数。请尽可能使用 const

代码 

struct CandyBar
{
	char name[20];
	double weight;
	int heat;
};

void showInformation(CandyBar& bar)
{
	cout << bar.name << endl;
	cout << bar.weight << endl;
	cout << bar.heat << endl;
}

void Func(CandyBar&bar, void (*showInformation)(CandyBar&),const char*name= "Millennium Munch",
	double weight=2.85,int heat=350)
{
	strcpy_s(bar.name, name);
	bar.weight = weight;
	bar.heat = heat;
	showInformation(bar);
}

int main()
{
	CandyBar bar;
	Func(bar, showInformation);

	system("pause");
	return 0;
}

题目: 8.3

 编写一个函数,它接受一个指向 string 对象的引用作为参数,并将该 string 对象的内容转换为大写,为此可使用表6.4描述 的函数 toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:

 Enter a string (q to quit): gi away
 GO AWAY
 Next string (q to quit): good grief!
 GOOD GRIEF!
 Next string (q to quit): q
 Bye.

 代码:

void Func()
{
	string ch;
	cout << "Enter a string (q to quit) :";
	
	while (cin >> ch && ch != "q")
	{
		string newch="";
		for (int i = 0; i < ch.size(); i++)
		{
			newch += toupper(ch[i]);
		}
		cout << newch << endl;

		cout << "Next string (q to quit) :";
	}


}

题目:8.4

下面是一个程序框架:

#include <iostream>
#include <cstring>

using namespace std;

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

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

 请提供其中描述的函数和原型,从而完成该程序。注意,应有两个 show() 函数,每个都使用默认参数。请尽可能使用 cosnt参数。 set() 使用 new 分配足够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名 称,删除using编译指令,这取决于所用的编译器。)

代码: 

struct stringy 
{

	char* str; //points to a string

	int ct; //length of string (not couting '\0')

};

void set(stringy& beany, const char* texting)
{
	
	int count = strlen(texting);
	beany.str = new char[count];
	//最后一位用来存放结束符'\0'
	strcpy_s(beany.str,count+1, texting);
	beany.ct = count;
}

void show(stringy& beany, int n = 1)
{
	
	if (n <= 0)
	{
		return;
	}
	cout << "--------------------show(beany)-----------------------" << endl;
	cout << beany.str << endl;
	cout << beany.ct << endl;
	show(beany, n - 1);
}

void show(const char* testing, int n = 1)
{
	
	if (n <= 0)
	{
		return;
	}
	cout << "--------------------------show(testing)---------------------------" << endl;
	/*int len = sizeof(testing) / sizeof(testing[0]);*/
	int len = strlen(testing);
	for (int i = 0; i < len; i++)
	{
		cout << testing[i];
	}
	cout << endl;
	show(testing, n - 1);
}

题目:8.5

 编写模板函数 max5(),它将一个包含5个 T 类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循 环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将 T 替换为一个包含5个 int 值的数组和一个包含5个 double 值的数组,以测试该函数。

代码: 

template<typename T>
T max5(T* arr)
{
	T max=arr[0];
	for (int i = 0; i < 5; i++)
	{
		if (max < arr[i])
		{
			max = arr[i];
		}
	}
	return max;
}
int main()
{
	int arr[5] = { 1,2,3,4,5 };
	int max=max5<int>(arr);
	cout << "max_int=" << max << endl;


	system("pause");
	return 0;
}

题目:8.6

编写模板函数 maxn(),它将由一个 T 类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。 在程序对它进行测试,该程序使用一个包含6个 int 元素的数组和一个包含4个 double 元素的数组来调用该函数。程序还包含一个具体化,它 将 char 指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用 由5个字符串指针组成的数组来测试该具体化。

代码: 

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

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

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

int main(void)
{
	int things[6] = { 13,31,103,301,310,130 };
	struct debts me_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] = &me_E->amouunt;

	cout << "Listing Mr.E's counts:" << endl;
	cout << SumArray(things, 6) << endl;

	cout << "Listing Mr.E's debts:" << endl;
	cout << SumArray(pd, 3) << endl;

	system("pause");
	return 0;
}
template<typename T>
T SumArray(T arr[], int n)
{
	T sum = 0;
	cout << "template A" << endl;
	for (int i = 0; i < n; i++)
		sum += arr[i];
	return sum;
}
template<typename T>                  
T SumArray(T* arr[], int n)
{
	T sum = 0;
	cout << "template B" << endl;
	for (int i = 0; i < n; i++)
		sum += *arr[i];
	return sum;
}

  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值