c++.primer.plus第五版第八章编程练习答案

// 1
#include <iostream>
using namespace std;
void print(const char *str,int n = 1);
int main()
{
    const char *str = "My English is very good + I can speak English Well!\n";
    cout << "=====================================================================\n";
    print(str);
    cout << "=====================================================================\n";
    print(str,10);
    cout << "=====================================================================\n";
    return 0;
}
void print(const char * str,int n)
{
    if(n <= 0)
        n = 1;
    for(int i = 0;i < n;i++)
        cout << str ;
}
// 2
#include <iostream>
#include <cstring>

using namespace std;

struct CandyBar
{
    char name[50];
    double weight;
    int hot;
};
void fill_candybar(CandyBar &t,char * str,double w,int h);
void show_candybar(const CandyBar &t);
int main()
{
    CandyBar t;
    char str[50] = "Millennium Munch";
    double w = 2.85;
    int h = 350;

    fill_candybar(t,str,w,h);
    show_candybar(t);

    return 0;
}
void fill_candybar(CandyBar &t,char * str,double w,int h)
{
    strcpy(t.name,str);
    t.weight = w;
    t.hot = h;
}
void show_candybar(const CandyBar &t)
{
    cout << "CandyBar.name = \t" << t.name << endl;
    cout << "CandyBar.weight = \t" << t.weight << endl;
    cout << "CandyBar.hot = \t\t" << t.hot << endl;
}
// 3                // 注意对string对象的判断
#include <iostream>
using namespace std;
void string_to_toupper(string & str);
void string_show(const string & str);
int main()
{
    string str;
    while(1)
    {
        cout << "Enter a string (q to quit): ";
        getline(cin,str);
        if(str == "q")
            break;
        string_to_toupper(str);
        string_show(str);
    }
    return 0;
}
void string_to_toupper(string & str)
{
    for(int i = 0;str[i] != '\0';i++)
        str[i] = toupper(str[i]);
}
void string_show(const string & str)
{
    cout << str << endl;
}
// 4
#include <iostream>
#include <cstring>          // for strlen(),strcpy()

using namespace std;
struct stringy{
    char * str;             // points to a string
    int ct;                 // length of string(not counting '\0')
};
// prototypes for set(),show(),and show() go here
void set(stringy &beany,char arr[]);
void show(const stringy sty,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. ";
    set(beany,testing);     // first argument is a reference.
                            // allocates space to hold copy of testing,
                            // sets str member of beany to point to the
                            // new block,copies testing to new block,
                            // and set ct member of beany
    show(beany);        // prints member string once
    show(beany,2);      // prints member string twice
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);      // prints testing string once
    show(testing,3);    // prints testing string thrice
    show("Done!" );

    return 0;
}
void set(stringy &beany,char arr[])
{
    int len = strlen(arr) + 1;
    beany.str = new char[len];
    strcpy(beany.str,arr);
    beany.str[len] = '\0';
    beany.ct = len - 1;
}
void show(const stringy sty,int n)
{
   if(n <= 0)
        n = 1;
   for(int i = 0;i < n;i++)
   {
        cout << "stringy.str = \t" << sty.str << endl;
        cout << "stringy.ct = \t" << sty.ct << endl;
   }
}
void show(const char str[],int n)
{
    if(n <= 0)
        n = 1;
    for(int i = 0;i < n;i++)
        cout << str << endl;
}
// 5
#include <iostream>
using namespace std;
const  int len = 5;
template <class T>
T max5(const T ar[]);

