第十四章 编程练习(1)

编程练习1

//winec.h
#ifndef WINEC_H_
#define WINEC_H_

#include <iostream>
#include <string>
#include <valarray>
#include <utility>

using namespace std;
typedef valarray<int> ArrayInt;
typedef pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
    string name;
    PairArray info;
public:
    Wine() {}
    Wine(const string str, int y, const int yr[], const int bot[]);
    Wine(const string str, int y);
    void GetBottles();
    const string &Label() const;
    int sum() const;
    void show() const;
};
#endif
//winec.cpp
#include "winec.h"

Wine::Wine(const string str, int y, const int yr[], const int bot[])
{
    name = str;
    ArrayInt a(y), b(y);
    for(int i = 0; i< y; i++)
    {
        a[i] = yr[i];
        b[i] = bot[i];
    }
    info = make_pair(a,b);
}
Wine::Wine(const string str, int y)
{
    name = str;
    ArrayInt a(y), b(y);
    info = make_pair(a,b);
}
void Wine::GetBottles()
{
    ArrayInt a(info.first.size());
    ArrayInt b(info.first.size());
    cout << "Enter " << name << " for " << info.first.size() << " year(s):" << std::endl;
    for (int i = 0; i < (int)info.first.size(); ++i)
    {
        cout << "Enter year: ";
        cin >> a[i];

        cout << "Enter bottles for that year: ";
        cin >> b[i];
    }
    info = make_pair(a, b);
}
const string & Wine::Label() const
{
    return name;
}
int Wine::sum() const
{
    int num = 0;
    for (int i = 0; i < (int)info.first.size(); ++i)
        num += info.second[i];
    return num;
}
void Wine::show() const
{
    cout << "Wine: " << name << endl;
    cout << "\t\t" << "Year" << "\t" << "Bottles" << endl;
    for (int i = 0; i < (int)info.first.size(); ++i)
        cout << "\t\t" << info.first[i] << "\t" << info.second[i] << endl;
}
#include <iostream>
#include "winec.h"

//main.cpp
int main()
{
    cout << "Enter name of wine: ";
    char lab[50];
    cin.getline(lab, 50);
    cout << "Enter number of years: ";
    int yrs;
    cin >> yrs;

    Wine holding(lab, yrs);
    holding.GetBottles();
    holding.show();

    const int YRS = 3;
    int y[YRS] = { 1993, 1995, 1998 };
    int b[YRS] = { 48, 60, 72 };

    Wine more("Gushing Grape Red", YRS, y, b);
    more.show();
    cout << "Total bottles for " << more.Label() << ": " << more.sum() << endl;
    cout << "Bye\n";

    system("pause");
    return 0;
}

编程练习2

//winec.h
#ifndef WINEC_H_
#define WINEC_H_

#include <iostream>
#include <string>
#include <valarray>
#include <utility>

using namespace std;
typedef valarray<int> ArrayInt;
typedef pair<ArrayInt, ArrayInt> PairArray;
class Wine : private string, private PairArray
{
public:
    Wine() {}
    Wine(const char *l, int y, const int yr[], const int bot[]);
    Wine(const char *l, int y);
    void GetBottles();
    const string &Label() const;
    int sum() const;
    void show() const;
};
#endif
//winec.cpp
#include "winec.h"

Wine::Wine(const char *l, int y, const int yr[], const int bot[]):string(l),PairArray(ArrayInt(yr,y),ArrayInt(bot,y))
{
}
Wine::Wine(const char *l, int y):string(l),PairArray(ArrayInt(y),ArrayInt(y))
{   
}
void Wine::GetBottles()
{
    ArrayInt a(PairArray::first.size());
    ArrayInt b(PairArray::first.size());
    cout << "Enter " << (const string &)*this << " for " << PairArray::first.size() << " year(s):" << std::endl;
    for (int i = 0; i < (int)PairArray::first.size(); ++i)
    {
        cout << "Enter year: ";
        cin >> a[i];

        cout << "Enter bottles for that year: ";
        cin >> b[i];
    }
    (PairArray &) *this = make_pair(a, b);
}
const string & Wine::Label() const
{
    return (const string &)*this;
}
int Wine::sum() const
{
    int num = 0;
    for (int i = 0; i < (int)PairArray::first.size(); ++i)
        num += PairArray::second[i];
    return num;
}
void Wine::show() const
{
    cout << "Wine: " << (const string &)*this << endl;
    cout << "\t\t" << "Year" << "\t" << "Bottles" << endl;
    for (int i = 0; i < (int)PairArray::first.size(); ++i)
        cout << "\t\t" << PairArray::first[i] << "\t" << PairArray::second[i] << endl;
}

main和上面一样啦,这就不重复了

编程练习3

//queuetp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_

template <class T>
class QueueTp
{
private:
    enum {LEN = 10};
    T *data;
    int top;
public:
    QueueTp() { top = 0; data = new T[LEN]; }
    QueueTp(int len) { top = 0; data = new T[2 * len]; }
    ~QueueTp() { delete[] data; }
    bool Push(T item);
    bool Pop();
    T & front() const;
    T & rear() const;
    bool isFull() const { return top == LEN; }
    bool isEmpty() const { return top == 0; }
};

template <class T>
bool QueueTp<T>::Push(T item)
{
    if (isFull())
        return false;

    for (int i = top; i > 0; --i)
        data[i] = data[i-1];
    data[0] = item;
    ++top;
    return true;
}

template <class T>
bool QueueTp<T>::Pop()
{
    if (isEmpty())
        return false;

    --top;
    return true;
}

template <class T>
T & QueueTp<T>::front() const
{
    return data[top-1];
}

