C++PrimerPlus中文第六版第8章编程练习答案

1、

复制代码
#include<iostream>
#include<string>
using namespace std;

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

const int Size = 50;
int main()
{
    char s[Size] = "test";
    show(s);
    show(s, 5);
}

void show(const char* str, int n)
{
    cout << str << endl;
    if (n > 1)
    {
        show(str, n - 1);
    }
}
复制代码

2、

复制代码
#include<iostream>
#include<cstring>
using namespace std;

const int Size = 30;

struct CandyBar
{
    string brand;
    double weight;
    int heat;
};

void setCandy(CandyBar& c, string brand = "Millennium", double wight = 2.85, int heat = 350);
void showCandy(const CandyBar& c);
int main()
{
    CandyBar candy;
    setCandy(candy);
    showCandy(candy);
}

void setCandy(CandyBar& c, string brand, double weight, int heat)
{
    c.brand = brand;
    c.weight = weight;
    c.heat = heat;
}
void showCandy(const CandyBar& c)
{
    cout << "Brand: " << c.brand << endl;
    cout << "Weight: " << c.weight << endl;
    cout << "Heat: " << c.heat << endl;
}
复制代码

3、

复制代码
#include<iostream>
#include<string>
using namespace std;

void toUpper(string& s)
{
    for (int i = 0; i < s.length(); i++)
    {
        if(isalpha(s[i]))
            s[i] = toupper(s[i]);
    }
}
int main()
{
    string s;
    cout << "Enter a string (q to quit): ";
    getline(cin, s);
    while (s != "q")
    {
        toUpper(s);
        cout << s << endl;
        cout << "Next string (q to quit): ";
        getline(cin, s);
    }
    cout << "Bye.\n";

}
复制代码

4、

复制代码
#include<iostream>
using namespace std;

struct stringy
{
    char* str;
    int ct;
};

void set(stringy& s, const char* str);
void show(const stringy& s, int n = 1);
void show(const char* s, int n = 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!");
}
void show(const char* s, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "str: "<<s << endl;
    }
}
void show(const stringy& s, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "str: "<<s.str << endl;
        cout << "ct: "<<s.ct << endl;
    }
}
void set(stringy& s, const char* str)
{
    char* testing = new char[strlen(str) + 1];
    s.str = testing;
    int i = 0;
    for (i = 0; i < strlen(str); i++)
    {
        testing[i] = str[i];
    }
    testing[i] = '\0';
    s.ct = strlen(str);
}
复制代码

5、

复制代码
#include<iostream>
using namespace std;

template<typename T>T max5(T a[]);

int main()
{
    int a[5] = { 1,3,5,2,4 };
    double b[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
    cout << "max int: " << max5(a) << endl;
    cout << "max double: " << max5(b) << endl;
}

template<typename T>T max5(T a[])
{
    T max = a[0];
    for (int i = 1; i < 5; i++)
        if (max < a[i])
            max = a[i];
    return max;
}
复制代码

6、

复制代码
#include<iostream>
using namespace std;

template<typename T>T maxn(T a[], int n);
template<> char* maxn<char*>(char* s[], int n);

int main()
{
    int a[6] = { 1,3,5,2,4,6 };
    double b[4] = { 1.1,3.3,2.2,4.4 };
    cout << "max int: " << maxn(a, sizeof(a) / sizeof(int)) << endl;
    cout << "max double: " << maxn(b, sizeof(b) / sizeof(double)) << endl;
    char* s1 = (char*)"Lining", * s2 = (char*)"Anta", * s3 = (char*)"Adiddas", * s4 = (char*)"Nike", * s5 = (char*)"Kwasaki";
    char* s[5] = { s1, s2, s3, s4, s5 };
    cout << "max *char*: " << maxn(s, 5) << endl;

}
template<> char* maxn<char*>(char* s[], int n)
{
    int maxidx = 0;
    int maxlen = strlen(s[0]);
    for (int i = 1; i < 5; i++)
    {
        if (strlen(s[i]) > maxlen)
        {
            maxidx = i;
            maxlen = strlen(s[i]);
        }
    }
    return s[maxidx];
}
template<typename T>T maxn(T a[], int n)
{
    int maxidx = 0;
    for (int i = 1; i < n; i++)
        if (a[i] > a[maxidx])
            maxidx = i;
    return a[maxidx];
}
复制代码

7、

复制代码
#include<iostream>
using namespace std;

template<typename T>int SumArray(T a[], int n);
template<typename T>T SumArray(T* a[], int n);
struct debts
{
    char name[50];
    double amount;
};
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}
    };
    double* pd[3];
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;

    cout << "SumArray(things): " << SumArray(things, 6) << endl;
    cout << "SumArray(debts): " << SumArray(pd, 3) << endl;
}

template<typename T>T SumArray(T* a[], int n)
{
    T total = 0;
    for (int i = 0; i < n; i++)
    {
        total += *a[i];
    }
    return total;
}
template<typename T>int SumArray(T a[], int n)
{
    T total = 0;
    for (int i = 0; i < n; i++)
    {
        total += a[i];
    }
    return total;
}
复制代码

 

posted @   Triphan  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示