C++ Primer Plus 第8章 课后编程练习 代码

第一题
#include <iostream>
using namespace std;
static int count = 0;
void print_str(char *str, int n, int &ref = count);
int main()
{
    char *str = (char *)"Hello World!";
    cout << "第1次调用:\n";
    print_str(str, 0);
    for (int i = 0; i < 5; i++)
    {
        cout << "第" << i + 2 << "次调用:\n";
        print_str(str, 1);
    }
    return 0;
}

void print_str(char *str, int n, int &ref)
{
    ++ref;
    if (n)
    {
        for (int i = 0; i < ref; i++)
        {
            cout << str << endl;
        }
    }
    else
    {
        cout << str << endl;
    }
}

第二题
#include <iostream>
struct CandyBar
{
    char name[50];
    double weight;
    int calories;
};
void SetCandyBar(CandyBar &candref, char *str = (char *)"Millennium Munch", double x = 2.85, int y = 350);
void ShowCandyBar(const CandyBar &candref);

int main()
{
    CandyBar candy;
    SetCandyBar(candy);
    ShowCandyBar(candy);
    char *str = (char *)"HappySuger";
    SetCandyBar(candy, str, 3.65, 500);
    ShowCandyBar(candy);

    return 0;
}

void ShowCandyBar(const CandyBar &candref)
{
    std::cout << "Name: " << candref.name << std::endl
              << "Weight: " << candref.weight << std::endl
              << "Calories: " << candref.calories << std::endl;
}
void SetCandyBar(CandyBar &candref, char *str, double x, int y)
{
    int i = 0;
    while (str[i] != '\0')
    {
        candref.name[i] = str[i];
        ++i;
    }
    candref.name[i] = str[i];

    candref.weight = x;
    candref.calories = y;
}

第三题
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

void Str_To_Upper(string &str);

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

    while (str[0] != 'q')
    {
        Str_To_Upper(str);
        cout << str << endl;
        cout << "Next string (q to quit):";
        getline(cin, str);
    }
    cout << "Bye!!!" << endl;

    return 0;
}

void Str_To_Upper(string &str)
{
    int i = 0;
    while (str[i] != '\0')
    {
        str[i] = toupper(str[i]);
        ++i;
    }
}
第四题
#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
    char *str;
    int ct;
};

void set(stringy &strref, char *str);
void show(const stringy &strref, int n = 1);
void show(const char *str, int n = 1);
int main()
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";
    // 分配空间保存testing、设置stringy结构的成员str指向new的空间
    // 并将testing拷贝到分配的空间,并且设置stringy的成员ct的值
    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 &strref, char *str)
{
    strref.str = new char[strlen(str) + 1];
    strcpy(strref.str, str);
    strref.ct = strlen(str);
}

void show(const stringy &strref, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << strref.str << "----->" << strref.ct << endl;
    }
}

void show(const char *str, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << str << endl;
    }
}
第五题
#include <iostream>

template <typename T>
T max5(T *arrT);

int main()
{
    using namespace std;
    int arri[5] = {1, 45, 676, 34, 444};
    double arrd[5] = {23.4, 44.5, 66.4, 34.5, 12.4};

    cout << "Max of double array: " << max5(arrd) << endl;
    cout << "Max of int array: " << max5(arri) << endl;

    return 0;
}

template <typename T>
T max5(T *arrT)
{
    T max = arrT[0];
    for (int i = 1; i < 5; i++)
    {
        if (max < arrT[i])
        {
            max = arrT[i];
        }
    }
    return max;
}
6#include <cstring>
#include <iostream>
using namespace std;
template <typename T>
T maxn(T *arrT, int n);

template <>
char *maxn<char *>(char **arrp, int n);

int main()
{

    int arri[5] = {1, 45, 676, 34, 444};
    double arrd[5] = {23.4, 44.5, 66.4, 34.5, 12.4};
    char *arr[5] = {
        "Hello World",
        "Hi,myfriend",
        "Dong zhao cheng",
        "Chen li ang",
        "Huang Yu xiong"}; //数组,元素是5个char指针(char *),分别指向5个字符串

    cout << "Max of double array: " << maxn(arrd, 5) << endl;
    cout << "Max of int array: " << maxn(arri, 5) << endl;
    cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";

    for (int i = 0; i < 5; i++)
    {   //arr[i]是char*代表是字符串,实质是指向字符串首元素的地址,打印的话结果是字符串
        //假如想打印这个地址,则需要(void*)arr[i]
        //那 &arr[i]是什么?----arr[i]是字符串的地址,加了取地址符号后,就是存储这个地址的内存地址
        cout << "Address of " << arr[i] << " = " << (void *)arr[i] << endl;
    }

    //测试显式具体化
    cout << hex << "The Adress of max length string in the array is " << (void *)(maxn(arr, 5)) << endl;
    return 0;
}
// 模板函数
template <typename T>
T maxn(T *arrT, int n)
{
    T max = arrT[0];
    for (int i = 1; i < n; i++)
    {
        if (max < arrT[i]) //长度相等并不会改变地址,所以即使出现同一长度的字符串,也只会返回首次出现的那个
        {
            max = arrT[i];
        }
    }
    return max;
}

template <>
char *maxn<char *>(char *arrp[], int n)
{
    char *MaxAddress = arrp[0];
    for (int i = 1; i < n; i++)
    {
        if (strlen(MaxAddress) < strlen(arrp[i]))
        {
            MaxAddress = arrp[i];
        }
    }
    return MaxAddress;
}
7#include <iostream>
struct debts
{
    char name[50];
    double amount;
};
template <typename T>
int SumArray(T arr[], int n);

template <typename T>
double SumArray(T *arr[], 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 << "Sum of thing is " << SumArray(things, 6) << endl;
    cout << "Sum of debts is " << SumArray(pd, 3) << endl;

    return 0;
}

template <typename T>
int SumArray(T arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    return sum;
}

template <typename T>
double SumArray(T *arr[], int n)
{
    double sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum = sum + *(arr[i]);
    }
    return sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖啡与乌龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值