C++ Primer Plus第六版 編程練習第九章

1.

golf.h

//golf.h
#include <iostream>

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

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

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

int setgolf(golf& g)
{
    std::cout << "輸入姓名: ";
    std::cin.getline(g.fullname, Len);
    if (g.fullname[0])
    {
        std::cout << "輸入等級: ";
        std::cin >> g.handicap;
        std::cin.get();
        return 1;
    }
    else
        return 0;
}

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

void showgolf(const golf& g)
{
    std::cout << "姓名: " << g.fullname << std::endl;
    std::cout << "等級: " << g.handicap << std::endl;
}

main.cpp

//main.cpp
#include "golf.h"

const int N = 5;

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

    golf gs[N];
    int i;
    int result;

    for (i = 0; i < N; ++i)
    {
        cout << "golf[" << i << "]" << endl;
        result = setgolf(gs[i]);
        if (!result)
        {
            cout << "名字爲空,再見!";
            break;
        }
    }

    int nout = i;
    for (i = 0; i < nout; ++i)
    {
        cout << "golf[" << i << "]" << endl;
        showgolf(gs[i]);
    }

    return 0;
}

2.

#include <iostream>
#include <string>

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): " << endl;
        getline(cin, input);
    }
    cout << "Bye" << endl;

    return 0;
}

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

    cout << "\"" << str << "\" contains " << endl;
    cout << str.size() << "characters" << endl;
    total += str.size();
    cout << total << " characters total" << endl;
}

3.

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

const int N = 2;

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

chaff buffer1[N];

int main()
{
    using namespace std;

    chaff arr[N] = {
        {"LOL", 24},
        {"PUBG", 23},
    };
    chaff* buffer2 = new (buffer1) chaff[N];

    strcpy_s(buffer2[0].dross, arr[0].dross);
    buffer2[0].slag = arr[0].slag;
    strcpy_s(buffer2[1].dross, arr[1].dross);
    buffer2[1].slag = arr[1].slag;
    cout << "Buffer at "<< &buffer1<<endl;
    for (int i = 0; i < N; ++i)
    {
        cout << "chaff[" << i << "] at " << &buffer2[i] <<endl;
        cout << "dross: " << buffer2[i].dross << endl;
        cout << "slag: " << buffer2[i].slag << endl;
    }
    return 0;
}

4.

sales.h

//sales.h
#include <iostream>

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

//sales.cpp
#include "sales.h"

namespace SALES
{
    //非交互
    void setSales(Sales& s, const double ar[], int n)
    {
        for (int i = 0; i < QUARTERS; ++i)
        {
            if (i < n)
                s.sales[i] = ar[i];
            else
                s.sales[i] = 0.0;
        }

        double sum = 0.0;
        double max = s.sales[0], min = s.sales[0];
        for (int i = 0; i < QUARTERS; ++i)
        {
            double cur = s.sales[i];
            if (cur > max)
                max = cur;
            if (cur < min)
                min = cur;
            sum += cur;
        }
        s.average = sum / (float)QUARTERS;
        s.max = max;
        s.min = min;
    }

    //交互
    void setSales(Sales& s)
    {
        using std::cin;
        using std::cout;
        using std::endl;

        cout << "Enter sales:" << endl;
        for (int i = 0; i < QUARTERS; ++i)
        {
            cout << "sales[" << i << "]: ";
            cin >> s.sales[i];
        }
        double sum = 0.0;
        double max = s.sales[0], min = s.sales[0];
        for (int i = 0; i < QUARTERS; ++i)
        {
            double cur = s.sales[i];
            if (cur > max)
                max = cur;
            if (cur < min)
                min = cur;
            sum += cur;
        }
        s.average = sum / (float)QUARTERS;
        s.max = max;
        s.min = min;
    }

    void showSales(const Sales& s)
    {
        using std::cout;
        using std::endl;

        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;
    }
}

main.cpp

//main.cpp
#include "sales.h"

int main()
{
    using namespace SALES;

    Sales s1, s2;
    setSales(s1);
    double ar[3] = { 3.0, 4.0, 1.0 };
    setSales(s2, ar, 3);
    showSales(s1);
    showSales(s2);

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章