C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第九章(chapter 9) 1-4

重拾c++,不知道自己为什么莫名奇妙好久没学,好吧,现在只能抓紧时间慢慢拾起来了,希望这学期之后具有初步的编程能力。现在已经都忘了,时间也不容许自己再浪费了,马上就要毕业了。。。好怕以后。

9.1

//golf.h---for pe9-1.cpp
#ifndef GOLF_H_
#define GOLF_H_

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

// non-interactive version
// function sets golf structure to provide name, handicap
// using values passed as argumens to the function

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

int setgolf( golf & g);


//function resets handicap to new valude

void handicap(golf & g, int hc);

//function displays contents of golf structure

void showgolf(const golf & g);

#endif
//golf.cpp
//compile with golf.h

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

using std::endl;

extern const int Len;
// non-interactive version
// function sets golf structure to provide name ,handicap
// using values passed as argumens to the function

void setgolf(golf & g, const char * name, int hc)
{
    std::cout<< "Provided name & handicap entered." << endl;
    std:strcpy(g.fullname,name);//invalid conversation from const char* to char ,must use strcpy
    g.handicap = 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

int setgolf( golf & g)
{
    std::cout << "Enter name" << endl;
    std::cin.getline(g.fullname, Len);
    if('\0' == g.fullname[0])
    {
        std::cout <<"empty full name! Input abortion!" << endl;
        return 0;
    }
    std::cout << "Please enter handicap"<<endl;
    while(!(std::cin>> g.handicap))
    {
        std::cin.clear();
        std::cout<< "Please enter an integer."<< endl;
    }

    while(std::cin.get()!= '\n')  //erase error."enter of the \n"
        continue;
    return 1;   


}


//function resets handicap to new valude

void handicap(golf & g, int hc)
{
    std::cout << "reset handicap." << endl;
    g.handicap = hc;

}

//function displays contents of golf structure

void showgolf(const golf & g)
{
    std::cout << "name:  " << g.fullname
              << "  handicap: "<< g.handicap<< endl;

}
//golfmain.cpp
//compile with golf.cpp and golf.h
#include <iostream>
#include "golf.h"

const int Mem = 5;

int main()
{
    using std::endl;
    golf golfinfo[Mem];
    setgolf(golfinfo[0], "The first one", 10);
    showgolf(golfinfo[0]);
    setgolf(golfinfo[1], "The second one", 15);
    showgolf(golfinfo[1]);

    int i = 2;
    int flag;
    while(i < Mem)
    {
        flag = setgolf(golfinfo[i]);
        if(flag == 0)
            break;
        i++;
    }


    int handi;
    std::cout << "input && reset handicap" << endl;
    std::cin>> handi;
    handicap(golfinfo[1],handi);

    for( int j = 0; j< i; j++)
    {
        showgolf(golfinfo[j]);
    }

    std::cin.get();
    std::cin.get();
    return 0;

}

9.2

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

int main()
{

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

    cin.get();
    return 0;

}

void strcount(const string str)
{
    using namespace std;
    static int total = 0;
    int count = 0;

    cout << "\"" << str << "\" contains ";
    count = str.size();

    total +=count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

9.3

#include <iostream>
#include <new> 

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

char buffer[50];


int main()
{
    using std::cout;
    using std::endl;

    chaff * pt1, * pt2; 

    // regular form of new
    pt1 = new chaff[2];
    // forms of placement new
    pt2 = new (buffer) chaff[2];

    // assignment
    strcpy(pt1[0].dross, "The first one");
    pt1[0].slag = 1;


    strcpy(pt1[1].dross, "The second one");
    pt1[1].slag = 2;

    strcpy(pt2[0].dross, "The third one");
    pt2[0].slag = 3;


    strcpy(pt2[1].dross, "The forth one");;
    pt2[1].slag = 4;


    for(int i = 0; i < 2; i++)
    {
        cout << "chaff: " << pt1->dross << " slag: " << pt1->slag << endl;
        pt1++;

    }

    for(int j = 0; j < 2; j++)
    {
        cout << "chaff: " << pt2->dross << " slag: " << pt2->slag << endl;
        pt2++;

    }


    std::cin.get();

    return 0;
}

9.4

// sales.h
// namespace
#include <iostream>

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,maxium, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0;

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

// gathers sales member of s and computes and stores the 
// average, maximum , and minimum values

    void setSales(Sales & s);

//display all informations in structure s
    void showSales(const Sales & s);

}
//sales.cpp
//compile with sales.h
#include <iostream>
#include "sales.h"


extern const int QUARTERS;

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

//return the average    
    double average(const double ar[], int n)
    {
        double sum = 0;
        int i = 0;

        while(i < n)
            sum += ar[i++];

        return sum/n;
    }
//return the maximum    
    double maximum(const double ar[], int n)
    {
        double max = 0;
        max = ar[0];
        while(--n > 0)
            max = max > ar[n] ? max : ar[n];

        return max; 
    }

//return the minimum    
    double minimum(const double ar[], int n)
    {
        double min = 0;
        min = ar[0];
        while(--n > 0)
            min = min < ar[n] ? min : ar[n];

        return 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,maxium, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0;

    void  setSales(Sales & s, const double ar[], int n)
    {
        int num = QUARTERS > n ? n : QUARTERS;

        for (int i = 0; i < num; i++)
        {
            s.sales[i] = ar[i];
        }
        for (int i = num; i < QUARTERS; i++)
        {
            s.sales[num] = 0;
        }

        s.average= average(ar,num);
        s.max = maximum(ar,num);
        s.min = minimum(ar,num);

    }

// gathers sales member of s and computes and stores the 
// average, maximum , and minimum values

    void setSales(Sales & s)
    {
        cout << "Enter "<< QUARTERS << " sales data" << endl;
        for(int i = 0; i < QUARTERS; i++)
        {
            cout << "#" << i+1 <<":";
            cin >> s.sales[i];
        }

        s.average= average(s.sales,QUARTERS);
        s.max = maximum(s.sales,QUARTERS);
        s.min = minimum(s.sales,QUARTERS);

    }

//display all informations in structure s
    void showSales(const Sales & s)
    {
        cout << "Sales: ";
        for (int i = 0; i < QUARTERS; i++)
        {
            cout << s.sales[i] << "  ";
        }
        cout << endl;

        cout << "Average: " << s.average << endl;
        cout << "Max: " << s.max << endl;
        cout << "Min: " << s.min << endl;

    }

}

//use sales.h
//compiel with sales.h and sales.cpp
#include <iostream>
#include "sales.h"

const int ArSize = 4;

int main()
{
    SALES::Sales first;
    SALES::Sales second;

    double dat[ArSize] = {20.0, 1, 20, 4};

    setSales(first, dat, ArSize);

    showSales(first);

    setSales(second);

    showSales(second);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值