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;
}
相关推荐
蒸蒸yyyyzwd26 分钟前
cpp 选手秋招学习笔记 day33
c++·笔记·求职招聘
郝学胜-神的一滴34 分钟前
Effective Python 条款 10 :海象运算符_=
开发语言·python·程序人生·开源
wuyk55536 分钟前
Python零基础入门第十四章:异常处理(try-except)
开发语言·python
wuyk55541 分钟前
【Socket 进阶之路】第 3 章 TCP 三次握手 & 四次挥手深度剖析|连接建立、断开、状态机、TIME_WAIT 核心工程问题
服务器·开发语言·网络·物联网·网络协议·tcp/ip
wzdark5 小时前
基于堆的优先队列实现原理与复杂度分析4
算法
彧azz6 小时前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php
平头哥AI7 小时前
Day 22 _ 包装错误别丢链_%w、errors.Is 与 errors.As
android·服务器·学习·golang·go
老王熬夜敲代码7 小时前
BLAKE3:最快的密码学哈希函数
算法·密码学·哈希算法
hansang_IR7 小时前
【题解】P4460 [CQOI2018] 解锁屏幕
c++·算法
嵌入式学习菌7 小时前
Modbus‑RTU 数据类型分析
开发语言·单片机·bug