C++ Primer Plus第八章练习

8.8.1

#include <iostream>
using namespace std;
void show(const char *, int n = 0);
int main(void)
{
    show("xswl");
    show("xswl", 1);
    return 0;
}
void show(const char * s1, int n)
{
    if(n)
        cout << s1 << endl;
}

8.8.2

#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar
{
    char name[30];
    float weith;
    int heat;
};
void inbar(CandyBar &, const char * name = "Millennium Munch", const double weith = 2.85, const int heat = 350);
void outbar(const CandyBar &);
int main(void)
{
    CandyBar s, b;
    inbar(s);
    outbar(s);
    inbar(b, "xswl", 5.2, 46);
    outbar(b);
    return 0;
}
void inbar(CandyBar & cand, const char * name, const double weith, const int heat)
{
    strcpy(cand.name, name);
    cand.weith = weith;
    cand.heat = heat;
}
void outbar(const CandyBar & cand)
{
    cout << cand.name << endl;
    cout << cand.weith << endl;
    cout << cand.heat << endl;
}

8.8.3

#include <iostream>
#include <cctype>
using namespace std;
void transition(string &);
int main(void)
{
    string x;
    cout << " Enter a string (q to quit): ";
    while(getline(cin, x) && x[0] != 'q')
    {
        transition(x);
        cout << x << endl;
        cout << "Next string (q to quit): ";
    }
    return 0;
}
void transition(string & s)
{
    for(int i = 0; i < s.size(); i++)
        s[i] = toupper(s[i]);
}

8.8.4

#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
    char * str;
    int ct;
};
void set(stringy &, const char *);
void show(const stringy &, int i = 1);
void show(const char * s1, int i = 1);
int main(void)
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";
    set(beany, testing);
    show(beany);
    show(beany);
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);
    show(testing, 3);
    show("Done!");
    delete beany.str;
    return 0;
}
void set(stringy & s1, const char * s2)
{
    s1.str = new char[strlen(s2) + 1];
    strcpy(s1.str, s2);
}
void show(const stringy & s1, int n)
{
    while(n--)
        cout << s1.str << endl;
}
void show(const char * s1, int n)
{
    while(n--)
        cout << s1 << endl;
}

8.8.5

#include <iostream>
using namespace std;
template<typename T>
T max5(T []);
int main(void)
{
    int arr1[5] = {5, 3, 16, 9, 4};
    double arr2[5] = {1.1, 2.2, 6.6, 3.3, 8.4};
    int intmax = max5(arr1);
    double doublemax = max5(arr2);
    cout << intmax << "  " << doublemax << endl;
    return 0;
}
template<typename T>
T max5(T arr[])
{
    T max = arr[0];
    for(int i = 0; i < 4; i++)
        if(max < arr[i + 1])
            max = arr[i + 1];
    return max;
}

8.8.6

#include <iostream>
#include <cstring>
using namespace std;
template<typename T>
T maxn(T [], int);
template<> char * maxn<char *>(char * [], int);
int main(void)
{
    int arr1[6] = {5, 3, 8, 9, 5, 10};
    double arr2[6] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
    const char * d[5] = {"xswl", "nmsl", "wssb", "nys", "wdnmd"};
    int max1 = maxn(arr1, 6);
    double max2 = maxn(arr2, 6);
    const char * max3 = maxn(d, 5);
    cout << max1 << endl
         << max2 << endl
         << max3 << endl;
    return 0;
}
template<typename T>
T maxn(T arr[], int n)
{
    T max = arr[0];
    for(int i = 0; i < n - 1; i++)
        if(max < arr[i + 1])
            max = arr[i + 1];
    return max;
}
template<> char * maxn<char *>(char * arr[], int n)
{
    int max = 0;
    for(int i = 0; i < n - 1; i++)
        if(strlen(arr[max]) < strlen(arr[i + 1]))
            max = i + 1;
    return arr[max];
}

8.8.7

#include <iostream>
using namespace std;
struct debts
{
    char name[50];
    double amount;
};
template <typename T>
T SumArray(T [], int);
template <typename T>
double SumArray(T * [], int);
int main(void)
{
    int things[6] = {13, 31, 103, 301, 2, 4};
	struct debts mr[3] = 
	{
		{"xswl", 2400.0},
		{"wdnmd", 1200.0},
		{"wssb", 1100.0},
	};
	double * pd[3];
	for (int i = 0; i < 3; i++)
		pd[i] = &mr[i].amount;
	cout << "the total number of Mr. E's things: " << SumArray(things, 6) << endl;
	cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl;
    return 0;
}
template <typename T>
T SumArray(T ar[], int n)
{
    T sum = 0;
    for(int i = 0; i < n; i++)
        sum += ar[i];
    return sum;
}
template <typename T>
double SumArray(T * ar[], int n)
{
    double sum = 0;
    for(int i = 0; i < n; i++)
        sum += *ar[i];
    return sum;        
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值