2020-12-23 C++ Primer Plus 第九章 内存模型和名称空间 编程练习

ch09.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 
#include "golf.h"
#include 
using namespace std;
void setgolf(golf &g ,const char * name,int hc)
{
    strcpy(g.fullname,name);
    g.handicap=hc;
}

int setgolf(golf &g)
{
    cout << "Please enter fullname: ";
    cin.getline(g.fullname,40);
    cout << "Please enter handicap: ";
    cin >> g.handicap;
}
void handicap(golf &g,int hc)
{
    g.handicap=hc;
}

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

main.cpp

#include 
#include "golf.h"
using namespace std;
int main(){
    golf g1;
    setgolf(g1,"Mike",6);
    showgolf(g1);

    setgolf(g1);
    showgolf(g1);

    cout << "modified hc "<

ch09.2

#include 
#include 
using namespace std;
void strcount(string str);

int main()
{
    string st1;

    cout << "Enter a line:\n";
    getline(cin,st1);
    while (st1 != "")
    {
        strcount(st1);
        cout << "Enter next line(empty line to quit):\n";
        getline(cin,st1);
    }
}
void strcount(string str)
{
    static int total = 0;
    cout << "\"" << str << "\" contains ";
    cout << str.size() << " characters\n";
    total+=str.size();
    cout << total << " characters\n";
}

ch09.3

#include 
#include 
#include 

using namespace std;
struct chaff
{
    char dross[20];
    int slag;
};
char buffer[2];
void showvalue(chaff &pd);

int main() {
    chaff arr[2] = {
            {"what", 10},
            {"How",  20},
    };

    chaff *pd = new(buffer) chaff[2];

    strcpy(pd[0].dross, arr[0].dross);
    pd[0].slag = arr[0].slag;
    showvalue(*pd);

    strcpy(pd[1].dross, arr[1].dross);
    pd[1].slag = arr[1].slag;
    showvalue(*(pd+1));


}
void showvalue(chaff &pd)
{
    cout << "dross= " << pd.dross << endl;
    cout << "slag= " << pd.slag << endl;
}

ch09.4
sales.h

#include 
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"
#include 
namespace SALES {
    void setSales(Sales &s, const double ar[], int n) {
        double sum = 0;
        for (int i = 0; i < QUARTERS; i++) {
            if(i max)
                max = s.sales[i];
            if (s.sales[i] < min)
                min = s.sales[i];
        }
        s.min = min;
        s.max = max;
        s.average = sum / n;


    }

    void setSales(Sales &s) {
        using namespace std;
        double sum = 0;
        cout << "Please enter 4 numbers"<< endl;
        for (int i = 0; i < QUARTERS; i++) {
            cin >> s.sales[i];
            sum += s.sales[i];
        }
        double min = s.sales[0], max = s.sales[0];
        for (int i = 1; i < QUARTERS; i++) {
            if (s.sales[i] > max)
                max = s.sales[i];
            if (s.sales[i] < min)
                min = s.sales[i];
        }
        s.min = min;
        s.max = max;
        s.average = sum / QUARTERS;
    }

    void showSales(const Sales &s) {
        using namespace std;
        for (int i = 0; i < QUARTERS; i++)
            cout << " s.sale= " << s.sales[i] << " ";
        cout << endl;
        cout << " min= " << s.min << " max= " << s.max << " s.avg= " << s.average << endl;
    }
}

main.cpp

#include "sales.h"

int main()
{
    using namespace SALES;
    Sales s1,s2;
    double arr[4]={1,2,3};
    setSales(s1,arr,3);
    showSales(s1);


    setSales(s2);
    showSales(s2);
}

你可能感兴趣的:(2020-12-23 C++ Primer Plus 第九章 内存模型和名称空间 编程练习)