C++ Primer Plus 第六版第九章编程练习答案

//question1

// golf.h
#ifndef GOLF_H
#define 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);
#endif

 

// golf.cpp
#include <iostream>
#include "golf.h"
#include <cstring>

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

int setgolf(golf & g)
{
    std::cout << "Please enter the name: ";
    std::cin.getline(g.fullname, Len);
    
    if (!strlen(g.fullname))
        return 0;
    else
    {
        std::cout << "Please enter the handicap: ";
        std::cin >> g.handicap;
        std::cin.get();
        return 1;
    }
}

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

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

 

// main.cpp
#include <iostream>
#include "golf.h"

int main()
{
    /*
    golf ann;
    setgolf(ann, "Ann Birdfree", 24);
    showgolf(ann);
    golf andy;
    setgolf(andy);
    showgolf(andy);
    */
    const int ArrSize = 5;
    golf a[5];
    int count = 0;
    for (int i = 0; i < 5 && setgolf(a[i]); i++, count++)
        ;
    for (int j = 0; j < count; j++)
        showgolf(a[j]);
    return 0;
}

 

//question2

#include <iostream>
#include <cstring>
#include <string>

const int ArSize = 10;

void strcount(const std::string * str);

int main()
{
    using namespace std;
    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 std::string * str)
{
    using namespace std;
    static int total = 0;
    
    cout << "\"" << *str << "\" contains ";
    int count = str->length();
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n"; 
}

 

//question3

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

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

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

int main()
{
    chaff * pd1;
    //pd1 = new chaff[N];
    chaff * pd2;
    pd2 = new (buffer) chaff[2];
    char str[20];
    for (int i = 0; i < N; i++)
    {
        std::cout << "Dross: ";
        std::cin >> str;
        strcpy(pd2[i].dross, str);
        std::cout << "Slag: ";
        std::cin >> pd2[i].slag;
    }
    for (int j = 0; j < N; j++)
    {
        std::cout << "Dross is " << pd2[j].dross << ", slag is " << pd2[j].slag << std::endl;
    }
    //delete [] pd1;
    return 0;
}

 

//question4

// sale.h
#ifndef SALE_H
#define SALE_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);
}

#endif

 

// sale.cpp
#include <iostream>
#include "sale.h"

void SALES::setSales(Sales & s, const double ar[], int n)
{
    double sum = 0;
    double max = ar[0];
    double min = ar[0];
    if (n <= QUARTERS)
    {
        for (int i = 0; i < n; i++)
        {
            s.sales[i] = ar[i];
            sum += ar[i];
            if (max < ar[i])
                max = ar[i];
            if (min > ar[i])
                min  = ar[i];
        }
        s.average = sum / n;
        s.max = max;
        s.min = min;
    }
    else 
        std::cout << "Wrong n!" << std::endl;
    if (n < QUARTERS)
    {
        for (int j = n; j < QUARTERS; j++)
        {
            s.sales[j] = 0;
        }
    }
}

void SALES::setSales(Sales & s)
{
    double sum = 0;
    std::cout << "Enter the nums: " << std::endl;
    for (int i = 0; i < QUARTERS; i++)
    {
        std::cin >> s.sales[i];
    }
    double max = s.sales[0];
    double min = s.sales[0];
    for (int j = 0; j < QUARTERS; j++)
    {
        sum += s.sales[j];
        if (max < s.sales[j])
            max = s.sales[j];
        if (min > s.sales[j])
            min = s.sales[j];
    }
    s.average = sum / QUARTERS;
    s.max = max;
    s.min = min;
}

void SALES::showSales(const Sales & s)
{
    std::cout << "Sales: " << std::endl;
    for (int i = 0; i < QUARTERS; i++)
    {
        std::cout << s.sales[i] << " ";
    }
    std::cout << "\n";
    std::cout << "Average: " << s.average << std::endl;
    std::cout << "Max: " << s.max << std::endl;
    std::cout << "Min: " << s.min << std::endl;
}

 

// main.cpp
#include <iostream>
#include "sale.h"

int main()
{
    using namespace SALES;
    Sales a, b;
    double arr[3] = {1.2, 3.2, 4.5};
    
    setSales(a, arr, 3);
    showSales(a);
    setSales(b);
    showSales(b);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值