C++ Primer Plus课后复习题及编程练习第八章-函数探幽

8.7 复习题

1.只有一行代码的小型、非递归函数适合作为内联函数。

在函数声明定义的时候,在前面加”inline“。能够提高代码运行效率,缺点是占用内存。

2.a,void song(const char* name, int times = 1);

3.

void iquote(int i) {
	cout << "\"" << i << "\"" << endl;
}

void iquote(double i) {
	cout << "\"" << i << "\"" << endl;
}

void iquote(string i) {
	cout << "\"" << i << "\"" << endl;
}

4.

//a.
void print_all(box& b) {
	cout << b.maker << endl;
	cout << b.height << endl;
	cout << b.width << endl;
	cout << b.length << endl;
}

//b.
void product(box& b) {
	b.volume = b.height * b.length * b.width;
	cout << b.volume << endl;
}

6.

//a.
double mass(double density, double volume) {
	double weight = 0;
	weight = density * volume;
	return weight;
}
double mass(double density) {
	double weight = 0;
	weight = density;
	return weight;
}

//b.
void print_string(char* ch, int n) {
	for (int i = 0; i < n; i++) {
		cout << ch << endl;
	}
}
void print_string(char* ch) {
	for (int i = 0; i < 5; i++) {
		cout << ch << endl;
	}
}

7.8.

#include<iostream>
using namespace std;

struct box {
	float height;
	float width;
	float length;
	float volume;
};

double more(double a, double b);
float more(box b1, box b2);

int main() {

	box b1 = { 10,10,5 };
	box b2 = { 5,5,10 };
	cout << more(b1, b2) << endl;
	cout << more(10,20) << endl;

	system("pause");
	return 0;
}


double more(double a, double b) {
	if (a > b) return a;
	else
	{
		return b;
	}
}

float more(box b1, box b2) {
	b1.volume = b1.height * b1.length * b1.width;
	b2.volume = b2.height * b2.length * b2.width;
	float bigger = more(b1.volume, b2.volume);
	return bigger;
}

8.8 编程练习

1.

#include<iostream>
using namespace std;

int main() {

    //1.
	void print_str(char* ch, int length=1);
	char ch[20] = "sdsahjdjs";
	print_str(ch,10);

	system("pause");
	return 0;
}

void print_str(char* ch, int length) {
	for (int i = 0; i < length; i++) {
		cout << ch << endl;
	}
}

2.

#include<iostream>
using namespace std;

struct CandyBar {
	string name;
	double weight;
	int calorie;
};

int main() {

    //2.
	void craet_candy(CandyBar & candy, string s = "Millennium Munch", double w = 2.85, int cal = 350);
	void print_candy(CandyBar & candy);

	CandyBar candy;
	craet_candy(candy);
	print_candy(candy);


	system("pause");
	return 0;
}

void craet_candy(CandyBar& candy, string s, double w, int cal) {
	candy.name = s;
	candy.weight = w;
	candy.calorie = cal;
}

void print_candy(CandyBar& candy) {
	cout << candy.name << endl;
	cout << candy.weight << endl;
	cout << candy.calorie << endl;
}

3.

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

int main() {

    //3.
	void upper(string & str);
	string str;
	cout << "Enter a string (q to quit): ";
	getline(cin, str);
	upper(str);
	cout << "Bye! " << endl;


	system("pause");
	return 0;
}

void upper(string& str) {
	while (str != "q") {
		for (int i = 0; i < str.size(); i++) {
			str[i] = toupper(str[i]);
			cout << str[i];
		}
		cout << "\nNext string (q to quit): ";
		getline(cin, str);
	}
}

4.

#define _CRT_SECURE_NO_WARNINGS   //strcpy函数可能会报错,加这一行可以解决
#include<iostream>
#include<string>
using namespace std;

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

int main() {

    //4.
	void set(stringy& str, char* test);
	void show(const stringy& str, int count = 1);
	void show(const string& str, int count = 1);

	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!");


	system("pause");
	return 0;
}

void set(stringy& str, char* test) {
	str.ct = strlen(test);
	str.strcat = new char[str.ct];
	strcpy(str.strcat, test);
}

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

void show(const string& str, int count) {
	for (int i = 0; i < count; i++) {
		cout << str << endl;
	}
}

5.

#include<iostream>
using namespace std;

template<typename T>
T max5(T number[5]);  //模板函数声明只能在全局

int main() {

    //5.
	int number1[5] = { 1,3,2,5,4 };
	cout << max5(number1) << endl;

	double number2[5] = { 1.1,2.5,1.9,4.6,2.3 };
	cout << max5(number2) << endl;


	system("pause");
	return 0;
}

template<typename T>
T max5(T number[5]) {
	T Max;
	for (int i = 0; i < 4; i++) {
		if (number[i] > number[i + 1])
			Max = number[i];
		else
			Max = number[i + 1];
	}
	return Max;
}

6.

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

template<typename T>
T maxn(T str[], int count);

template<>char* maxn(char* p[], int count);

int main() {

    //6.
	int num[] = { 1,4,3,2 };
	int count = sizeof(num) / sizeof(num[0]);
	cout << maxn(num, count) << endl;

	double num2[] = { 1.1,2.3,6.8,9.5,4.0,3.6,5.4 };
	int count2 = sizeof(num2) / sizeof(num2[0]);
	cout << maxn(num2, count2) << endl;
	 
	string str[] = { "hello","helloworldpluspro","helloworldplus"};
	int count3= sizeof(str) / sizeof(str[0]);
	cout << maxn(str, count3) << endl;

	system("pause");
	return 0;
}

template<typename T>
T maxn(T str[], int length) {
	T Max = str[0];
	for (int i = 0; i < length; i++) {
		if (Max < str[i])
			Max = str[i];
	}
	return Max;
}

template<>char* maxn<char*>(char* p[], int count) {
	int flag = 0;
	for (int i = 0; i < count; i++) {
		if (strlen(p[flag]) < strlen(p[i]))
			flag=i;
	}
	return p[flag];
}

6.

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

template<typename T>
T maxn(T str[], int count);

template<>char* maxn(char* p[], int count);

int main() {

    //6.
	int num[] = { 1,4,3,2 };
	int count = sizeof(num) / sizeof(num[0]);
	cout << maxn(num, count) << endl;

	double num2[] = { 1.1,2.3,6.8,9.5,4.0,3.6,5.4 };
	int count2 = sizeof(num2) / sizeof(num2[0]);
	cout << maxn(num2, count2) << endl;
	 
	string str[] = { "hello","helloworldpluspro","helloworldplus"};
	int count3= sizeof(str) / sizeof(str[0]);
	cout << maxn(str, count3) << endl;

	system("pause");
	return 0;
}

template<typename T>
T maxn(T str[], int length) {
	T Max = str[0];
	for (int i = 0; i < length; i++) {
		if (Max < str[i])
			Max = str[i];
	}
	return Max;
}

template<>char* maxn<char*>(char* p[], int count) {
	int flag = 0;
	for (int i = 0; i < count; i++) {
		if (strlen(p[flag]) < strlen(p[i]))
			flag=i;
	}
	return p[flag];
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值