C++ Primer Plus(第六版)编程练习 第8章 函数探幽

本章所有编程练习的工程源码可在此处下载(点击打开下载链接),供大家参考交流!

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

本题要求编写一个打印字符串的函数,但是接受参数的方式比较奇特,如果只提供一个字符串地址则打印该字符串,如果提供了第二个int类型参数则多次,打印次数等于被调用次数。所以该函数首先要包含一个全局静态变量,这样才可以知道每次调用具体是第几次调用,使用static int来解决。其他的应该就都很简单了。

代码如下:

// 8.1.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#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);
    print(str);
    system("pause");
    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;
}

由以上代码可知,第一次和第四次是基础调用,只有字符串地址,第二次和第三次是高级调用,含有第二个int类型变量。

运行结果如下图所示:

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

本题要求首先创建一个结构变量,分别存储char类型、double类型和int类型的三个变量,然后编写两个函数,第一个函数接受4个参数,第一个参数是结构引用,后面3个参数存在默认值,写结构引用的参数传递,就直接Struct &x就可以了,其中Struct表示结构名称;第二个函数显示结构的各个内容。

具体代码如下:

// 8.2.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int heat;
};
void set(CandyBar &x, const char * brand = "Millennium Munch", const double weight = 2.85, const int heat = 350);
void showbar(CandyBar &x);

int main()
{
    using namespace std;
    CandyBar x0, x1, x2;
    set(x0);
    showbar(x0);
    set(x1, "Critiano Ronaldo", 1.88, 240);
    showbar(x1);
    set(x2, "Sergio Ramos", 3.56, 360);
    showbar(x2);
    system("pause");
    return 0;
}

void set(CandyBar &x, const char * brand, const double weight, const int heat)
{
    x.brand = brand;
    x.weight = weight;
    x.heat = heat;
}

void showbar(CandyBar &x)
{
    using namespace std;
    cout << "Brand:\t" << x.brand << endl;
    cout << "Weight:\t" << x.weight << endl;
    cout << "Heat:\t" << x.heat << endl;
}

程序中的main()函数包含默认设置结构,传参设置结构,每次设置不同的结构变量都会使用显示函数将结构内容显示出来。

运行结果如下图所示:

3. 编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:

Enter a string (q to quit): go away

GO AWAY

Next string (q to quit): good grief!

GOOD GRIEF!

Next string (q to quit): q

Bye.

本题要求编写一个函数将接受的string对象参数的内容转换为大写,要用到toupper()函数,该函数是包含在头文件<cctype>中的。然后在主函数中使用循环来使用户多次输入要转换为大写的数据。

代码如下:

/ 8.3.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
string change(string &str);

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

string change(string &str)
{
    for (int i = 0; str[i]; i++)
    {
        str[i] = toupper(str[i]);
    }
    return str;
}

其中对string对象的内容进行大写转换,使用了一个for循环来进行,判断停止可以string对象的长度,也可以直接判断string对象的内容是否为空来做,我在这里就是直接用str[i]来作为判断条件,当str[i]为'\n'或为空时,则表示循环结束。

运行结果如下图所示:

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

…………(省略,敲起来实在繁琐,大家看书就好)

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

首先,本题明确要求要有两个show()函数,每个接受不同的默认参数,根据题目给出的程序框架中的要求,一个show()函数是接受两个参数,一个结构对象和一个int整数,其中int整数表示显示结构对象的次数;而另一个show()函数则必须接受char类型数组,然后显示,我们可以也配一个显示次数的int整数,因为反正肯定是默认显示1次。

另外还有一个set()函数,该函数使用new方法来分配内存空间给字符串,根据程序框架中的使用方式,该set()函数接受一个结构对象和一个char类型数组作为参数,然后使用new来给char类型数组分配长度,在这里为了获得char类型数组的长度以及将char类型数组的内容复制给其他变量,必须使用strcpy()函数和strlen()函数,这两个函数都是包含在头文件<cstring>中的。

具体代码如下:

// 8.4.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

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

void set(stringy &st, const char * x);
void show(stringy &st, const int n = 1);
void show(const char * x, const 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!");
    system("pause");
    return 0;
}

