C++ primer plus 第六版 第八章 编程练习答案

第八章 编程练习答案

1.

一开始没看懂题目意思,百度参考了一下别人的结果。

#include <iostream>

using namespace std;
int count = 0;
void Printf(const char * str, int n = 0);//默认参数 

int main()
{
    const char* str = "hello!";
    Printf(str);
    Printf(str, 1);
    Printf(str, 0);
    Printf(str, -1);        
    return 0;
}


void Printf(const char * str, int n)
{
    count++;        
    if(n == 0)
        cout << str << endl;
    else{
        for(int i = 0; i < count ; i++)
            cout << str << endl;    
    }    
    cout << "-----------------" << endl;    
}

2.

没有做到多用const,下次再说吧。

#include <iostream>
#include <cstring>
using namespace std;

const char size = 20; 
struct CandyBar{
    char brand[size];
    double weight;
    int calories;   
}; 

void fill(CandyBar & fc, char * fb = "Millennium Munch", double weight = 2.85, int calories = 350);
//注意结构的引用
void show(CandyBar const & fc);

int main()
{
    CandyBar a, b;
    cout << "The brand #1: \n"; 
    fill(a);//这里用了默认参数 
    show(a);
    cout << endl;       

    char brand[size];
    double weight;
    int calories;
    cout << "Please enter the brand: ";
    cin.getline(brand, size);
    cout << "Please enter the weight: ";
    cin >> weight;
    cout << "Please enter the calories: ";  
    cin >> calories;
    cout << endl;

    cout << "The brand #2: \n"; 
    fill(b, brand, weight, calories);   
    show(b);

    return 0;
}

void fill(CandyBar & fc, char * fb, double weight, int calories)
{
    strcpy(fc.brand, fb);
//字符串的复制用了cstring中的strcpy,不能直接用赋值=,不能将指针赋值给常量 
    fc.weight = weight;
    fc.calories = calories;
}


void show(CandyBar const & fc)
{
    cout << "Brand: " << fc.brand << endl;
    cout << "Weight: " << fc.weight << endl;    
    cout << "Calories: " << fc.calories << endl;        
}

3.

#include <iostream>
#include<cstring>
#include<cctype>

using namespace std;

void change(string &f);

int main()
{
    string input;

    cout << "Enter a string (q to quit): ";
    getline(cin, input);
    while ( input[0] != 'q' || input.size() > 1)//string.size()获取string的长度 
//控制输入条件,只有输入q时退出,首字母为q后面有其他内容不退出 
    {
        change(input);
        cout << input << endl;
        cout << "Enter a string (q to quit): "; 
        getline(cin, input);
    }   

    cout << "bye" <<endl;   
    return 0;   
} 


void change(string &f)
{

    for(int i = 0; i < f.size(); i++)
    { 
        f[i] = toupper(f[i]);//转换大写
    }
}

4.

#include <iostream>
#include <cstring>

using namespace std;

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

void set(stringy & s, char * ch);
//引用参数,注意这里结构成员是一个指向char的指针 
void show(stringy & s, int n = 1);
void show(char * fc, 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!");
    return 0;
}

void set(stringy & s, char * ch)
{   
    s.ct = strlen(ch);//获取字符长度 
    s.str = ch;
}

void show(stringy & s, int n)
{
    for(int i = 0; i < n; i++)
        cout << s.str << endl;      
    cout << endl;   
}

void show(char * fc, int n)
{
    int count = 0, j = 0;
    count = strlen(fc);//获取字符长度 
    while(j < n)//控制显示次数 
    {
        for(int i = 0; i < count; i++)
            cout << fc[i];//char类型逐个显示 
        cout << endl;
        j++;        
    }
    cout << endl;       
}

5.

#include <iostream>

using namespace std;

template <typename T>
T max5(T str[5]);//模板函数 

int main()
{
    int s1[5];
    cout << "Please enter five int numbers: " << endl;
    for (int i = 0; i < 5; i++)
        cin >> s1[i];
    cout << "The max is: " << max5(s1) << endl;

    double s2[5];
    cout << "Please enter five double numbers: " << endl;
    for (int i = 0; i < 5; i++)
        cin >> s2[i];       
    cout << "The max is: " << max5(s2) << endl; 

    return 0;   
} 

template <typename T>
T max5(T str[5])
{
    T temp = str[0];
    for(int i = 0; i < 5; i++ )
    {
        if(str[i] > temp)
            temp = str[i];
    }
    return temp;
}

6.

#include <iostream>
#include <cstring>

using namespace std;

template <typename T>
T maxn(T str[], int n);//模板函数 

template <> char *maxn(char *p[], int n);
//返回最长的字符串的地址 



int main()
{
    int n;
    cout << "Please enter lentrh of array: " << endl;
    cin >>  n;
    int s1[n];
    cout << "Please enter numbers: " << endl;
    for (int i = 0; i < n; i++)
        cin >> s1[i];
    cout << "The max is: " << maxn(s1,n) << endl;

    cout << "Please enter lentrh of array: " << endl;
    cin >>  n;
    double s2[n];
    cout << "Please enter numbers: " << endl;
    for (int i = 0; i < n; i++)
        cin >> s2[i];       
    cout << "The max is: " << maxn(s2,n) << endl;

    char *s3[5] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
    cout << endl << "Monday , Tuesday , Wednesday , Thursday , Friday" << endl;
    cout << "The address  of max is: " << maxn(s3, 5) << endl;  

    return 0;   
} 

template <typename T>
T maxn(T str[], int n)
{
    T temp = str[0];
    for(int i = 0; i < n; i++ )
    {
        if(str[i] > temp)
            temp = str[i];
    }
    return temp;
}

template <> char *maxn(char *p[], int n)//具体化模板 
{
    int max = 0;
    char * temp = p[0];
    for(int i = 0; i < n; i++)
    {
        if((int)strlen(p[i])> max)//注意比较地址长度 
        {
            max = strlen(p[i]);
            temp = p[i];                
        }
    }
    return temp;
}

7.

#include <iostream>

using namespace std;
//修改函数返回类型和函数结构
//增加显示数组的和 
template <typename T>
int ShowArray(T arr[], int n);

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

struct debts
{
    char name[50];
    double amount;
};

int main()
{
    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];

    for (int i =0; i < 3; i++)
        pd[i] = &mr_E[i].amount;

    cout << "Listing Mr.E's counts of things:\n";
    int thingsum;
    thingsum = ShowArray(things, 6);
    cout << "The sum of things:" << thingsum << endl << endl;

    cout << "Listing Mr.E's debts:\n";
    double debtsum;
    debtsum = ShowArray(pd, 3);
    cout << "The sum of debts:" << debtsum << endl << endl; 

    return 0;
}

template <typename T>
int ShowArray(T arr[], int n)
{
    int sum = 0;
    cout << "template A\n";
    for (int i =0; i < n; i++)    
        {
        cout << arr[i] << ' ';
        sum += arr[i];  
        }
    cout << endl;
    return sum;
}

template <typename T>
double ShowArray(T * arr[], int n)
{
    double sum = 0.0;
    cout << "template B\n";
    for (int i =0; i < n; i++)
    {
        cout << *arr[i] << ' ';
        sum += *arr[i]; 
    }
    cout << endl;
    return sum;     
}

猜你喜欢

转载自blog.csdn.net/weixin_41882882/article/details/81388276