template <class T>
T & QueueTp<T>::rear() const
{
    return data[0];
}

#endif
//Workers.h
#ifndef WORKERS_H_
#define WORKERS_H_

#include <string>

class Worker
{
private:
    std::string fullname;
    long id;
protected:
    virtual void Data() const;
    virtual void Get();
public:
    Worker() : fullname("no one"), id(0L) {}
    Worker(const std::string &s, long n) :fullname(s), id(n) {}
    virtual ~Worker() = 0;
    virtual void Set() = 0;
    virtual void Show() const = 0;
};

class Waiter : virtual public Worker
{
private:
    int panache;
protected:
    void Data() const;
    void Get();
public:
    Waiter() : Worker(), panache(0) {}
    Waiter(const std::string &s, long n, int p = 0) : Worker(s, n), panache(p) {}
    Waiter(const Worker &wk, int p = 0) : Worker(wk), panache(p) {}
    void Set();
    void Show() const;
};

class Singer : virtual public Worker
{
protected:
    enum { other, alto, contralto, soprano, bass, baritone, tenor };
    enum { Vtypes = 7 };
    void Data() const;
    void Get();
private:
    static char *pv[Vtypes];
    int voice;
public:
    Singer() : Worker(), voice(other) {}
    Singer(const std::string &s, long n, int v = other) : Worker(s, n), voice(v) {}
    Singer(const Worker &wk, int v = other) : Worker(wk), voice(v) {}
    void Set();
    void Show() const;
};

class SingingWaiter : public Singer, public Waiter
{
protected:
    void Data() const;
    void Get();
public:
    SingingWaiter() {}
    SingingWaiter(const std::string &s, long n, int p = 0, int v = other) : Worker(s, n), Waiter(s, n, p), Singer(s, n, v) {}
    SingingWaiter(const Worker &wk, int p = 0, int v = other) : Worker(wk), Waiter(wk, p), Singer(wk, v) {}
    SingingWaiter(const Waiter &wt, int v = other) : Worker(wt), Waiter(wt), Singer(wt, v) {}
    SingingWaiter(const Singer &wt, int p = 0) : Worker(wt), Waiter(wt, p), Singer(wt) {}
    void Set();
    void Show() const;
};

#endif
//Workers.cpp
#include "Workers.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

Worker::~Worker() {}

void Worker::Data() const
{
    cout << "Name: " << fullname << endl;
    cout << "Emplyee ID: " << id << endl;
}

void Worker::Get()
{
    getline(cin, fullname);
    cout << "Enter worker's ID: ";
    cin >> id;
    while (cin.get() != '\n')
        continue;
}

void Waiter::Set()
{
    cout << "Enter waiter's name: ";
    Worker::Get();
    Get();
}

void Waiter::Show() const
{
    cout << "Category: waiter\n";
    Worker::Data();
    Data();
}

void Waiter::Data() const
{
    cout << "Panache rating: " << panache << endl;
}

void Waiter::Get()
{
    cout << "Enter waiter's panache rating: ";
    cin >> panache;
    while (cin.get() != '\n')
        continue;
}

char *Singer::pv[Singer::Vtypes] = { "other", "alto", "contralto", "soprano", "bass", "baritone", "tenor" };

void Singer::Set()
{
    cout << "Enter singer's name: ";
    Worker::Get();
    Get();
}

void Singer::Show() const
{
    cout << "Category: singer\n";
    Worker::Data();
    Data();
}

void Singer::Data() const
{
    cout << "Vocal range: " << pv[voice] << endl;
}

void Singer::Get()
{
    cout << "Enter number for singer's vocal range:\n";
    int i;
    for (i = 0; i < Vtypes; ++i)
    {
        cout << i << ": " << pv[i] << "   ";
        if (i % 4 == 3)
            cout << endl;
    }
    if (i % 4 != 0)
        cout << '\n';
    cin >> voice;
    while (cin.get() != '\n')
        continue;
}

void SingingWaiter :: Data() const
{
    Singer::Data();
    Waiter::Data();
}

void SingingWaiter::Get()
{
    Waiter::Get();
    Singer::Get();
}

void SingingWaiter::Set()
{
    cout << "Enter singing waiter's name: ";
    Worker::Get();
    Get();
}

void SingingWaiter::Show() const
{
    cout << "Category: singing waiter\n";
    Worker::Data();
    Data();
}
//main.cpp
#include <iostream>
#include <cstring>
#include "Workers.h"
#include "QueueTp.h"
const int SIZE = 5;

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::strchr;
    QueueTp<Worker *> lolas(SIZE);

    int ct;
    for (ct = 0; ct < SIZE; ++ct)
    {
        char choice;
        cout << "Enter the employee category: \n"
            << "w: waiter s: singer "
            << "t: singing waiter q: quiter\n";
        cin >> choice;
        while (strchr("wstq", choice) == NULL)
        {
            cout << "Please enter a w, s, t, or q: ";
            cin >> choice;
        }
        if (choice == 'q')
            break;
        switch (choice)
        {
        case 'w': lolas.Push(new Waiter);
            break;
        case 's': lolas.Push(new Singer);
            break;
        case 't': lolas.Push(new SingingWaiter);
            break;
        }
        cin.get();
        lolas.rear()->Set();
    }

    cout << "\nHere is your staff:\n";
    int i;
    for (i = 0; i < ct; ++i)
    {
        cout << endl;
        lolas.front()->Show();
        lolas.Push(lolas.front());
        lolas.Pop();
    }

    for (i = 0; i < ct; ++i)
    {
        delete lolas.front();
        lolas.Pop();
    }
    cout << "Bye";

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值