C++ Primer Plus第六版 編程練習第八章

1.

#include<iostream>

void silly(const char*, int n = 0);

int main()
{
    using namespace std;

    char str[10] = "whssl";
    cout << "call 1" << endl;
    silly(str);
    cout << "call 2" << endl;
    silly(str);
    cout << "call 3" << endl;
    silly(str, 1);

    return 0;
}

void silly(const char* str, int n)
{
    using namespace std;
    static int times = 1;
    if (n)
        for (int i = 1; i <= times; ++i)
            cout << str << endl;
    else
        cout << str << endl;

    times++;
}

2.

#include <iostream>
#include <cstring>

struct CandyBar {
    char brand[30];
    double weight;
    int heat;
};

void set(CandyBar&, const char* brand = "Millennium Munch", double weight = 2.85, int heat = 350);
void show(const CandyBar&);

int main()
{
    using namespace std;

    CandyBar cb;
    char brand[] = "Munch";
    double weight = 4.85;
    int heat = 650;

    set(cb);
    cout << "Original Bar: " << endl;
    show(cb);
    set(cb, brand, weight, heat);
    cout << "New Bar: " << endl;
    show(cb);

    return 0;
}

void set(CandyBar& cb, const char* brand, double weight, int heat)
{
    strcpy_s(cb.brand, brand);
    cb.weight = weight;
    cb.heat = heat;
}

void show(const CandyBar& cb)
{
    using namespace std;

    cout << "Brand: " << cb.brand << endl;
    cout << "Weight: " << cb.weight << endl;
    cout << "Heat: " << cb.heat << endl;
}

3.

#include <iostream>
#include <string>
#include <cctype>

using namespace std;
void to_upper(string& str);

int main()
{
    string str;
    cout << "Enter a string (q to quit): ";
    getline(cin, str);
    while (str != "q")
    {
        to_upper(str);
        cout << str << endl;

        cout << "Next string (q to quit): ";
        getline(cin, str);
    }
    cout << "Bye." << endl;
    return 0;
}

void to_upper(string& str)
{
    for (int i = 0; i < str.size(); ++i)
    {
        if (isalpha(str[i]))
            str[i] = toupper(str[i]);
    }
}

4.

#include <iostream>
using namespace std;
#include <cstring>
struct stringy {
    char* str;
    int ct;
};

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

int main()
{
    stringy beauy;
    char testing[] = "Reality isn't what it used to be.";

    set(beauy, testing);
    show(beauy);
    show(beauy, 2);
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);
    show(testing, 3);
    show("Done!");
    return 0;
}

void set(stringy& beauy, char* str)
{
    int len = strlen(str);

    beauy.str = new char[len];
    strcpy_s(beauy.str, strlen(str) + 1, str);

    beauy.ct = len;
}

void show(const stringy& beauy, int n)
{
    cout << "共" << n  << "次循環\n";
    while (n > 0)
    {
        cout << beauy.str << endl;
        n--;
    }
}

void show(const char* str, int n)
{
    cout << "共" << n << "次循環\n";
    while (n > 0)
    {
        cout << str << endl;
        n--;
    }
}

5.

#include <iostream>

const int Len = 5;

template <typename T>
T max5(T  arr[Len])
{
    T max = arr[0];
    for (int i = 0; i < Len; ++i)
    {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}

int main()
{
    using namespace std;

    int arr_int[Len] = { 1, 2, 3, 4 ,5 };
    double arr_flt[Len] = { 1.1, 2.2, 3.3, 4.4, 5.5 };

    cout << "Max int = " << max5(arr_int) << endl;
    cout << "Max double = " << max5(arr_flt) << endl;

    return 0;
}

6.

#include <iostream>
#include <cstring>

using namespace std;

template <typename T> void show(T arr[], int);
template <>void show<char*>(char*[], int);

template <typename T> T maxn(T arr[], int);
template <>char* maxn<char*>(char*[], int);

int main()
{
    int arr_int[6] = { 1, 2, 3, 4, 5, 6 };
    double arr_flt[4] = { 10.0, 11.0, 12.0, 13.0 };
    const char* arr_ch[4] = {"wo","jiushiyao", "wan", "yingxionglianmeng" };

    show(arr_int, 6);
    cout << "Max int: " << maxn(arr_int, 6) << endl;

    show(arr_flt, 4);
    cout << "Max double: " << maxn(arr_flt, 4) << endl;

    show(arr_ch, 4);
    cout << "Max string: " << maxn(arr_ch, 4) << endl;

    return 0;
}

template <typename T>
T maxn(T arr[], int n)
{
    T max = arr[0];
    for (int i = 0; i < n; ++i)
    {
        if (arr[i] > max)
            max = arr[i];
    }

    return max;
}

template <>
char* maxn<char*>(char* arr[], int n)
{
    int max_len = strlen(arr[0]);
    int tmp_len;
    int max_idx = 0;

    int i;
    for (i = 0; i < n; ++i)
    {
        tmp_len = strlen(arr[i]);
        if (tmp_len > max_len)
        {
            max_len = tmp_len;
            max_idx = i;
        }
    }

    return arr[max_idx];
}

template <typename T>
void show(T arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

template <>
void show<char*>(char* arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << endl;
}

7.

#include <iostream>

struct debts {
    char name[50];
    double amount;
};

template <class T>
T SumArray(T arr[], int n);

template <typename T>
T SumArray(T* pd[], int n);

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];

    for (int i = 0; i < 3; ++i)
        pd[i] = &mr_E[i].amount;

    cout << "amount of things: " << SumArray(things, 6) << endl;
    cout << "amount of debt: " << SumArray(pd, 3) << endl;

    return 0;
}

template <class T>
T SumArray(T arr[], int n)
{
    T total = 0;
    for (int i = 0; i < n; ++i)
        total += arr[i];

    return total;
}

template <typename T>
T SumArray(T* pd[], int n)
{
    T sum = (T)0;

    for (int i = 0; i < n; ++i)
        sum += *pd[i];

    return sum;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章