您的位置:首页 > 编程语言 > C语言/C++

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

2019-01-04 10:08 453 查看

第八章 函数探幽
1. 编写一个通常接受一个参数(字符串的地址),并打印该字符串的函数。
    然而,如果提供了第二个参数(int)类型,且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数。
   (注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。
   是的,这是一个非常可笑的函数(我怎么不觉得可笑,手动微笑),但它能让您使用本章介绍的一些技术。
   在一个简单的程序中使用该函数,以演示该函数是如何工作的。
   Write a function that normally takes one argument, the address of a string, andprints that string once. 
   However, if a second, type int, argument is provided and is nonzero, 
   the function should print the string a number of times equal to the number of times 
   that function has been called at that point. 
   (Note that the number of times the string is printed is not equal to the value of the second argument; 
   it is equal to the number of times the function has been called.) 
   Yes, this is a silly func-tion, but it makes you use some of the techniques discussed in this chapter. 
   Use the function in a simple program that demonstrates how the function works.

1.1 使用C-风格字符串

[code]#include <iostream>

void print(char * str, int n = 0);

int main()
{
    using namespace std;
    char str[20];
    int n;
    
    cout << "请输入字符串:";
    cin.get(str, 20).get();
    
    cout << "请输入一个整数(输入非整数跳过)";
    if (cin >> n)
    {
        cout << "输入了字符串与整数:\n" ;
        print(str, n);
    }        
    else
    {   
        cout << "仅输入了字符串:\n";
        print(str);
    }
    return 0;
}
         
void print(char * str, int n)
{
    using namespace std;
    
    if (n != 0)
    {
        print(str, n-1);
    }
    cout << "第" << n+1 << "次调用print函数:" << "\t" << str << endl;
}

1.2 使用string类

[code]#include <iostream>
#include <string>
using namespace std;
void print(string str, int n = 0);

int main()
{
    using namespace std;
    string str;
    int n;
    
    cout << "请输入字符串:";
    getline(cin, str);
    
    cout << "请输入一个整数(输入非整数跳过)";
    if (cin >> n)
    {
        cout << "输入了字符串与整数:\n" ;
        print(str, n);
    }        
    else
    {   
        cout << "仅输入了字符串:\n";
        print(str);
    }
    return 0;
}
         
void print(string str, int n)
{
    using namespace std;
    
    if (n != 0)
    {
        print(str, n-1);
    }
    cout << "第" << n+1 << "次调用print函数:" << "\t" << str << endl;
}

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

[code]#include <iostream>
#include <cstring>

struct CandyBar{
    char band[30];
    double weight;
    int calorie;
};

char init_band[30] = "Millennium Munch";
void setting(CandyBar & cb, const char * band = init_band, const double weight = 2.85, const int calorie = 350);
void show(const CandyBar & cb);

int main()
{
    using namespace std;
    CandyBar cb;
    char input_band[30];
    double input_weight;
    int input_calorie;
    
    //设置为默认值并显示
    setting(cb);
    cout << "设置为默认值为:\n";
    show(cb);
    
    //按输入的值设置并显示
    cout << "请输入品牌名称:";
    cin.get(input_band, 30).get();
    cout << "请输入重量:";
    cin >> input_weight;
    cout << "请输入热量:";
    cin >> input_calorie;
    
    setting(cb, input_band, input_weight, input_calorie);
    show(cb);
    
    //再次设置为默认值并显示
    setting(cb);
    cout << "再次设置为默认值为:\n";
    show(cb);
    
    return 0;
}

void setting(CandyBar & cb, const char * band, const double weight, const int calorie)
{
    strcpy(cb.band, band);
    cb.weight = weight;
    cb.calorie = calorie;
}

void show(const CandyBar & cb)
{
    using namespace std;
    cout << "当前糖果的品牌名称为:" << cb.band << endl;
    cout << "当前糖果的重量为:" << cb.weight << endl;
    cout << "当前糖果的热量为:" << cb.calorie << endl << endl;
}


3. 编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,
为此可使用cctype中的字符函数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.

3.1 不返回

[code]#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void upper(string & input);

int main()
{
    using namespace std;
    string input;

    cout << "Enter a string (q to quit):";
    getline(cin, input);
    while(input[0] != 'q')
    {
        upper(input);
        cout << input << endl;
        cout << "Next string (q to quit):";
        getline(cin, input);
    }
    cout << "Bye.";
        
    return 0;
}

void upper(string & input)
{
    for (int i = 0; i < input.size(); i++)
        input[i] = toupper(input[i]);
}

3.2 返回引用

[code]#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string & upper(string & input);

int main()
{
    using namespace std;
    string input;

    cout << "Enter a string (q to quit):";
    getline(cin, input);
    while(input[0] != 'q')
    {
        cout << upper(input) << endl;
        cout << "Next string (q to quit):";
        getline(cin, input);
    }
    cout << "Bye.";
        
    return 0;
}

string & upper(string & input)
{
    for (int i = 0; i < input.size(); i++)
        input[i] = toupper(input[i]);
    return input;
}

4. 下面是一个程序框架

[code]#include <iostream>
using namespace std;
#include <cstring>              // for strlen(), strcpy()
struct stringy {
    char * str;                 // points to a string
    int ct;                     // length of string (not counting '\0')
}; 
// prototypes 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 argument is a reference,
                                // allocates space to hold copy of testing, 
                                // sets str member of beany to point to the new block,
                                // copies testing to new block, and sets ct member of beany
                                //第一个参数是一个引用,分配空间来保存testing副本,
                                //设置beany的str成员指向新块,将testing复制到新块,
//并设置beany的ct成员
    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编译指令,这取决于所用的编译器)

