C++Primer Plus(第6版)第八章编程练习答案

第一题

#include<iostream>
#include<cstdlib>

unsigned int count = 0;


inline void print(const char*,int n = 1);//这道题没看懂打印次数不能和第二个参数值一样 如果能和第二个参数值一样的话第二个参数就可以使用引用 以及函数的返回值
//指针什么的都能用 既然不能和第二个参数值一样 目前我能想到的就这些 就定义了全局变量count 如果还有什么办法请通知下
int main()
{
	using namespace std;
	print("hello");
	print("hello");
	print("hello");
	print("hello");
	print("hello");

	return 0;
}

void print(const char* str,int n)
{
	using std::cout;
	using std::endl;

	count++;
	if (!n)
		exit(1);

	for (unsigned i = 1; i <= count; i++)
	{
		cout << str << endl;
	}
	cout << endl << endl;
}

第二题

#include<iostream>
#include<string>
struct CandyBar
{
	std::string candy_b;//建议还是char str[]
	double candy_a;
	int candy_r;
};

void scan(CandyBar&,const char*, double, int);
void print(const CandyBar&);

int main()
{
	CandyBar candy;
	scan(candy, "Millennium Munch", 2.85, 350);
	print(candy);

	return 0;
}

void scan(CandyBar& candy, const char* can_b, double can_a, int can_r)
{
	candy.candy_b = can_b;
	candy.candy_a = can_a;
	candy.candy_r = can_r;
}

void print(const CandyBar& candy)
{
	using std::cout;
	using std::endl;

	cout << candy.candy_b << endl
		<< candy.candy_a << endl
		<< candy.candy_r << endl;
}

第三题

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

void toupper_str(string&);

int main()
{
	string str;

	cout << "Enter a string (q to quit): ";
	getline(cin, str);
	while (str[0] != 'q' || str[1] != '\0')
	{
		toupper_str(str);
		cout << "Next string (q to quit): ";
		getline(cin, str);
	}
	
	return 0;
}

void toupper_str(string& str)
{
	int i = 0;
	while (str[i])
	{
		str[i] = toupper(str[i]);
		i++;
	}
	cout << str << endl;
}

第四题

#define _CRT_SECURE_NO_WARNINGS 3//如果是用vs2022编译器请在这里添加宏定义不然strcpy函数用不了windows会让你用安全版本的strcpy函数后面数字随便填写
//宏定义没有分号记住

#include<iostream>
#include<cstring>
using namespace std;
struct stringy
{
	char* str;//指向字符串
	int ct;//字符串长度(不包括“0”)
};

//set(),show() 和 show() 的原型到这里
void set(stringy&, const char*);

void show(stringy& beany, int m = 1);
void show(const char* beany, int m = 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& beany, const char* str1)
{
	beany.ct = strlen(str1);
	beany.str = new char[beany.ct];
	strcpy(beany.str, str1);
}

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

void show(stringy& beany, int m)
{
	for (int i = 1; i <= m; i++)
		cout << beany.str << endl;

	cout << endl << endl;
}

第五题

#include<iostream>
template <typename T>
auto max5(T a[])->T;

int main()
{
	using namespace std;
	int a[5]{ 1,2,3,4,5 };
	double b[5]{ 1.2, 2.2, 4.2, 1.1, 3.1};
	//auto max_int = max5(a);
	//auto max_dou = max5(b);
	int max_int = max5(a);
	double max_dou = max5(b);
	cout << max_int << endl << max_dou;

	return 0;
}

template <typename T>
auto max5(T a[])->T//T max5(T a[])可以这样写 写成这样只不过对这个形式加深一点
{
	T temp= a[0];
	for (int i = 1; i < 5; i++)
	{
		if (temp < a[i])
			temp = a[i];
	}

	return temp;
}

第六题

#include<iostream>
using namespace std;

template <class T>//函数模板
T maxn(T a[], int n);

template<>const char* maxn<const char*>(const char**str, int);//显示具体化

int main()
{
	int a1[] = { 1, 2 , 3 , 4 ,5 ,6 , 7};
	double a2[]{ 1.1,2.1,3.1,1.9 };
	const char* str[4] = { "hello","order","fixed","ooooo"};//vs比较严格 后面要加const因为每个指针指向的都是一个字符串常量
	int max_int = maxn(a1, 7);//; 
	double max_dou = maxn(a2, 4);
	const char* p = maxn(str,4);
	cout << max_int << endl
		 << max_dou << endl
		 << p << endl;

	return 0;
}

template <class T>
T maxn(T a[], int n)
{
	T  temp;
	temp = a[0];
	for (int i = 1; i < n; i++)
	{
		if (temp < a[i])
			temp = a[i];
	}

	return temp;
}

template<>const char* maxn<const char*>(const char** str, int n)
{
	int i, j = 0;
	size_t temp;

	temp = strlen(str[0]);
	for (int i = 1; i < n; i++)
	{
		if (temp < strlen(str[i]))
		{
			temp = strlen(str[i]);
			j = i;
		}
	}
	return str[j];
}

第七题

#include<iostream>

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

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

template<> void ShowArray<debts>(debts de[], int n);

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.1}
	};
	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";
	//things is an array of int
	ShowArray(things, 6);//uses template A
	cout << "Listing Mr.E's debts:\n";
	//pd is an array of pointers to double
	ShowArray(mr_E, 3);//uses template B(more specialized)
	return 0;
}

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

template<> void ShowArray<debts>(debts de[], int n)
{
	using std::cout;
	using std::endl;

	decltype(de->amount) sum = 0;
	for (int i = 0; i < n; i++)
	{
		sum += de[i].amount;
	}

	cout << "debts: " << sum;

	return;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值