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;
}
相关推荐
山登绝顶我为峰 3(^v^)310 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Two_brushes.1 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
Python×CATIA工业智造2 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
十五年专注C++开发3 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
我叫小白菜3 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
森焱森3 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
天水幼麟3 小时前
动手学深度学习-学习笔记(总)
笔记·深度学习·学习
狐凄3 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
weixin_446122464 小时前
JAVA内存区域划分
java·开发语言·redis
悦悦子a啊4 小时前
Python之--基本知识
开发语言·前端·python