[code]#include <iostream>
using namespace std;
#include <cstring>              // for strlen(), strcpy()

struct stringy {
    char * str;                 // points to a string
    int ct;                     // length of string (not counting '\0')
}; 

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

void set(stringy & stry, const char * str);
void show(const stringy & str, int n = 1);
void show(const char * str, int n = 1);

int main()
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";
    
    set(beany, testing);      
                               
    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 & stry, const char * str)
{
    stry.str = new char [strlen(str)+1];
    strcpy(stry.str, str);
    stry.ct = strlen(str)+1;
}

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

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

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

[code]#include <iostream>

template <class T>            // 或 template <typename T>
T max5(T * ar);                      //或  T max5(T ar[]);      

int main()
{
    using namespace std;
    
    int int_arr[5] = {1,2,3,4,5};
    double double_arr[5] = {1.5, 2.5, 3.5, 4.5, 5.5};
    
    cout << "int数组中元素的最大值为:" << max5(int_arr) << endl;
    cout << "double数组中元素的最大值为:" << max5(double_arr) << endl;
    
    return 0;
}
    
template <class T>            // 或 template <typename T>
T max5(T * ar)                      //或  T max5(T ar[])  
{
    T a = ar[0];
    for (int i = 1; i < 5; i++)
    {
        if (a < ar[i])
            a = ar[i];
    }
    return a;
}

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

[code]#include <iostream>

template <class T>            // 或 template <typename T>
T maxn(T * ar, int n);                      //或  T maxn(T * ar, int n);      

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

int main()
{
    using namespace std;
    
    int int_arr[6] = {1,2,3,4,5,6};
    double double_arr[4] = {1.5, 2.5, 3.5, 4.5};
    const char * srt_arr1[5] = {"a", "ab", "abc", "abcd", "abcde"};
    const char * srt_arr2[5] = {"a", "ab", "aaaaa", "abcd", "abcde"};
    
    cout << maxn(int_arr, 6) << endl;
    cout << maxn(double_arr, 4) << endl;
    cout << maxn(srt_arr1, 5) << endl;
    cout << maxn(srt_arr2, 5) << endl;
    
    return 0;
}
    
template <class T>            // 或 template <typename T>
T maxn(T * ar, int n)                      //或  T maxn(T * ar, int n) 
{
    T a = ar[0];
    for (int i = 1; i < n; i++)
    {
        if (a < ar[i])
            a = ar[i];
    }
    return a;
}

template <> 
const char * maxn(const char * ar[], int n)
{
    const char * a = ar[0];
    for (int i = 1; i < n; i++)
    {
        if (strlen(a) < strlen(ar[i]))
            a = ar[i];
    }
    return a;
}

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

[code]// tempover.cpp --- template overloading
#include <iostream>

template <typename T>            // template A
void ShowArray(T arr[], int n);

template <typename T>            // template B
void ShowArray(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]; 

// set pointers to the amount members of the structures in mr_E
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;
    
    cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
    ShowArray(things, 6);  // uses template A
    cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
    ShowArray(pd, 3);      // uses template B (more specialized)
    // cin.get();
    return 0;
}

template <typename T>
void ShowArray(T arr[], int n)
{
    using namespace std;
    cout << "template A\n";
    for (int i = 0; i < n; i++)
        cout << arr[i] << ' ';
    cout << endl;
}

template <typename T>
void ShowArray(T * arr[], int n)
{
    using namespace std;
    cout << "template B\n";
    for (int i = 0; i < n; i++)
        cout << *arr[i] << ' ';
    cout << endl; 
}

修改后:  

[code]#include <iostream>

template <typename T>            // template A
void SumArray(T arr[], int n);

template <typename T>            // template B
void 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]; 

// set pointers to the amount members of the structures in mr_E
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;
    
    cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
    SumArray(things, 6);  // uses template A
    cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
    SumArray(pd, 3);      // uses template B (more specialized)

    return 0;
}

template <typename T>
void SumArray(T arr[], int n)
{
    using namespace std;
    T sum = 0;
    cout << "template A\n";
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
    cout << sum << endl;
}

template <typename T>
void SumArray(T * arr[], int n)
{
    using namespace std;
    T sum = 0;
    cout << "template A\n";
    for (int i = 0; i < n; i++)
        sum = sum + *arr[i];
    cout << sum << endl;
}
 


 
 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: