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

第九章 编程练习答案

1.

//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 <cstring>
#include "golf.h"


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

int setgolf(golf & g)
{
    using namespace std;
    int temp = 1;

    cout << "The fullname is: ";
    cin.getline(g.fullname, Len);//获取字符串 
    if(strcmp(g.fullname, "") == 0)//判断输入行是否为空 
    {
        temp = 0;
        return temp;        
    }
    else{
        cout << "The handicap is: ";
        cin >> g.handicap;  
        cin.get();//注意数字输入时要去掉末尾的结束符 
        return temp;                
    }
}

void handicap(golf & g, int hc)
{
    g.handicap = hc;
}

void showgolf(const golf & g)
{
    using namespace std;
    cout << "The fullname is: " << g.fullname << endl;
    cout << "The handicap is: " <<g.handicap<<endl; 
}
//main.cpp
#include <iostream>
#include "golf.h"

int main()
{
    using namespace std;
    golf g[10];
    int count = 0;
    cout << "Enter the information of golf player: " << endl;    
    while((count < 10) && (setgolf(g[count])))
    {
        cout << "Next golf player: " << endl;       
        count++;    
    }


    cout << "\nShow all golf player information: " << endl;
    for (int i = 0; i < count; i++)
    {
        showgolf(g[i]);
    }

    cout << "\nReset the value of handicap, and "
            "Show all golf player information: " << endl;
    for (int i = 0; i < count; i++)
    {
        handicap(g[i], 99);
        showgolf(g[i]);
    } 


    return 0;
}

2.

#include <iostream>
#include <cstring>//string库不能忘记 

using namespace std;

void strcount(const string str);

int main()
{
    string input;   
    cout << "Enter a line:\n";
    getline(cin, input);//string类输入 
    while(input != "")//string类 可以直接用!=判断 
    {
        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;//静态局部变量,只在程序开始时初始化一次 
    int count = 0;

    cout << "\"" << str << "\" contains ";
    count =str.size();//获取string的长度 
    total += count;
    cout << count << " characters\n";
    cout << total << " haraccters total\n"; 
}

3.

#include <iostream>
#include <cstring>
#include <new>

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

const int BUF = 512;
const int N = 2;
char buffer[BUF];
const char ch[] = "hello";

int main()
{
    using namespace std;
    chaff *p1, *p2;
    int i;
    cout << "Calling new and placement new:\n";
    p1 = new chaff;
    p2 = new (buffer) chaff;
    for (i = 0; i < N; i++){
        p2[i].slag = p1[i].slag = 1000 + 20 * i;        
        strcpy(p1[i].dross, ch);    
        strcpy(p2[i].dross, ch);    
    }       
    cout << "Memory addresses:\n" << "  heap: " << p1
         << "   static: " << (void*) buffer << endl;
    cout << "Memory contents:\n";
    for (i = 0; i < N; i++)
    {
        cout << p1[i].dross << " at " << &p1[i].dross << endl;
        cout << p1[i].slag << " at " << &p1[i].slag << endl;

        cout << "                   " 
             << p2[i].dross << " at " << &p2[i].dross << endl;
        cout << "                    " 
             << p2[i].slag << " at " << &p2[i].slag << endl;        
    }   

    cout << "\nCalling new and placement new a second time:\n";
    chaff *p3, *p4; 
    p3 = new chaff;
    p4 = new (buffer) chaff;
    for (i = 0; i < N; i++)
    {
        p4[i].slag = p3[i].slag = 1000 + 40 * i;        
        strcpy(p1[i].dross, ch);    
        strcpy(p2[i].dross, ch);        
    }
    cout << "Memory addresses:\n" << "  heap: " << p1
         << "   static: " << (void*) buffer << endl;
    cout << "Memory contents:\n";
    for (i = 0; i < N; i++)
    {
        cout << p3[i].dross << " at " << &p3[i].dross << endl;
        cout << p3[i].slag << " at " << &p3[i].slag << endl;

        cout << "                   " 
             << p4[i].dross << " at " << &p4[i].dross << endl;
        cout << "                    " 
             << p4[i].slag << " at " << &p4[i].slag << endl;        
    }

    cout << "\nCalling new and placemen new a third time:\n";
    delete [] p1;
    p1 = new chaff;
    p2 = new (buffer + N * sizeof(chaff)) chaff;
    for (i = 0; i < N; i++)
    {
        p2[i].slag = p1[i].slag = 1000 + 60 * i;        
        strcpy(p1[i].dross, ch);    
        strcpy(p2[i].dross, ch);
    }
    cout << "Memory addresses:\n" << "  heap: " << p1
         << "   static: " << (void*) buffer << endl;
    cout << "Memory contents:\n";
    for (i = 0; i < N; i++)
    {
        cout << p1[i].dross << " at " << &p1[i].dross << endl;
        cout << p1[i].slag << " at " << &p1[i].slag << endl;

        cout << "                   " 
             << p2[i].dross << " at " << &p2[i].dross << endl;
        cout << "                    " 
             << p2[i].slag << " at " << &p2[i].slag << endl;        
    }
    delete [] p1;
    delete [] p3;
    return 0;
}

4.

//sale.h
#include <iostream>

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);
}
//sales.cpp
#include "sales.h"

namespace SALES
{
    using std::cout;
    using std::cin;

    void setSales(Sales &s, const double ar[], int n)
    {
    //计算和存储数组       
        double a, b, c;
        a = b = ar[0];
        c = 0.0;
        for(int i = 0; i < n; i++)
        {
            s.sales[i] = ar[i];
            c += ar[i];
            if(ar[i] < a)
                a = ar[i];
            if(ar[i] > b)
                b = ar[i];              
        }
        s.average = c / n;
        s.max = b;
        s.min = a;          
    }

    void setSales(Sales &s)
    {
    //输入数组 
        double ar[QUARTERS];
        cout << "Enter 4 numbers: " << std::endl;
        for(int i = 0; i < QUARTERS; i++)
        {
            cin >> ar[i];
        }

    //计算和存储数组       
        double a, b, c;
        a = b = ar[0];
        c = 0.0;
        for(int i = 0; i < QUARTERS; i++)
        {
            s.sales[i] = ar[i];
            c += ar[i];
            if(ar[i] < a)
                a = ar[i];
            if(ar[i] > b)
                b = ar[i];              
        }
        s.average = c / QUARTERS;
        s.max = b;
        s.min = a;          
    }

    void showSales(const Sales &s)
    {
    //显示结构数据
        for(int i = 0; i < QUARTERS; i++)
            cout << s.sales[i] << " ";          
        cout << "\nThe average is: "<< s.average << "  The max is: "
             << s.max << "  The min is: " << s.min << std::endl;
    }   
}
//main.cpp
#include <iostream>
#include "sales.h"

int main()
{
//声明两个sales 
    SALES::Sales s1;
    SALES::Sales s2;
//交互式
    int n;  
    std::cout << "Please enter n items:";
    std::cin >> n;//输入数据
    double a[n];
    std::cout << "Enter numbers: " << std::endl;
        for(int i = 0; i < n; i++)
        {
            std::cin >> a[i];
        }
    SALES::setSales(s1, a, n);
    SALES::showSales(s1);

//非交互式
    std::cout << std::endl;
    SALES::setSales(s2);
    SALES::showSales(s2);

    return 0;   
}

猜你喜欢

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