C++ Prime Plus第8章编程练习答案和运行结果

1. 第1题

#include <iostream>

using namespace std;
static int number = 0;//全局静态变量,所有函数都可以访问

void Printf(const char * ch, int num = 0);

int main()
{
	const char *str = "Hello World!";
	Printf(str);//第1次调用
	Printf(str, 5);//第2次调用
	Printf(str);//第3次调用
	Printf(str, 2);//第4次调用
	return 0;
}

void Printf(const char * ch, int num)
{
	number++;//每次调用加1
	if (num == 0)
	{
		cout << ch << endl;
	}
	else
	{
		for (int i = 0; i < number; i++)
		{
			cout << ch << endl;
		}
	}
	cout << endl;
}

在这里插入图片描述

2. 第2题

#include <iostream>

using namespace std;

struct CandyBar
{
	const char * name;//既可以接受const也可以接受非const参数
	double weight;
	int calorie;
};

CandyBar & setting(CandyBar & cb, const char * n = "Millennium Munch", double wt = 2.85, int c = 350);
void Show(const CandyBar &cb);//保证不修改结构const

int main()
{
	const char *ch = "Dove";
	CandyBar cb;
	cb = setting(cb);//默认参数
	Show(cb);
	cb = setting(cb, ch);//后两个参数默认
	Show(cb);
	cb = setting(cb, ch, 3.00, 400);
	Show(cb);
	return 0;
}

CandyBar & setting(CandyBar & cb, const char * n, double wt, int c)
{
	cb.name = n;
	cb.weight = wt;
	cb.calorie = c;
	return cb;
}

void Show(const CandyBar &cb)
{
	cout << "品牌名称:" << cb.name << endl;
	cout << "品牌重量:" << cb.weight << endl;
	cout << "品牌热量:" << cb.calorie << endl;
	cout << endl;
}

在这里插入图片描述

3. 第3题

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

string ToUpper(string &st);//string为内置类型,无需返回引用
int main()
{
	string st;
	cout << "Enter a string (q to quit): ";	
	getline(cin, st);
	while (st != "q")
	{
		st = ToUpper(st);
		cout << st << endl;
		cout << "Enter a string (q to quit): ";
		getline(cin, st);
	}
	cout << "Bye." << endl;
	return 0;
}

string ToUpper(string &st)
{
	int len = st.length();
	for (int i = 0; i < len; i++)
	{
		if (isalpha(st[i]))//是字母才转大写
		{
			st[i] = toupper(st[i]);
		}
	}
	return st;
}

在这里插入图片描述

4. 第4题

#include<iostream>
#include<cstring>

using namespace std;

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

void set(stringy & sy, const char * ch);
void show(const stringy sy, 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 & sy, const char * ch)
{
	sy.ct = strlen(ch) + 1;//首先确定字符串长度
	sy.str = new char[sy.ct];//分配相应长度的内存空间
	strcpy_s(sy.str, sy.ct, ch);//将字符串拷贝
}

void show(const stringy sy, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "#" << i + 1 << ": " << endl;
		cout << "str: " << sy.str << endl;
	}
	cout << endl;
}

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

在这里插入图片描述

5. 第5题

#include <iostream>

using namespace std;

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

int main()
{
	int arr1[5] = { 1,2,3,4,5 };
	double arr2[5] = { 10.0,21.0,55.0,9.5,5.5 };
	int MAX1 = max5(arr1);
	int MAX2 = max5(arr2);
	cout << "int型数组中最大的元素为:" << MAX1 << endl;
	cout << "double型数组中最大的元素为:" << MAX2 << endl;
	return 0;
}

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

在这里插入图片描述

6. 第6题

#include <iostream>
#include<cstring>

using namespace std;

template <typename T>
T maxn(T * arr,int n);

template<> char * maxn(char * arr[], int n);//显式具体化的声明

int main()
{
	int arr1[6] = { 1,2,3,4,5,6};
	double arr2[4] = { 10.0,21.0,55.0,9.5};
	const char * arr3[3] = { "abc","defg","hl" };
	int MAX1 = maxn(arr1,6);
	int MAX2 = maxn(arr2,4);
	const char * MAX3 = maxn(arr3, 3);//MAX3为最长的字符串
	cout << "int型数组中最大的元素为:" << MAX1 << endl;
	cout << "double型数组中最大的元素为:" << MAX2 << endl;
	cout << "char指针数组中最长字符串的地址为:" << &MAX3 << endl;//取地址运算符
	return 0;
}

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

//显式具体化的定义
template<> char * maxn(char * arr[], int n)
{
	int len = strlen(arr[0]);
	char *p = arr[0];
	for (int i = 1; i < n; i++)
	{
		if (len < strlen(arr[i]))
		{
			len = strlen(arr[i]);
			p = arr[i];//p为最长的字符串
		}
	}
	return p;
}

在这里插入图片描述

7. 第7题

#include <iostream>

using namespace std;

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()
{
	int things[6] = { 13,31,103,301,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;
	}

	int sum1 = SumArray(things, 6);
	int sum2 = SumArray(pd, 3);
	cout << "things数组元素之和为:" << sum1 << endl;
	cout << "pd指针数组所指的元素之和为:" << sum2 << endl;
	return 0;
}

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

template<typename T>
T SumArray(T * arr[], int n)
{
	T total = 0;
	for (int i = 0; i < n; i++)
	{
		total = total + *arr[i];
	}
	return total;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值