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;
}
相关推荐
“抚琴”的人几秒前
C#上位机观察者模式
开发语言·观察者模式·c#·上位机
思成Codes1 分钟前
Go语言的多返回值是如何实现的?
开发语言·后端·golang
北极糊的狐4 分钟前
MQTT报错:Exception in thread main java.lang.at io.github.pnoker.common.sdk.utils.ParseUtils.decodeHex
java·开发语言
BahTiYar_5 分钟前
ctfshow Web应用安全与防护系列
笔记·web安全
Grassto7 分钟前
Go 是如何解析 `import path` 的?第三方包定位原理
开发语言·golang·go module·go import
七月七337 分钟前
半小时搞定GitHub学生认证
笔记·github
福大大架构师每日一题7 分钟前
go-zero v1.9.4 版本发布详解:云原生适配升级与稳定性性能全面提升
开发语言·云原生·golang
蒙奇D索大9 分钟前
【数据结构】排序算法精讲 | 交换排序全解:交换思想、效率对比与实战代码剖析
数据结构·笔记·考研·算法·排序算法·改行学it
sin_hielo11 分钟前
leetcode 1351
数据结构·算法·leetcode
byte轻骑兵11 分钟前
【安全函数】memmove_s ():C 语言内存安全迁移的守护者与 memmove 深度对比
c语言·开发语言·安全