第八章 编程练习 (C++ Primer Plus)

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

#include <iostream>
using namespace std;

void print(char * str, int n = 0);
 
int main()
{
    char str[20] = "leonardo liu";
    print(str);
    print(str, 5);
    print(str, 16);
    return 0;
}
 
void print(char * str, int n)
{
    static int flag = 0;
    flag++;
    if (n == 0)
	   cout << str << endl;
    else
    {
        for (int i = 0; i < flag; i++)
            cout << str << endl;
    }
    cout << endl;
    return;
}

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

#include <iostream>
#include <cstring>

struct CandyBar{
	std::string name;
	double weight;
	int heat;
}; 

void SetFun(CandyBar &, char * na = "Milennium munch", 
			double wt = 2.85, int ht = 350);
void Show(const CandyBar & cb);

int main()
{
	CandyBar cb = {"jingliming",3.44,220};
	SetFun(cb);
	Show(cb);
	return 0;
}

void SetFun(CandyBar & cb, char * na, double wt , int ht)
{
	using namespace std;
	cout << "name: " << cb.name << endl;
	cout << "weight: " << cb.weight << endl;
	cout << "heat: " << cb.heat << endl;
	
	cb.name = na;
	cb.weight = wt;
	cb.heat = ht;
}

void Show(const CandyBar & cb)
{
	using namespace std;
	cout << "name: " << cb.name << endl;
	cout << "weight: " << cb.weight << endl;
	cout << "heat: " << cb.heat << endl;	
}

3.编写一个函数,它接受一个指向 string对象的引用作为参数,并将该 string对象的内容转换为大写,为此可使用表64描述的函数 toupper().然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下
Enter a string (g to quit): go away
GO AWAY
Next string (a to quit): good grief!
GOOD GRIEF
Next string (a to quit): g
Bye.

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void ConvertFun(std::string &);

int main()
{
	string word;
	cout << "Enter a string (q to quit): ";
	while(getline(cin,word) && word != "q")
	{
		ConvertFun(word);
		cout << word << endl;
		cout << "Next string (q to quit):";
	}
	cout << "Bye." << endl;
	return 0;
}

void ConvertFun(std::string & str)
{
	for(int i = 0; i != str.size(); i++)
		str[i] = toupper(str[i]);
}

4.下面是一个程序框架:

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

#include <iostream>
using namespace std;

#include <cstring> // for strlen() ,strcpy()
struct stringy{
	char * str;
	int ct; // length of string (not counting '\0')
};

// prototypes for set(), show(), and show() go here
void set(stringy &, const char []);
void show(const stringy &, const int n = 1);
void show(const char [], const int n = 1);

int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	set(beany,testing);// first argument is a reference,
						// allocates space to hold copy of testing,
						// sets str member of beany to point to the 
						// new block, copies testing to new block,
						// and sets ct member of beany
	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 arr[])
{
	int n = strlen(arr);
	char * pt = new char [n+1];
	strcpy(pt,arr);
	str.str = pt;
	str.ct = n;
//	delete [] pt; //此处有问题,这里我不知道怎么处理,若有心人发现,请告诉我!!!谢谢
}

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

void show(const char str[], const int n)
{
	for (int i = 0; i != n ; i++)
	{
		cout << str << endl;
	}
	cout << endl;
}
注意:本菜鸟认为写的这个程序有问题!!!

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

#include <iostream>

using namespace std;

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

int main()
{
	int str[5] = {0, 1, 2, 3, 4};
	double arr[5] = { 6.0, 7.0, 9.0, 4.0, 1.0};
	
	cout << "int max: " << max5(str) << endl;
	cout << "double max: " << max5(arr) << endl;
	return 0;
}

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

#include <iostream>

using namespace std;

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

int main()
{
	int arr[4] = {0, 4, 3, 1};
	double str[10];
	for(int i = 0; i != 10; i++)
	{
		str[i] = ++i;
	}
	cout << "int max: " << maxn(arr, 4) << endl;
	cout << "double max: " << maxn(str, 10) << endl;
	return 0;
}

7.修改程序清单8.14,使其使用两个名为 SumArrayo的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示 thing的总和以及所有debt的总和。

#include <iostream>

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

template <typename T>
T ShowArray(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 << "sum = "<<ShowArray(things, 6) << endl;
	cout << "Listing Mr`s debts:\n";
	cout << "sum = "<<ShowArray(pd, 3)<< endl;
	return 0;
}

template <typename T>
T ShowArray(T arr[], int n)
{
	using namespace std;
	cout << "template A\n";
	
	T sum = 0;
	for (int i = 0; i < n ; i++)
		sum += arr[i];
	return sum;
}

template <typename T>
T ShowArray(T * arr[], int n)
{
	using namespace std;
	cout << "template B\n";
	
	T sum = 0;
	for (int i = 0; i < n; i++)
		sum += *arr[i];
	return sum;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值