《C++ Primer Plus》——编程练习答案(7)

来源:互联网 发布:韩国女主播软件 编辑:程序博客网 时间:2024/06/06 06:50

第八章 函数探幽

8.8.1 有点没看懂题目表达什么,由n决定输出的次数(函数调用的次数),那么就递归喽。

#include <iostream>#include <string>using namespace std;void printf_m(string s, int n = 0);int main(){    string sInput = "I am a student.";    printf_m(sInput);    int Time;    cout << "Enter times:  ";    cin >> Time;    printf_m(sInput, Time);    cin.sync();    cin.get();    return 0;}void printf_m(string s, int n){     cout << s << endl;     if (n==0||n==1)     {         return;     }     else     {         printf_m(s, n - 1);     }}

这里写图片描述

8.8.2

#include <iostream>const int SIZE_CB = 20;using namespace std;struct CandyBar{    char CBname[SIZE_CB];    double CBweight;    int CBheat;};void SetCandyBar(CandyBar &x, char *p = "Millennium Munch", double w = 2.85, int h = 350);int main(){    CandyBar T;    SetCandyBar(T);    SetCandyBar(T,"Arthur Yong",155.25,78);    cin.get();    return 0;}void SetCandyBar(CandyBar &x, char *p, double w, int h){    void ShowCandyBar(const CandyBar &c);    int i = 0;    while (p[i]!='\0')    {        x.CBname[i] = p[i];        i++;    }    x.CBname[i] = '\0';    x.CBweight = w;    x.CBheat = h;    ShowCandyBar(x);}void ShowCandyBar(const CandyBar &c){    cout <<"CandyBar name:  "<< c.CBname << endl;    cout << "CandyBar weight:  " << c.CBweight<< endl;    cout << "CandyBar heat:  " << c.CBheat<< endl;}

这里写图片描述

8.8.3
这里要说一下 书上说对string类使用toupper()完成大小写转换,可是在std标准库中,cctype中的函数只能用于char型,因此必须借助STL的transform才能完成上述要求。

#include <iostream>#include <string>#include <cctype>#include <algorithm>   //transformusing namespace std; void TOUPPER(string &temp);int main(){    string T;    cout << "Enter a string (q to quit): ";    getline(cin, T);    while (T!="q")    {        TOUPPER(T);        cout << T << endl;        cout << "Next string (q to quit): ";        getline(cin, T);    }    cout << "Bye!" << endl;    cin.get();    return 0;}void TOUPPER(string &temp){    transform(temp.begin(), temp.end(), temp.begin(), ::toupper);   

这里写图片描述

8.8.4
分别使用了函数的重载和默认参数完成

#include <iostream>using namespace std;#include <cstring>struct stringy{    char *str;    int ct;};void set(stringy &x, char *s);void show(stringy const &x);void show(stringy const &x, int n);void show(char *s, int n=0);int main(){    stringy beany;    char testing[] = "Really 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!");    cin.get();    return 0;}void set(stringy &x, char *s){    x.str = s;    int len = 0;    while (*s!='\0')    {        len++;        s++;    }    x.ct = len;}void show(stringy const &x){    cout <<"str:  "<< x.str<<endl;    cout <<"ct:  "<< x.ct<<endl;}void show(stringy const &x, int n){    for (int i = 0; i < n; i++)    {        cout << "str:  " << x.str << endl;        cout << "ct:  " << x.ct << endl;    }}void show(char *s, int n){    cout << s << endl;    if (n==0||n==1)    {        return;    }    else    {        show(s, n - 1);    }}

这里写图片描述

8.8.5

#include <iostream>using namespace std;const int SIZE = 5;template<typename T>T Max_(T x[SIZE]);int main(){    int a[SIZE] = { 32, 65, 32, 21, 66 };    double b[SIZE] = { 54.32, 54.23, 34.67, 87.4, 23.6 };    cout << "a#: " << Max_<int>(a) << endl;    cout << "b#: " << Max_<double>(b) << endl;    cin.get();    return 0;}template<typename T>T Max_(T x[SIZE]){    T temp = x[0];    for (int i = 1; i < SIZE; i++)    {        if (x[i]>temp)        {            temp = x[i];        }    }    return temp;}

这里写图片描述

8.8.6

#include <iostream>using namespace std;template<typename T>T Max(T x[], int n);//////////template<> char * Max<char *>(char* x[], int n);int main(){    int a[6] = { 56, 32, 43, 54, 2, 45 };    double b[4] = { 54.32, 34.6, 23.4, 12.34 };    char *c[3] = { "I am a student", "Avalon_Y", "Arthur Yong" };    cout << "a# : " << Max<int>(a, 6) << endl;    cout << "b# : " << Max<double>(b, 4) << endl;    cout << "c# : " << Max<char *>(c, 3) << endl;    cin.get();    return 0;}template<typename T>T Max(T x[], int n){    if (x==NULL)    {        cerr << "error!" << endl;        exit(1);    }    T temp=x[0];    for (int i = 1; i < n; i++)    {        if (x[i]>temp)        {            temp = x[i];        }    }    return temp;}template<> char * Max<char *>(char* x[], int n){    if (x == NULL)    {        cerr << "error!" << endl;        exit(1);    }    char *temp =x[0];    for (int i = 1; i < n; i++)    {        if (strlen(x[i])>strlen(temp))        {            temp =x[i];        }    }    return temp;}

这里写图片描述

8.8.7

#include <iostream>using namespace std;struct debts{    char name[50];    double amount;};template<typename T>void Sumarray(T *x, int n);template<> void Sumarray<debts>(debts* x, int n);int main(){    int things[6] = { 13, 31, 103, 301, 310, 130 };    debts mr_E[3] =     {        { "Ima Wolfe", 2400.0 },         { "Ura Foxe", 1300.0 },        { "Iby Stout", 1800.0 }    };    Sumarray<int>(things, 6);    Sumarray<debts>(mr_E, 3);    cin.get();    return 0;}template<typename T>void Sumarray(T *x, int n){    T temp=0;    for (int i = 0; i < n; i++)    {        temp += x[i];    }    cout << "Sum: " << temp << endl;}template<> void Sumarray<debts>(debts* x, int n){    double temp = 0;    for (int i = 0; i < n; i++)    {        temp += x[i].amount;    }    cout << "Sum: " << temp << endl;}

这里写图片描述

0 0