C++ primer Plus(第六版)中文版 第九章 内存模型和名称空间 编程练习答案

第九章 内存模型和名称空间
1. 下面是一个头文件

//golf.h -- for pr9-1.cpp

const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};

// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguement to the function
// 函数使用传递的name、hc,设置golf结构的成员
void setgolf(golf & g, const char * name, int hc);

// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// return 1 if name is entered, 0 if name is empty string 
// 函数从用户请求name和handicap,并将g的成员设置为输入的值,
// 如果输入name则返回1,如果name为空字符串则返回0
int setgolf(golf & g);

// function resets handicap to new value
// 重置handicap为新值的函数
void handicap(golf & g, int hc);

// function displays contents of golf structure
// 用来显示golf的内容的函数
void showgolf(const golf & g);

注意到showgolf()被重载,可以这样使用其第一个版本:
golf ann;
setgolf(ann, "Ann Birdfree", 24);
上述函数调用了提供存储在ann结构中的信息,可以这样使用其第三个版本
golf andy;
setgolf(andy); 
上述函数将提示用户输入姓名和等级,并将它们存储在andy结构中,这个函数可以(但是不一定必须)在内部使用第一个版本。

根据这个头文件,创建一个多文件程序。
其中的一个文件名为golf.cpp,它提供了与有文件中的原型匹配的函数定义;
另一个文件应包含main(),并演示原型化函数的所有特性。
例如,包含一个让用户输入的循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。main()函数只使用头文件原型化的函数来访问golf结构

1.1 单个文件的情况:

#include <iostream>
#include <string>

const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};
void setgolf(golf & g, const char * name, int hc);
int setgolf(golf & g);
void handicap(golf & g, int hc);
void showgolf(const golf & g);

int main()
{
    using namespace std;
    const int size = 5;
    golf array[size];
    int num = 0;
    int setgolf_return;
    
     for (int i = 0; i < size; i++)
    {
        setgolf_return = setgolf(array[i]);
        if(setgolf_return == 0)
            break;
        else
            num += setgolf_return;         //或 num++;
    }
   
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }
    
    handicap(array[0], 0);
    cout << "修改第1个的handicap为0后:\n";
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }

    setgolf(array[0], "Ann Birdfree", 24);
    cout << "修改第1个的fullname和handicap后:\n";
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }
    
    return 0;
}

int setgolf(golf & g)
{
    using namespace std;
    int t;
    cout << "请输入姓名:";
    cin.get(g.fullname, Len);
    if (strlen(g.fullname)==0)
        return 0;
    else
    {
        cin.get();
        cout << "请输入等级:";
        cin >> g.handicap;
        cin.get();
        return 1;
    }
}

void setgolf(golf & g, const char * name, int hc)
{
    strcpy(g.fullname, name);
    g.handicap = hc;
}
  
void handicap(golf & g, int hc)
{
    g.handicap = hc;
}

void showgolf(const golf & g)
{
    using namespace std;
    cout << "姓名:\t" << g.fullname << endl;
    cout << "等级\t" << g.handicap << endl << endl;
}
        

//对于输入字符串的处理方式,还可以:
int setgolf(golf & g)
{
    using namespace std;
    int t;
    cout << "请输入姓名:";
    cin.get(g.fullname, Len).get();      //或cin.getline(g.fullname, Len);
    if (strlen(g.fullname)==0)
        return 0;
    else
    {
        cout << "请输入等级:";
        cin >> g.handicap;
        cin.get();
        return 1;
    }
}

1.2 将以上单个文件,分成三个文件

golf.h
const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};

void setgolf(golf & g, const char * name, int hc);

int setgolf(golf & g);

void handicap(golf & g, int hc);

void showgolf(const golf & g);
//golf.cpp
#include <iostream>
#include "golf.h"

int setgolf(golf & g)
{
    using namespace std;
    int t;
    cout << "请输入姓名:";
    cin.get(g.fullname, Len);
    if (strlen(g.fullname)==0)
        return 0;
    else
    {
        cin.get();
        cout << "请输入等级:";
        cin >> g.handicap;
        cin.get();
        return 1;
    }
}

void setgolf(golf & g, const char * name, int hc)
{
    strcpy(g.fullname, name);
    g.handicap = hc;
}
  