void set(stringy &st, const char * x)
{
    st.ct = strlen(x) + 1;
    st.str = new char[st.ct];
    strcpy_s(st.str, st.ct, x);
}

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

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

运行结果如下图所示:

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

本题要求编写一个函数来返回输入参数中最大的元素,其中元素类型为T,这就需要创建一个函数模板,在模板函数内部,比较得到最大值的方式有很多种,我在这里使用了"A>B?A:B"的方式,同时由于固定了数组长度一定是5,所以可以直接用比较四次的方式来写;之后在主函数中使用不同的5个数值来测试就很简单了。

具体代码如下:

// 8.5.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
T max5(T arr[5]);

int main()
{
    int arr1[5] = { 0, 1, 2, 3, 4 };
    double arr2[5] = { 1.1, 2.3, 5.6, 7.123, 2.0 };
    int max1;
    double max2;
    max1 = max5(arr1);
    max2 = max5(arr2);
    cout << "The first array is " << arr1[0] << " " << arr1[1] << " " << arr1[2] << " " << arr1[3] << " " << arr1[4]  << endl;
    cout << "The biggest number is " << max1 << endl;
    cout << "The second array is " << arr2[0] << " " << arr2[1] << " " << arr2[2] << " " << arr2[3] << " " << arr2[4]  << endl;
    cout << "The biggest number is " << max2 << endl;
    cout << "Bye." << endl;
    system("pause");
    return 0;
}

template <typename T>
T max5(T arr[5])
{
    T max;
    max = arr[0] > arr[1] ? arr[0] : arr[1];
    max = max > arr[2] ? max : arr[2];
    max = max > arr[3] ? max : arr[3];
    max = max > arr[4] ? max : arr[4];
    return max;
}

运行结果如下图所示:

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

本题首先要求编写一个模板函数来比较返回最大值元素,该模板函数与上一题的功能完全一样,但是由于数组长度可以不同,所以使用循环来完成比较和返回最大值,于是就需要输入参数包含数组长度;另外本题要求编写一个模板函数具体化,该具体化要能够比较并返回最长的字符串地址,取地址可以直接用char *来获得,关键是要得到最长字符串的标号,我们同样是使用循环来比较获得。

具体代码如下:

// 8.6.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#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] = { 1.5, 8.66, 7.773, 0.4 };
    int max1;
    double max2;
    max1 = maxn(arr1, 6);
    max2 = maxn(arr2, 4);
    char * arr3[5] = { "one", "two", "three", "four", "five" };
    char * max3;
    max3 = maxn(arr3, 5);
    cout << "The biggest number of the first array is " << max1 << endl;
    cout << "The biggest number of the second array is " << max2 << endl;
    cout << "The longest address of the third array is " << &max3 << endl;
    cout << "Bye." << endl;
    system("pause");
    return 0;
}

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

template <> char * maxn(char * arr[], int n)
{
    int j = 0;
    int max = 0;
    for (int i = 0; i < n; i++)
    {
        if (max < strlen(arr[i]))
        {
            max = strlen(arr[i]);
            j = i;
        }
    }    
    char * address = arr[j];
    return address;
}

运行结果如下图所示:

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

本题要求编写两个SumArray()的模板函数,返回数组元素总和。程序清单8.14中,其实已经提供了两个SumArray()模板函数,只是它们是用来显示内容的,本题要求计算数组元素总和,所以主要是修改两个模板函数的功能。在这里主要需要注意的是,在每个模板函数内部,我们都需要首先定义一个T类型变量sum,用来存储元素的总和并返回,因为输入参数就包含数组长度,所以使用一个循环去累加就可以得到总和了。

具体代码如下:

// 8.7.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#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] = 
    {
        {"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);
    cout << "The sum of things is " << sum1 << endl;
    double sum2 = SumArray(pd, 3);
    cout << "The sum of the debts is " << sum2 << endl;
    system("pause");
    return 0;
}

template <typename T>
T SumArray(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 SumArray(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;
}

运行结果如下图所示:

本章所有编程练习的工程源码可在此处下载(点击打开下载链接),供大家参考交流!

猜你喜欢

转载自blog.csdn.net/leowinbow/article/details/81361995
今日推荐