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

1.

golf.h 头文件

// golf.h -- for pe9.1

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 "golf.h"
#include <iostream>
#include <cstring>
using namespace std;

//由题目提示,需要输入名称和handicap的值,直接将这两个输入参数赋给golf结构的相应变量
//name是char数组,所以需strcpy()函数来赋值,所以需要<cstring>头文件
void setgolf(golf & g, const char * name, int hc){
    strcpy(g.fullname, name);
    g.handicap = hc;
}
//提示用户输入姓名和handicap值,赋给相应变量
int setgolf(golf & g){
    cout << "Enter fullname of golf plyer: ";
    cin.getline(g.fullname, Len);//char数组输入
    if (strcmp(g.fullname, "") == 0)
        return 0;
    cout << "Enter handicap of golf plyer: ";
    cin >> g.handicap;
    cin.get();//必须有,否则出错
    return 1;
}
//对handicap赋值
void handicap(golf & g, int hc){
    g.handicap = hc;
}
//输出内容
void showgolf(const golf & g){
    cout << "Name: " << g.fullname << endl;
    cout << "Handicap: " << g.handicap << endl;
}

play.cpp 测试执行文件

#include <iostream>
#include "golf.h"
using namespace std;

int main(){
    golf a;
    setgolf(a, "Li Ming", 10);
    showgolf(a);

    golf b[3];
    int i = 0;
    while ((i<3) && setgolf(b[i])) //当数组填满或输入的姓名为空,循环结束
    {
        showgolf(b[i]);
        cout << "Next golf: \n";
        i++;
    }

    golf c;
    setgolf(c, "Mao Mao", 50);
    handicap(c, 60);
    showgolf(c);

    return 0;
}

2.暂

3.暂

4. 

func.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);
}

func.cpp

#include <iostream>
#include "func.h"
using namespace std;

namespace SALES
{
    void setSales(Sales & s, const double ar[], int n)
    {
        //赋值
        //根据题目,n小于4,则多余的4-n个置0
        if(n < 4)
        {
            for(int i=0; i<n; i++)
            {
                s.sales[i] = ar[i];
            }
            for(int j=n; j<4; j++)
            {
                s.sales[j] = 0;
            }
        }
        else
        {
            for(int i=0; i<4; i++)
            {
                s.sales[i] = ar[i];
            }
        }
        //求均值
        s.average = (s.sales[0] + s.sales[1] + s.sales[2] + s.sales[3]) / 4;
        //求最值
        double max = 0;
        double min = 100000000;
        for(int i=0; i<4; i++)
        {
            if(s.sales[i] > max)
            {
                max = s.sales[i];
            }
            if(s.sales[i] < min)
            {
                min = s.sales[i];
            }
        }
        s.max = max;
        s.min = min;
    }

    void setSales(Sales & s)
    {
        cout << "Enter 4 quarters: \n";
        for(int i=0; i<4; i++)
        {
            cout << "The #" << i+1 << "quarter is: ";
            cin >> s.sales[i];
        }

        s.average = (s.sales[0] + s.sales[1] + s.sales[2] + s.sales[3]) / 4;

        double max = 0;
        double min = 100000000;
        for(int i=0; i<4; i++)
        {
            if(s.sales[i] > max)
            {
                max = s.sales[i];
            }
            if(s.sales[i] < min)
            {
                min = s.sales[i];
            }
        }
        s.max = max;
        s.min = min;
    }

    void showSales(const Sales &s)
    {
        cout << "The 4 quarters are $" << s.sales[0] << ", $" << s.sales[1] << ", $" << s.sales[2] << ", $" << s.sales[3] << endl;
        cout << "The average income is: $" << s.average <<endl;
        cout << "The maximum income is: $" << s.max <<endl;
        cout << "The minimum income is: $" << s.min <<endl;
    }
}

sales.cpp

#include "func.h"
#include <iostream>
using namespace std;
using namespace SALES;

int main()
{
    Sales s1;
    cout << "The 1st sale's information: \n";
    setSales(s1);
    showSales(s1);
    cout << endl << endl;

    Sales s2;
    cout << "The 2nd sale's information: \n";
    double ar[2] = {1.1, 2.2};
    setSales(s2, ar, 2);
    showSales(s2);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值