c++primer 第八章函数编程答案

题一

#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);
    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;
}

题二

#include <iostream>
using namespace std;
struct CandyBar
{
    char *name;
    double weight;
    int calorie;
};
void Show(CandyBar &candy, char *p = "Millennium, Munch", double a = 2.85, int b = 350);
int main()
{
    CandyBar can1;
    Show(can1);
    cout << can1.calorie << endl;
    return 0;
}
void Show(CandyBar &candy, char *p, double a, int b)
{
    candy.name = p;
    candy.weight = a;
    candy.calorie = b;
    cout << candy.name << endl;
    cout << candy.weight << endl;
    cout << candy.calorie << endl;
}

题三

#include <iostream>
#include <cctype>
using namespace std;
void upper(string &str);
int main()
{
    string str1;
    cout << "Enter a string (q to quit):";
    while (str1 != "q") // string 类型可以直接比较
    {
        upper(str1);
        cout << "Next string (q to quit): ";
        getline(cin, str1);
    }
    cout << "Bye.";
    return 0;
}
void upper(string &str)
{
    for (int i = 0; str[i]; i++)
        str[i] = toupper(str[i]);
    cout << str << endl;
}

题四

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
    char *str; // string pointer
    int ct;    // length of string (not counting '\0')
};
void set(stringy &rs, char a[]);
void show(stringy s, int a = 1);
void show(string str, int a = 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! ");
    return 0;
}
void set(stringy &rs, char a[])
{
    strcpy(rs.str, a);
    rs.ct = strlen(a);
}
void show(stringy s, int a)
{
    for (int i = 0; i < a; i++)
    {
        cout << "Str: " << s.str << endl;
        cout << "length: " << s.ct << endl;
    }
}
void show(string str, int a)
{
    for (int i = 0; i < a; i++)
    {
        cout << "Str: " << str << endl;
    }
}

题五

#include <iostream>
using namespace std;
const int SIZE = 5;
template <class Any>
Any max(Any a[]);
int main()
{
    int int_arr[SIZE];
    double double_arr[SIZE];
    cout << "Enter five int numbers: ";
    for (int i = 0; i < SIZE; i++)
    {
        cin >> int_arr[i];
    }
    cout << "Enter five double numbers: ";
    for (int i = 0; i < SIZE; i++)
    {
        cin >> double_arr[i];
    }
    cout << "The maximum of int array is " << max(int_arr) << endl;
    cout << "The maximum of int array is " << max(double_arr) << endl;
    cout << "Done!";
    return 0;
}
template <class Any>
Any max(Any a[])
{
    Any temp = a[0];
    for (int i = 1; i < SIZE; i++)
    {
        a[i] > temp ? temp = a[i] : temp = temp;
    }
    return temp;
}

题六

#include <iostream>
#include <cstring>
using namespace std;
template <class Any>
Any maxn(Any a[], int n);
template <>
const char *maxn<const char *>(const char *arr[], int n);
int main()
{
    int int_arr[6] = {1, 2, 3, 4, 5, 6};
    double double_arr[4] = {1.1, 2.2, 3.3, 1.5};
    const char *char_arr[5] = {"one", "two", "three", "four", "five"};
    cout << "The maximum of int array is " << maxn(int_arr, 6) << endl;
    cout << "The maximum of int array is " << maxn(double_arr, 4) << endl;
    cout << "The maximum of char array is " << maxn(char_arr, 5) << endl;
    cout << "The address of the longest string is: " << (void *)maxn(char_arr, 5) << endl; //
    return 0;
}
template <class Any>
Any maxn(Any a[], int n)
{
    Any temp = a[0];
    for (int i = 1; i < n; i++)
    {
        a[i] > temp ? temp = a[i] : temp = temp;
    }
    return temp;
}
template <>
const char *maxn(const char *arr[], int n)
{
    const char *temp = arr[0];
    for (int i = 1; i < n; i++)
    {
        strlen(arr[i]) > strlen(temp) ? temp = arr[i] : temp = temp;
    }
    return temp;
}

题七

#include <iostream>
template <typename T>
T SumArray(T arr[], int n);

template <typename T>
T SumArray(T *arr[], int n);
using namespace std;
struct debts
{
    char name[50];
    double amount;
};
int main(void)
{
    int things[6] = {13, 31, 103, 301, 310, 130};
    struct debts mr_R[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_R[i].amount;

    cout << "Listing Mr. R's counts of things: \n"
         << SumArray(things, 6) << endl;

    cout << "Listing Mr R's debts: \n"
         << SumArray(pd, 3) << endl;

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

template <typename T>
T SumArray(T *arr[], int n)
{
    // cout << "template B\n";
    T temp;
    for (int i = 0; i < n; i++)
        temp += *arr[i];
    return temp;
}
相关推荐
并不会33 分钟前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
龙鸣丿38 分钟前
Linux基础学习笔记
linux·笔记·学习
一点媛艺2 小时前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风2 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
奋斗的小花生3 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
pianmian13 小时前
python数据结构基础(7)
数据结构·算法
Nu11PointerException3 小时前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程3 小时前
一个例子来说明Ada语言的实时性支持
开发语言·ada