C++ Primier Plus(第六版) 第八章 函数探幽编程练习答案


目录
  • 1. 编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然后,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串打印的次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术,在一个简单的程序中使用该函数,以演示该函数是如何工作的。
  • 2. CandyBar的结构有3个成员。第一个成员存储candy bar的品牌名称;第二个存储candy bar的重量(可能有小数);第三个存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为"Millenium Munch"、2.85和350。另外,该程序还包含一个以CandyBar引用为参数,并显示结构内容的函数。请尽可能使用const。
  • 3. 编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用表6.4描述的函数toupper()。然后编写一个程序,它通过一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:
  • 4. 下面是一个程序框架:
  • 5. 编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
  • 6. 编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序中对他进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。
  • 7. 修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的综合,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。

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

本题首先需要设置一个全局变量,统计showstr()函数调用了几次,然后使用默认参数的函数,默认参数的显示函数用if来设置,代码如下:

// ex1.cpp -- using default argument
#include
int count = 0; // count the times of call showstr() function
void showstr(const char * s, int n = 0);

int main()
{
    const char * ps = {"I like C++!"};
    for(int j = 0; j < 5; j++)
    {
        showstr(ps);
        showstr(ps,1);
    }
    return 0;
}

void showstr(const char * s, int n)
{
    using std::cout;
    ++count;
    if(n == 0)
        cout << s << "\n";
    else
    {
        for(int i = 0; i < count; i++)
            cout << s << " ";
        cout << "\n";
    }
}

运行结果如下:

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

本题考查的还是默认参数函数的声明定义和调用,注意默认参数从右到左的顺序。
代码如下:

// ex2.cpp -- using default argument and reference of structure
#include
#include
const int Size = 20;
struct CandyBar
{
    char name[Size];
    double weight;
    int carloris;
};

void fill(CandyBar &, char * n = "Millennium Munch", double w = 2.85, int c = 350);
void show(const CandyBar &);

int main()
{
    using namespace std;
    char *cname = new char[Size];
    double cweight;
    int ccaloris;
    CandyBar cbs;
    fill(cbs);
    show(cbs);

    cout << "Enter the name of CandyBar: ";
    cin.getline(cname,Size);
    cout << "After input name:\n";
    fill(cbs, cname);
    show(cbs);

    cout << "Enter the weight of CandyBar: ";
    cin >> cweight;
    cin.get();
     cout << "After input weight:\n";
    fill(cbs, cname, cweight);
    show(cbs);

    cout << "Enter the weight of CandyBar: ";
    cin >> ccaloris;
    cin.get();
    cout << "After input caloris:\n";
    fill(cbs, cname, cweight, ccaloris);
    show(cbs);
    return 0;
}


void fill(CandyBar & cb, char * n, double w, int c)
{
    strcpy(cb.name,n);
    cb.weight = w;
    cb.carloris = c;
}

void show(const CandyBar &cb)
{
    using std::cout;
    cout << "Name: " << cb.name << "\tWeight: " << cb.weight;
    cout << "\tCarloris: " << cb.carloris << "\n";
}

运行结果如下:

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.
本题使用toupper()会很简单,使用toupper()函数需要添加头文件cstdlib。不适用也可以,通过if语句和ASCKII码转换来进行,笔者使用了toupper函数。
代码如下:

// ex3.cpp -- convert the a... to A...
#include
#include
#include
using namespace std;
void str_toupper(string & s);

int main()
{
    string s;
    cout << "Enter a string(q to quit): ";
    getline(cin,s);
    while(s != "q")
    {
        str_toupper(s);
        cout << s << endl;
        cout << "Next string(q to quit): ";
        getline(cin,s);
    }
    cout << "Bye.\n";
    return 0;
}

void str_toupper(string & s)
{
    int i = 0;
    while(s[i] != '\0')
    {
        s[i] = toupper(s[i]);
        i++;
    }
}

运行结果如下:

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

#include
using namespace std;
#include
struct stringy
{
    char * str;
    int ct;
};

// prototypes for set(), show() and show() go here

int main()
{
    string 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 memebr of beany to point to the
                // new block, copies testing to new block,
                // and sets ct membet of beany
    show(beany);        // prints member string once
    show(beany, 2);     // prints member string twice
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);      // prints testing string once
    show(testing, 3);   // prints testing string thrice
    show("Done!");
    return 0;
}

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

// ex4.cpp -- write the rest procedures
#include
using namespace std;
#include
struct stringy
{
    char * str;
    int ct;
};

// prototypes for set(), show() and show() go here
void set(stringy &,char []);
void show(const stringy &, int n = 1);
void show(const char [], int n = 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 memebr of beany to point to the
                // new block, copies testing to new block,
                // and sets ct member of beany
    show(beany);        // prints member string once
    show(beany, 2);     // prints member string twice
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);      // prints testing string once
    show(testing, 3);   // prints testing string thrice
    show("Done!");
    return 0;
}

