C++ Primer Plus 第六版 第八章课后编程练习答案

1.

#include <iostream>
using namespace std;
void print(char * str, int n=0);

int main()
{
    char str[30] = "I love u.";
    print(str);
    print(str, 1);
    print(str, 2);
    print(str);

    return 0;
}

void print(char * str, int n)
{
    static int flag=0;//全局静态变量,再调用时会保持在更新后的值,而不是初始值
    flag++;
    if(n==0)
        cout << str << endl;
    else
    {
        for(int i=0; i<flag; i++)
            cout << str << endl;
    }
    cout << endl;
}

2.

#include <iostream>
using namespace std;

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

void set(CandyBar & x, const char * brand="Millennium Munch", const double weight=2.85, const int heat=350);
void show(CandyBar & x);


int main()
{
    CandyBar x0, x1, x2;
    set(x0);
    show(x0);

    set(x1, "AA", 1.11, 100);
    show(x1);

    set(x2, "BB", 2.22, 200);
    show(x2);

    return 0;
}

void set(CandyBar & x, const char * brand, const double weight, const int heat)
{
    x.brand = brand;
    x.weight = weight;
    x.heat = heat;
}

void show(CandyBar & x)
{
    cout << "Brand: " << x.brand << endl;
    cout << "Weight: " << x.weight << endl;
    cout << "Heat: " << x.heat << endl;
}



3.

#include <iostream>
#include <cctype>
using namespace std;
string ToUper(string & x);

int main()
{
    cout << "Enter a string (q to quit): ";
    string str;
    getline(cin, str);//不能cin>>str
    while (str != "q")
    {
        str = ToUper(str);
        cout << str << endl;
        cout << "Next string (q to quit): ";
        getline(cin, str);
    }
    cout << "Bye." << endl;

    return 0;
}

string ToUper(string & x)
{
    for(int i=0; x[i]; i++)
        x[i] = toupper(x[i]);
    return x;
}

4.暂

5.

#include <iostream>
using namespace std;
template<typename T>
T max5(T x[5]);

int main()
{
    int x[5] = {2, 4, 6, 8, 10};
    double y[5] = {2.2, 4.4, 6.6, 8.8, 9.9};
    int max_x;
    double max_y;
    max_x = max5(x);
    max_y = max5(y);
    cout << "Int array: " << x[0] << ", " << x[1] << ", " << x[2] << ", " << x[3] << ", " << x[4] << endl;
    cout << "Biggest num: " << max_x << endl;
    cout << "Double array: " << y[0] << ", " << y[1] << ", " << y[2] << ", " << y[3] << ", " << y[4] << endl;
    cout << "Biggest num: " << max_y << endl;

    return 0;
}

template<typename T> //T类型
T max5(T x[5])
{
    T max;
    max = x[1] > x[0] ? x[1]:x[0];
    max = x[2] > max ? x[2]:max;
    max = x[3] > max ? x[3]:max;
    max = x[4] > max ? x[4]:max;
    return max;
}

6.

#include <iostream>
using namespace std;
template <typename T>
T maxn(T arr[], int n);
char * max_n(char * arr[], int n);

int main()
{
    int arr1[6] = {1, 2, 3, 4, 5, 6};
    double arr2[4] = {1.1, 2.2, 3.3, 4.4};
    char * arr3[5] = {"a", "bbb", "cc", "dddd", "e"};
    int max1;
    double max2;
    char * max3;
    max1 = maxn(arr1, 6);
    max2 = maxn(arr2, 4);
    max3 = max_n(arr3, 5);
    cout << "Biggest num of arr1: " << max1 << endl;
    cout << "Biggest num of arr2: " << max2 << endl;
    cout << "Longest address of arr3: " << &max3 << endl;

    return 0;
}

template <typename T>
T maxn(T arr[], int n)
{
    T max=0;
    for(int i=0; i<n; i++)
        max = arr[i]>max ? arr[i]:max;
    return max;
}

char * max_n(char * arr[], int n) //返回地址
{
    int max_len = 0;
    int j = 0;
    char * address;
    for(int i=0; i<n; i++)
    {
        if(strlen(arr[i]) > max_len)
        {
            max_len = strlen(arr[i]);
            j = i;
        }
    }
    address = arr[j];//字符串地址中最长的那个
    return address;
}

7.

//原程序是2个传参及传指针的用来显示的函数ShowArray,现改成2个求和的函数SumArray()
#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};
    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;

    int sum1 = SumArray(things, 6);
    cout << "The sum of things is " << sum1 << endl;
    double sum2 = SumArray(pd, 3);
    cout << "The sum of Mr.E's debts is " << sum2 << endl;

    return 0;
}

template <typename T>
T SumArray(T arr[], int n)
{
    T 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)
{
    T sum=0;
    cout << "template B\n";
    for(int i=0; i<n; i++)
        sum += *arr[i];
    return sum;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值