void handicap(golf & g, int hc)
{
    g.handicap = hc;
}

void showgolf(const golf & g)
{
    using namespace std;
    cout << "姓名:\t" << g.fullname << endl;
    cout << "等级\t" << g.handicap << endl << endl;
}
//pe9-1.cpp
#include <iostream>
#include "golf.h"

int main()
{
    using namespace std;
    const int size = 5;
    golf array[size];
    int num = 0;
    int setgolf_return;
    
     for (int i = 0; i < size; i++)
    {
        setgolf_return = setgolf(array[i]);
        if(setgolf_return == 0)
            break;
        else
            num += setgolf_return;         //或 num++;
    }
   
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }
    
    handicap(array[0], 0);
    cout << "修改第1个的handicap为0后:\n";
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }

    setgolf(array[0], "Ann Birdfree", 24);
    cout << "修改第1个的fullname和handicap后:\n";
    for (int i = 0; i < num; i++)
    {
        cout << "第" << i+1 << "个:\n";
        showgolf(array[i]);
    }
    
    return 0;
}

2. 修改程序清单9.9: 用string类代替字符数组。
这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串""进行比较,以判断是否为空行。
程序清单9.9如下:

//static.cpp -- using a static local variable
#include <iostream>
// constants
const int ArSize = 10;

// function prototype
void strcount(const char * str);

int main()
{
    using namespace std;
    char input[ArSize];
    char next;

    cout << "Enter a line:\n";
    cin.get(input, ArSize);
    while (cin)
    {
        cin.get(next);
        while (next != '\n')    // string didn't fit!
            cin.get(next);      // dispose of remainder
        strcount(input);
        cout << "Enter next line (empty line to quit):\n";
        cin.get(input, ArSize);
    }
    cout << "Bye\n";
// code to keep window open for MSVC++
/*
cin.clear();
    while (cin.get() != '\n')
        continue;
    cin.get();
*/
    return 0;
}

void strcount(const char * str)
{
    using namespace std;
    static int total = 0;        // static local variable
    int count = 0;               // automatic local variable

    cout << "\"" << str <<"\" contains ";
    while (*str++)               // go to end of string
        count++;
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

//修改后

#include <iostream>
#include <string>
using namespace std;
// function prototype
void strcount(const string str);

int main()
{
    string input;

    cout << "Enter a line:\n";
    getline(cin, input);
    while (input != "")
    {
        strcount(input);
        cout << "Enter next line (empty line to quit):\n";
        getline(cin, input);
    }
    cout << "Bye\n";

    return 0;
}

void strcount(const string str)
{
    static int total = 0;        // static local variable
    int count = 0;               // automatic local variable

    cout << "\"" << str <<"\" contains ";
    while (str[count])               // go to end of string
        count++;
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

3. 下面是一个结构声明:
struct chaff
{
    char dross[20];
    int slag;
};
编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。
然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。
一种方法是像程序清单9.10 那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区。

3.1 方法一

#include <iostream>
#include <new>
#include <string>

struct chaff
{
    char dross[20];
    int slag;
};

int main()
{
    using namespace std;
    
    char buffer[512];
    cout << "buffer的地址为:\t" << (void *)buffer << endl;
    
    chaff * p1 = new (buffer) chaff[2];
    cout << "p1的地址为:\t" << p1 << endl;
    
    strcpy(p1[0].dross, "abc");
    p1[0].slag = 1;

    strcpy(p1[1].dross, "def");
    p1[1].slag = 2;
    
    for (int i = 0; i < 2; i++)
    {
        cout << "p1[" << i << "]  at  " << &p1[i] << endl;
        cout << "dross:\t" << p1[i].dross << endl;
        cout << "slag:\t" << p1[i].slag << endl;
    }
    
    return 0;
}

方法二

#include <iostream>
#include <new>
#include <string>

struct chaff
{
    char dross[20];
    int slag;
};

int main()
{
    using namespace std;
    
    char * buffer = new char [512];
    cout << "buffer的地址为:\t" << (void *)buffer << endl;
    
    chaff * p1 = new (buffer) chaff[2];
    cout << "p1的地址为:\t" << p1 << endl;
    
    strcpy(p1[0].dross, "abc");
    p1[0].slag = 1;

    strcpy(p1[1].dross, "def");
    p1[1].slag = 2;
    
    for (int i = 0; i < 2; i++)
    {
        cout << "p1[" << i << "]  at  " << &p1[i] << endl;
        cout << "dross:\t" << p1[i].dross << endl;
        cout << "slag:\t" << p1[i].slag << endl;
    }
    
    return 0;
}


4. 请基于下面的名称空间编一个由3个文件组成的程序

namespace SALES 
{
    const int QUARTERS = 4;
    struct Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    };
    
    // copies the lesser of 4 or n items from the array ar
    // to the sales member of s and computes and stores the
    // average, maximum, and minimum values of the entered items; 
    // remaining elements of sales, if any, set to 0
    // 将含有最多4个元素的数组ar复制到s结构的sales成员中,计算并存储输入项的平均值,最大值和最小值,如果有剩余的sales元素,设置为0
    void setSales(Sales & s, const double ar[], int n);
    // gathers sales for 4 quarters interactively, stores them 
    // in the sales member of s and computes and stores the
    // average, maximum, and minimum values
    // 以交互方式收集4个季度的销售额,将它们存储在s的sales成员中并计算并存储平均值,最大值和最小值
    void setSales(Sales & s);
    // display all information in structure s
    // 显示s结构中的所有信息
    void showSales(const Sales & s);
}

第一个文件是一个头文件,其中包含名称空间;
第二个文件是一个源代码文件,它对这个名称空间进行拓展,以提供这三个函数的定义;
第三个文件声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,
          然后使用setSales()的非交互式版本,为另一个结构提供值。
          另外它还使用showSales()来显示这两个结构的内容。
          
//头文件sales

//sales.h
namespace SALES 
{
    const int QUARTERS = 4;
    struct Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    };

    void setSales(Sales & s, const double ar[], int n);

    void setSales(Sales & s);

    void showSales(const Sales & s);
}

