c++ primer plus第九章課後習題

37 篇文章 1 订阅

main.cpp

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

//hw1***
void hw_1()
{
    golf an1;
    setgolf(an1,"Ann Birdfree",24);
    showgolf(an1);
    golf andy[5];
    int count=0;
    while ( (count<5) && (setgolf(andy[count])))
    {
        count++;
    }
    for(int i=0;i<count;i++)
    {
        showgolf(andy[i]);
    }
    int change;
    for(int i=0;i<count;i++)
    {
        cout << "#" << (i + 1) << ":\n";
        cout << "Please enter the change:";
        cin >> change;
        handicap(andy[i], change);
        showgolf(andy[i]);
    }
}
//hw1***

//hw2***
void strcount(const string &str)
{
    static int total = 0;       //静态局部变量
    int count = 0;
    cout << "\"" << str << "\" contains ";
    while(str[count])//
    {
        count++;
    }
    total+=count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}
void hw_2()
{
    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);
    }
}
//hw2***

//hw3***
const int BUF = 512;
char buffer[BUF];
struct chaff
{
    char dross[20];
    int slag;
};
void get(chaff &str,char * ch)
{
    strcpy(str.dross, ch);
    cout << "Please enter the slag:";
    cin >> str.slag;
    cin.get();
}
void show(const chaff &str)
{
    static int count = 0;
    cout << "#" << ++count << ":\n";
    cout << "dross of the chaff is: " << str.dross << endl;
    cout << "slag of the chaff is: " << str.slag << endl;
}
void  hw_3()
{
    chaff *p1 = new (buffer) chaff[2];//buffer 爲 靜 態內存,不需要delete
    for(int i=0;i<2;i++)
    {
        static int count=0;
        cout << "#" << ++count << ":\n";
        cout << "Please enter the dross:";
        char ch[20];
        cin.getline(ch,20);
        get(p1[i],ch);
    }
    for (int i = 0; i < 2; i++)
        show(p1[i]);
}
//hw3***


//hw4***
const int ArSize = 4;
void hw_4()
{
    using SALES::Sales;//
    using SALES::setSales;
    using SALES::showSales;
    Sales s1,s2;
    double a[ArSize];
    cout<<"Please enter numbers: "<<endl;
    for(int i=0;i<ArSize;i++)
    {
        cin>>a[i];
    }
    setSales(s1,a,ArSize);
    showSales(s1);
    setSales(s2);
    showSales(s2);
}
//hw4***

int main()
{
    //hw_1();
    //hw_2();
    //hw_3();
    hw_4();
    return 0;
}

golf.h

//
// Created by pl on 2021/4/27.
//

#ifndef CH9_GOLF_H
#define CH9_GOLF_H

#endif //CH9_GOLF_H
#include <iostream>
#include <string>
#include<cstring>

using namespace std;
//golf.h--for pe9-1.cpp

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

//
// Created by pl on 2021/4/27.
//
#include "golf.h"
void setgolf(golf &g, const char *name, int hc)
{
    strcpy(g.fullname,name);
    g.handicap=hc;
}
int setgolf(golf & g)
{
    static  int i=0;//
    cout<<"#"<<++i<<":\n";
    cout<<"Please enter the name: ";
    cin.getline(g.fullname,Len);//
    int temp=1;
    if(strcmp(g.fullname," ")==0)
    {
        temp=0;
        return 0;
    }
    else
    {
        cout<<"Please enter the grade: ";
        cin>>g.handicap;
        cin.get();//这样是为了简单的暂停screen
        return temp;
    }

}
void handicap(golf &g, int hc)
{
    g.handicap=hc;
}
void showgolf(const golf &g)
{
    cout<<"The Name of golf is "<<g.fullname<<endl;
    cout<<"The Grade of golf if "<<g.handicap<<endl;
}

namesp.h

//
// Created by pl on 2021/4/27.
//

#ifndef CH9_NAMESP_H
#define CH9_NAMESP_H

#endif //CH9_NAMESP_H
#include <iostream>
using namespace std;
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);

}

namesp.cpp

//
// Created by pl on 2021/4/27.
//

#include "namesp.h"
namespace SALES
{
    void setSales(Sales &s, const double ar[], int n)
    {
        double sum=0.0;
        for(int i=0;i<n;i++)
        {
            s.sales[i]=ar[i];
            sum += s.sales[i];
        }
        s.average=sum/n;
        s.max=s.min=s.sales[0];
        for(int i=0;i<n;i++)
        {
            if(s.max<s.sales[i])
            {
                s.max=s.sales[i];
            }
            if(s.min>s.sales[i])
            {
                s.min=s.sales[i];
            }
        }
    }
    void setSales(Sales &s)
    {

        double sum = 0.0;
        for (int i = 0; i < QUARTERS; i++)
        {
            cout << "Please enter sales:";
            cin >> s.sales[i];
            sum += s.sales[i];
        }
        cin.get();
        s.average = sum/QUARTERS;
        s.max = s.min = s.sales[0];
        for (int i = 0; i < QUARTERS; i++)
        {
            if (s.max < s.sales[i])
                s.max = s.sales[i];
            if (s.min > s.sales[i])
                s.min = s.sales[i];
        }

    }
    void showSales(const Sales & s)
    {
        for(int i=0;i<QUARTERS;i++)
        {
            cout<<s.sales[i]<<" "<<endl;
            cout<<"The average is: "<<s.average<<endl;
            cout <<"The max is: "<< s.max << endl;
            cout <<"The min is: "<< s.min << endl;
        }
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值