int main()
{
    int ari[] = {1,5,2,4,3};
    double ard[] = {1.0,5.0,2.0,4.0,3.0};

    cout << "ari-max = \t" << max5(ari) << endl;
    cout << "ard-max = \t" << max5(ard) << endl;

    return 0;
}
template <class T>
T max5(const T ar[])
{
    T temp = ar[0];
    for(int i = 1;i < len;i++)
        if(temp < ar[i])
            temp = ar[i];
    return temp;
}
// 6            待验证
#include <iostream>
#include <cstring>
using namespace std;
template <class T>
T maxn(const T ar[],int n);
const char *  maxchlen(const char *str[],int n);
const char **  maxchlenaddr(const char *str[],int n);
int main()
{
    int ari[] = {1,5,2,4,3};
    double ard[] = {1.0,2.0,4.0,3.0};
    const char *arst[5] = {
        "aaaaaaaaaaaaaaaaaa",
        "bbbbbbbbbbbbbbbbbbbbb",
        "ccccccccccccccccccccccccccc",
        "ddddddddddddddddddddddddddd",
        "eeeeeeeeeeeeeeeeeeeeeeeeeee"
    };
    cout << "ari-max = \t" << maxn(ari,5) << endl;
    cout << "ard-max = \t" << maxn(ard,4) << endl;
    cout << "the theory address is\t" << &arst[2] << endl;
    cout << "the real address is\t" << maxchlenaddr(arst,5) << endl;
    cout << "the real address is\t" << maxchlen(arst,5) << endl;
    return 0;
}
template <class T>
T maxn(const T ar[],int n)
{
    T temp = ar[0];
    for(int i = 1;i < n;i++)
        if(temp < ar[i])
            temp = ar[i];
    return temp;
}
const char * maxchlen(const char *str[],int n)
{
    unsigned temp = strlen(str[0]);
    const char *add = str[0];
    for(int i = 1;i < n;i++)
        if(temp < strlen(str[i]))
        {
            temp = strlen(str[i]);
            add = str[i];
        }
    return add;
}
const char **  maxchlenaddr(const char *str[],int n)
{
    unsigned temp = strlen(str[0]);
    const char **add = &str[0];
    for(int i = 1;i < n;i++)
        if(temp < strlen(str[i]))
        {
            temp = strlen(str[i]);
            add = &str[i];
        }
    return add;
}
// 8.14 temptempover.cpp
// temptempover.cpp -- template overloading
#include <iostream>
template <typename T>
void ShowArray(T arr[],int n);              // template A

template <typename T>                       // template B
void ShowArray(T *arr[],int n);

struct debts
{
    char name[50];
    double amount;
};
int main(void)
{
    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];
    // set pointers to the amount members of the structures in the arr mr_E
    for(int i = 0;i < 3;i++)
        pd[i] = &mr_E[i].amount;

    cout << "Listing Mr.E's counts of things:\n";
    // things is an array of int
    ShowArray(things,6);        // uses template A
    cout << "Listing Mr. E's debts: \n";
    // pd is an array of pointers to double
    ShowArray(pd,3);            // uses template B(more specialized)

    return 0;
}
template <typename T>
void ShowArray(T arr[],int n)
{
    using namespace std;
    cout << "template A\n";
    for(int i = 0;i < n;i++)
        cout << arr[i] << ' ';
    cout << endl;
}
template <typename T>
void ShowArray(T * arr[],int n)
{
    using namespace std;
    cout << "template B\n";
    for(int i = 0;i < n;i++)
        cout << *arr[i] << ' ';
    cout << endl;
}
// 7
#include <iostream>
template <typename T>
long ShowArray(T arr[],int n);              // template A
template <typename T>                       // template B
double ShowArray(T *arr[],int n);
struct debts
{
    char name[50];
    double amount;
};
int main(void)
{
    using namespace std;
    int things[6] = {13,31,103,301,310,130};
    long things_sum = 0;
    struct debts mr_E[3] =
    {
        {"Ima Wolfe",2400.0},
        {"Ura Foxe ",1300.0},
        {"Iby Stout",1800.0}
    };
    double * pd[3];
    double pd_sum = 0.0;
    // set pointers to the amount members of the structures in the arr mr_E
    for(int i = 0;i < 3;i++)
        pd[i] = &mr_E[i].amount;

    // things is an array of int
    things_sum = ShowArray(things,6);        // uses template A
    cout << "thing_sum = " << things_sum << endl;

    // pd is an array of pointers to double
    pd_sum = ShowArray(pd,3);            // uses template B(more specialized)
    cout << "pd_sum = " << pd_sum << endl;

    return 0;
}
template <typename T>
long ShowArray(T arr[],int n)
{
    long arr_sum = 0;
    using namespace std;
    cout << "template A\n";
    for(int i = 0;i < n;i++)
        arr_sum = arr_sum + arr[i];

    return arr_sum;
}
template <typename T>
double ShowArray(T * arr[],int n)
{
    using namespace std;
    double arr_sum;
    cout << "template B\n";
    for(int i = 0;i < n;i++)
        arr_sum = arr_sum + *arr[i];

    return arr_sum;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值