//源文件1

#include <iostream>
#include "sales.h"

namespace SALES
{
    void setSales(Sales & s, const double ar[], int n)
    {
        for (int i = 0; i < n; i++)
            s.sales[i] = ar[i];
        if (n < QUARTERS)
            for (int i = n; i < QUARTERS; i++)
                s.sales[i] = 0;
        
        double sum = 0;      
        double max = s.sales[0];
        double min = s.sales[0];
        for (int i =0; i < QUARTERS; i++)
        {
            sum += s.sales[i];
            if (max < s.sales[i])
                max = s.sales[i];
            if (min > s.sales[i])
                min = s.sales[i];
        }
        
        s.average = sum/QUARTERS;
        s.max = max;
        s.min = min;     
    }
        
    void setSales(Sales & s)
    {
        for (int i = 0; i < QUARTERS; i++)
        {
            using namespace std;
            cout << "请输入第" << i+1 << "季度的销售额:";
            cin >> s.sales[i];
            cin.get();
        }
        
        double sum = 0;
        double max = s.sales[0];
        double min = s.sales[0];
        for (int i =0; i < QUARTERS; i++)
        {
            sum += s.sales[i];
            if (max < s.sales[i])
                max = s.sales[i];
            if (min > s.sales[i])
                min = s.sales[i];
        }
        
        s.average = sum/QUARTERS;
        s.max = max;
        s.min = min;  
    }        
            
    void showSales(const Sales & s)
    {
        using namespace std;
        cout << "四个季度的销售额为:\n";
        for (int i =0; i < QUARTERS; i++)
            cout << "第" << i+1 << "季度的销售额:\t" << s.sales[i] << endl;
        cout << endl;
        cout << "平均值为:\t" << s.average << endl;
        cout << "最大值为:\t" << s.max << endl;
        cout << "最小值为:\t" << s.min << endl;
        cout << endl << endl;
    }
}

//源文件2

#include <iostream>
#include "sales.h"

using namespace SALES;

int main()
{
    Sales object1;
    Sales object2;
    Sales object3;
    
    double ar1[4] = {1,2,3,4};
    double ar2[3] = {1,2,3};

    setSales(object1);
    
    setSales(object2, ar1, 4);
    setSales(object3, ar2, 3);

    showSales(object1);
    showSales(object2);
    showSales(object3);

    return 0;
}


 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值