C++ primer plus 第八章答案

万恶的暑假(T_T)

1.

#include <iostream>
#include <string>
using namespace std;
void Print(string *name, int time= 0);
int time = 0;
int main()
{
    string name = "Hu Zeyu";
    Print(&name,time);
    Print(&name,time);
    return 0;
}
void Print(string *name, int time)
{
    for(int i = 0; i <= time; i++)
    {
        cout << *name << endl;
    }
    time++;
}

2.

#include <iostream>
using namespace std;
struct Candy
{
    char* brandname;
    double weight;
    int calories;
};
void Set(Candy &candybar, char* brandname = "Millenium Munch", double weight = 2.85, int calories = 350);
void Show(const Candy &candybar);
int main()
{
    Candy test;
    Set(test);
    Show(test);
    return 0;
}
void Set(Candy & candybar, char * brandname, double weight, int calories)
{
    candybar.brandname = brandname;
    candybar.weight = weight;
    candybar.calories = calories;
}
void Show(const Candy & candybar)
{
    cout << candybar.brandname << endl;
    cout << candybar.weight << endl;
    cout << candybar.calories << endl;
}

3.

#include <iostream>
#include <string>
using namespace std;
void mytoupper(string & words);
int main()
{
    string test;
    cout << "Enter a string (q to quit): ";
    while(getline(cin, test) && test != "q")
    {
        mytoupper(test);
        cout << test << endl;
        cout << "Next string(q to quit): ";
    }
    cout << "Bye.";
    return 0;
}
void mytoupper(string & words)
{
    int length = words.size();
    for(int i = 0; i < length; i++)
    {
            words[i] = toupper(words[i]);
    }
}
4.
#include <iostream>
using namespace std;
#include <cstring> // for strlen(),strcpy()
struct stringy{
    char * str;
    int ct;
};
void set(stringy &beany, char []);
void show(const char [],int = 1);
void show(const stringy &, int = 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 &beany,char testing[])
{
    beany.ct = strlen(testing);
    beany.str = new char[beany.ct + 1];
    strcpy(beany.str, testing);
}
void  show(const char testing[],int length)
{
    for(int i = 0; i < length; i++)
    {
        cout << testing << endl;
    }
}
void show(const stringy &beany,int length)
{
    for(int i = 0; i < length; i++)
    {
        cout << beany.str << endl;
    }
}
5.
#include <iostream>
using namespace std;
template <typename T>
T max5(T a[]);
int main()
{
    int array1[] = {1,2,3,4,5};
    double array2[] = {2.2,3.4,4.5,5.6,3.2};
    cout << max5(array1) << endl;
    cout << max5(array2) << endl;
    return 0;
}
template <typename T>
T max5(T a[])
{
    T max = a[0];
    for(int i = 0; i < 5; i++)
    {
        max = max >= a[i] ? max : a[i];
    }
    return max;
}

6.
#include <iostream>
#include <cstring>
using namespace std;

template <typename T>
T maxn(T num[], int length);

template <> char *maxn<char *>(char *num[], int length);
int main()
{
    int a[6] = {1, 2, 3, 4, 5, 6};
    double b[4] = {1.1, 2.2, 3.3, 4.4};
    char *c[5] ={"apple","banana","egg","seal","elephant"};
    std::cout << maxn(a, 6) << std::endl;
    std::cout << maxn(b, 4) << std::endl;
    std::cout << maxn(c, 5) << std::endl;
    return 0;
}
template <typename T>
T maxn(T num[], int length)
{
    T max = num[0];
    for(int i = 0; i < length; i++)
    {
        max = max >= num[i] ? max : num[i];
    }
    return max;
}
template <> char *maxn<char *>(char *num[], int length)
{
    int max = strlen(num[0]);
    for(int i = 0; i < length; i++)
    {
        max = max >= (int)strlen(num[i]) ? max : strlen(num[i]);
    }
    for(int i = 0; i < length; i++)
    {
        if((int)strlen(num[i]) == max)
            return num[i];
    }
}
7.
#include <iostream>
using namespace std;

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

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

struct debts
{
    char name[50];
    double amount;
};
int main()
{
    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 << SumArray(things, 6) << endl;
    cout << "Listing Mr.E's debts:\n";
    cout << SumArray(pd, 3) << endl;
    return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{
    int sum = 0;
    cout << "template A\n";
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}
template <typename T>
T SumArray(T * arr[], int n)
{
    cout << "template B\n";
    int sum = 0;
    for(int i = 0;i < n; i++)
        sum += *arr[i];
    return sum;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值