void set(stringy & s,char cs[])
{
    s.ct = strlen(cs);
    s.str = new char[s.ct + 1];
    strcpy(s.str,cs);
}

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

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

运行结果如下:

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

本题考查的是模板函数的知识点,在最开始定义一个全局常变量记录数组的大小为5,然后创建模板函数,笔者使用了?:运算符。
代码如下:

// ex5.cpp -- using template funtion to find the max
#include
const int Size = 5;

template
T max5(T[]);

template
void show(T[]);

int main()
{
    using namespace std;
    int iar[Size];
    double dar[Size];
    // integer
    cout << "Enter five integer,and I will find the max:\n";
    for(int i = 0; i < Size; i++)
    {
        cout << "Integer #" << i + 1 << ": ";
        cin >> iar[i];
    }
    cout << "The integer array:\n";
    show(iar);
    int imax = max5(iar);
    cout << "The max value is " << imax << endl;
    // double
    cout << "Enter five double values,and I will find the max:\n";
    for(int i = 0; i < Size; i++)
    {
        cout << "Value #" << i + 1 << ": ";
        cin >> dar[i];
    }
    cout << "The double array:\n";
    show(dar);
    double dmax = max5(dar);
    cout << "The max value is " << dmax << endl;
    return 0;
    
}

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

template
void show(T arr[])
{
    using std::cout;
    for(int i = 0; i < Size; i++)
        cout << arr[i] << " ";
    cout << "\n";
}

运行结果如下:

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

本题考查的是模板函数和模板函数的具体化,本题的模板函数和具体化都没有问题,在写测试的过程中主函数出现了问题,分配内存空间,创造指针数组存贮字符串的时候没有给字符串分配空间,分配空间后忘记了释放
代码如下:

// ex6.cpp -- using template function and explicit template function to find max
#include
#include

template
T maxn(T[], int n);

template<>
char * maxn(char *ps[], int n);

template
void show(T[], int size);

int main()
{
    using namespace std;
    int size;
// test integer
    cout << "Enter the number of integer: ";
    cin >> size;
    int iar[size];
    cout << "Enter "<< size <<" integer value ,and I will find the max:\n";
    for(int i = 0; i < size; i++)
    {
        cout << "Value #" << i + 1 << ": ";
        cin >> iar[i];
    }
    cout << "The integer array:\n";
    show(iar,size);
    int imax = maxn(iar,size);
    cout << "The max value is " << imax << endl;

// test double
    cout << "Enter the number of double: ";
    cin >> size;
    double dar[size];
    cout << "Enter "<< size <<" double value ,and I will find the max:\n";
    for(int i = 0; i < size; i++)
    {
        cout << "Value #" << i + 1 << ": ";
        cin >> dar[i];
    }
    cout << "The double array:\n";
    show(dar,size);
    double dmax = maxn(dar,size);
    cout << "The max value is " << dmax << endl;
// test char *
    char temp[100];
    cout << "Enter the number of strings: ";
    cin >> size;
    char * ps[size];
    cout << "Enter "<< size <<" string value ,and I will find the address of max:\n";
    for(int i = 0; i < size; i++)
    {
        cout << "Strings #" << i + 1 << ": ";
        while(cin.get() == '\n')
            continue;
        cin.getline(temp,100);
        ps[i] = new char[strlen(temp)+1]; // is must else will cause the mistake,need to distribute RAM
        strcpy(ps[i],temp);
    }
    cout << "The String array:\n";
    show(ps,size);
    char * psmax = maxn(ps,size);
    cout << "The address of max length string is " << &psmax << endl;
    for(int i = 0; i < size; i++)
        delete [] ps[i];
    return 0;
}

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

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

template
void show(T arr[],int n)
{
    using std::cout;
    for(int i = 0; i < n; i++)
        cout << arr[i] << ",";
    cout << "\n";
}

测试结果如下:

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

本题修改函数模板,再修改时,会遇到很多错误,修改时按照先原型,在定义,最后调用的方式修改。
代码如下:

// ex7.cpp -- using template function to calculate sum
#include

template
T SumArray(T[], int);

template
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", 1900.0}
    };
    
    double *pd[3];
    for(int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;
    
    cout << "The sum of Mr.E's things: ";

    cout << SumArray(things,6) << endl;
    cout << "The sum of Mr.E's debts: ";
    cout << SumArray(pd,3) << endl;
    return 0;

}

template
T SumArray(T arr[], int n)
{
    using std::cout;
    T sum = 0;
    cout << "template A\n";
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

template
T SumArray(T* arr[], int n)
{
    using std::cout;
    T sum = 0;
    cout << "template B\n";
    for(int i = 0; i < n; i++)
        sum += *arr[i];
    return sum;
}

运行结果如下: