C++ primer plus 课后练习题

第八章:函数探幽

        一、复习题

        1. 调用时间远远长于运行时间的函数

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

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

            b. 不需要修改

            c. 可以: void song(char* name  = "O, My Papa", int times = 1);

        3. void iquote(int); void iquote(double); void iquote(string);

        4. a. void show(const box& b){

                        cout << b.maker << endl;

                        cout << b.height << endl;

                        cout << b.width << endl;

                        cout << b.length  << endl;

                        cout << b.volume << endl;

                }

             b. void set_volume(box& b){

                        b.volume = b.height * b.length * b.width;

                }

        5. void fill(array<double, Seasons>& a);

            void show(const array<double, Seasons>& a); 

        6. a. 默认参数

                 double mass(double,double = 1.0);

            b. 默认参数

                void repeat(int = 5, const char *); (错误! 必须从左往右添加默认值!)

                正确答案:函数重载

                void repeat(int, const char *);

                void repeat(const char *);

            c. 函数重载

                int average(int, int);

                double average(double, double);

            d. 两种方法都无法完成

        7. template<typename T>

            T bigger(T& a, T& b){

                return a > b  ? a , b;

            }

        8. template <> T bigger<box> (T& a, T& b){

                return a.volume  > b.volume ? a : b;

            }

        9. v1: float; v2: float &; v3: float &; v4: int; v5: float

        二、编程练习

        1.

#include <iostream>
using namespace std;
static int num = 0;
void repeat(const char* str, int n = 1) {
	num++;
	if (n != 0) {
		for (int i = 0; i < num; i++) {
			cout << str << endl;
		}
	}
	else {
		cout << str << endl;
	}
}
int main() {
	const char* str = "Hello";
	repeat(str);
	repeat(str,5);
	return 0;
}

        2.

#include <iostream>
using namespace std;
struct CandyBar {
	const char* brand;
	double weight;
	int calorie;
};
void setCandyBar(CandyBar& cb, const char* n = "Millennium Munch", const double w = 2.85, const int c = 350) {
	cb.brand = n;
	cb.weight = w;
	cb.calorie = c;
}
void show(CandyBar& cb) {
	cout << cb.brand << endl;
	cout << cb.weight << endl;
	cout << cb.calorie << endl;
}
int main() {
	CandyBar cb1;
	setCandyBar(cb1);
	CandyBar cb2;
	setCandyBar(cb2, "David", 1.5, 300);
	show(cb1);
	show(cb2);
	return 0;
}

        3.

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

string& toUpper(string& str) {
	if (str == "q") {
		cout << "Bye" << endl;
		return str;
	}
	int m = 0;
	while (str[m]) {
		m++;
	}
	for (int i = 0; i < m; i++) {
		if (str[i] >= 'a' && str[i] <= 'z') {
			str[i] -= 32;
		}
	}
	cout << str << endl;
	return str;
}

int main() {
	string str = "";
	while (str != "q") {
		cout << "Enter a string (q to quit): ";
		getline(cin,str);
		str = toUpper(str);
	}

	return 0;
}

        4.

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
	char* str;
	int ct;
};

void set(stringy& newstr, const char* oldstr) {
	newstr.ct = strlen(oldstr) + 1;
	newstr.str = new char[newstr.ct];
	strcpy(newstr.str, oldstr);
}
void show(const stringy& str, int n = 1) {
	for (int i = 0; i < n; i++) {
		cout << str.str << endl;
	}
}
void show(const char* str, int n = 1) {
	for (int i = 0; i < n; i++) {
		cout << str << endl;
	}
}
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;
}

        5.

#include <iostream>
using namespace std;

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

int main() {
	int arr1[5] = {1,3,9,2,-5};
	double arr2[5] = {1.2,3.6,2.1,-9.2,8.2};
	cout << max5(arr1) << " " << max5(arr2) << endl;
	return 0;
}

template <typename T>
T max5(const T arr[]) {
	T max = arr[0];
	for (int i = 1; i < 5; i++) {
		max = max < arr[i] ? arr[i] : max;
	}
	return max;
}

        6.

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

template <typename T>
T maxn(T arr[],int len);
template <> char* maxn(char* arr[], int len);

int main() {
	int arr1[6] = { 1,3,9,2,-5,10};
	double arr2[4] = { 1.2,3.6,2.1,-9.2};
	char* parr[5] = { "H","HH","HHH","HHHHH","HHHH" };
	cout << maxn(arr1,6) << " " << maxn(arr2,4) << endl;
	cout << maxn(parr, 5) << endl;
	return 0;
}

template <typename T>
T maxn(T arr[],int len) {
	T max = arr[0];
	for (int i = 1; i < len; i++) {
		max = max < arr[i] ? arr[i] : max;
	}
	return max;
}

template <> char* maxn(char* arr[], int len) {
	char* max = arr[0];
	for (int i = 0; i < len; i++) {
		max = strlen(max) < strlen(arr[i]) ? arr[i] : max;
	}
	return max;
}

        7.

#include <iostream>

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()
{
    using namespace std;
    int things[6] = { 13, 31, 103, 301, 310, 130 };
    struct debts mr_E[3] =
    {
        {"Ims 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 sumThings = SumArray(things, 6);
    cout << "sum of things:" << sumThings << endl;
    int sumDebts = SumArray(pd, 3);
    cout << "sum of debts:" << sumDebts << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值