跳转至

编程题

第 8 章 函数探幽 编程练习题

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

    #include <iostream>
    using namespace std;
    void show_str(char const *str); 
    void show_str(char const *str,int n);
    
    int main(void)
    {
        show_str("this is first function");
        show_str("this is second function");
        show_str("this is third function",44); // 44 is not 44 times prints
        return 0;
    
    }
    /*使用函数复用*/
    void show_str(char const *str)
    {
        cout <<str<<endl;
    }
    
    void show_str(char const *str,int n)
    {
        for(int i = 0;i < n;i++)
            show_str(str);
    }
    /**使用char const str的原因:由于编译器会报错误如下
     * chapter08_p1.cpp:9:35: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-str
     * 所以该用以上code的方式编译调试
     */
    

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

    
    

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

    1
    2
    3
    4
    5
    6
    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.
    
    // 完整的code
    

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

    #include <iostream>
    #include <cstring> // for strlen(),strcpy()
    using namespace std;
    struct stringy{
        char * str; // point to a string
        int ct; // length of string (not counting '\0')
    };
    // prototype for set(),show(),and show() go here
    int main()
    {
        stringy beany;
        char testing[] = "Reality isn't what it used to be.";
        set(beany,testing); // first arguement is a reference,
                            // allocates space to hold copy of testing
                            // sets str member of beany to point to th
                            // 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 3 times
        show("Done!");
        return 0;
    }
    
    请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请尽可能使用const参数。set() 使用new分配足够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名称,删除using编译质量,这取决于所用的编译器)。
    /*修改版code*/
    

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

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

  7. 修改程序清单8.14,使模板函数返回数组元素的总和,而不是显示数组的内容.程序thing的总和以及所有debt的总和.