C++Primer Plus习题记录-Chapter9

9-1

#pragma once
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);

#include "pch.h"
#include <iostream>
#include "golf.h"
using namespace std;
void setgolf(golf &g, const char *name, int hc) {
    int i;
    g.handicap = hc;
    for (i = 0;name[i]; i++) {
        g.fullname[i] = name[i];
    }    
    while (i<Len)
        g.fullname[i++] = '\0';
}
int setgolf(golf &g) {
    char* name;
    if (cin) {

        cin.get(g.fullname, Len);
        cin.get();
        cin >> g.handicap;
        return 1;
    }
    else
        return 0;
}
void handicap(golf &g, int hc) {
    g.handicap = hc;
}
void showgolf(const golf &g) {
    cout << g.fullname << ": " << g.handicap;
}


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

int main(void) {
    golf gfs;
    setgolf(gfs,"Light monster",100);
    showgolf(gfs);
    return 0;
}

9-2

using namespace std;
void strcount(const string str);
int main(void) {
    using namespace std;
    string input;
    char next;
    cout << "Enter a line:\n";
    getline(cin, input);
    while (input!="q")
    {
        if (input == "")
            cout << "blank line" << endl;
        strcount(input);
        getline(cin, input);
    }
    cout << "Bye!\n";
    return 0;
}
void strcount(const string str) {
    using namespace std;
    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";
}

9-3

using namespace std;
struct chaff
{
    char dross[20];
    int slag;
};
char buffer[100];
int main(void) {
    chaff* cf1 = new (buffer) chaff[2];
    chaff* cf2 = new (buffer + 2 * sizeof(chaff)) chaff[2];
    char cop[20] = { 0 };
    int slag = 0;
    cout << sizeof(chaff) << endl;
    for (int i = 0; i < 2; i++) {
        cin.getline(cop,20);
        cin >> slag;
        cin. get();
        strcpy_s(cf1[i].dross, cop);
        strcpy_s(cf2[i].dross, cop);
        cf1[i].slag = cf2[i].slag = slag;
    }
    for (int i = 0; i < 2; i++) {
        cout << "staff #" << (i + 1) << ":" << endl;
        cout << "pcha.dross: " << cf1[i].dross << ". pcha.slag: " << cf1[i].slag << &cf1[i] << endl;
        cout << "pcha2.dross: " << cf2[i].dross << ". pcha2.slag: " << cf2[i].slag << &cf2[i] << endl;
    }

    return 0;
}

9-4

#pragma once
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);
}


#include "pch.h"
#include "sales.h"
#include <iostream>
using namespace std;
namespace SALES {
    void setSales(Sales &s, const double ar[], int n) {
        s.max = s.min = ar[0];
        double sum = 0;
        for (int i = 0; i < n; i++) {
            s.sales[i] = ar[i];
            sum += ar[i];
            if (ar[i] > s.max)
                s.max = ar[i];
            if (ar[i] < s.min)
                s.min = ar[i];
        }
        s.average = sum / n;
    }
    void setSales(Sales &s) {
        for (int i = 0; i < 4; i++) {
            cin >> s.sales[i];
        }
        double sum = s.sales[0];
        s.max = s.min = s.sales[0];
        for (int i = 1; i < 4; i++) {
            sum += s.sales[i];
            if (s.sales[i] > s.max)
                s.max = s.sales[i];
            if (s.sales[i] < s.min)
                s.min = s.sales[i];
        }
        s.average = sum / 4;
    }
    void showSales(const Sales &s) {
        for (int i = 0; i < 4; i++) {
            cout << s.sales[i] << " ";
        }
        cout << "\nmin: " << s.min << " max: " << s.max << " average: " << s.average;
    }
}

#include "sales.h"
int main(void) {
    using namespace SALES;
    Sales sale1,sale2;
    double ar[4] = { 1,2,3,4 };
    setSales(sale1, ar, 4);
    showSales(sale1);
    setSales(sale2);
    showSales(sale2);
    return 0;
}

 

posted @ 2019-01-29 14:05  Lightmonster  阅读(193)  评论(0编辑  收藏  举报