ReadyToRoll / laba4-31.10.2013

0 stars 0 forks source link

Заўвагі #1

Open HaraldZealot opened 10 years ago

HaraldZealot commented 10 years ago

Ну тут да канца яшчэ вельмі далёка :smile:

ReadyToRoll commented 10 years ago

Ну с файлами я понимаю, разнесу потом.. А все-таки, какая конкретно ставится задача тогда? объясните пожалуйста поконкретней:)

HaraldZealot commented 10 years ago

Я як раз пішу вам прыклад, пачакайце яшчэ хвілін 10

HaraldZealot commented 10 years ago

Вось прыклад на стэке (ён рэалізаваны не цалкам, а толькі з часткай функцый). Можаце гэты прыклад паказаць і іншым, хто піша нелінейныя структуры.

Файл stack.h:

#ifndef STACK_H
#define STACK_H

typedef int Data;

class Stack
{
    public:
        Stack();
        ~Stack();
        void push(Data datum);
    protected:
        bool nextDatum(bool firstTime, Data &datum) const;
    private:
        class StackImplementation;
        StackImplementation *pimpl;
};

#endif // STACK_H

Файл stack.cpp:

#include "stack.h"

struct Node
{
    Data datum;
    Node *next;
    Node(Data datum): datum(datum), next(0) {}
    ~Node()
    {
        datum = 0;
        next = 0;
    }
};

class Stack::StackImplementation
{
    public:
        StackImplementation();
        ~StackImplementation();

        void push(Data datum);
        void clear();

        bool nextDatum(bool firstTime, Data &datum) const;
    private:
        Node *top;
};

////////////  Stack //////////////

Stack::Stack():
    pimpl(0)
{
    pimpl = new StackImplementation;
}

Stack::~Stack()
{
    delete pimpl;
    pimpl = 0;
}

void Stack::push(Data datum)
{
    pimpl->push(datum);
}

bool Stack::nextDatum(bool firstTime, Data &datum) const
{
    return pimpl->nextDatum(firstTime, datum);
}
///// StackImplementation ////////

Stack::StackImplementation::StackImplementation():
    top(0)
{}

Stack::StackImplementation::~StackImplementation()
{
    clear();
}

void Stack::StackImplementation::push(Data datum)
{
    Node *p = new Node(datum);
    p->next = top;
    top = p;
    p = 0;
}

void Stack::StackImplementation::clear()
{
    while(top)
    {
        Node *p = top;
        top = top->next;
        delete p;
        p = 0;
    }
}

bool Stack::StackImplementation::nextDatum(bool firstTime, Data &datum) const
{
    static Node *cur;

    if(firstTime)
        cur = top;

    bool result = cur != 0;

    if(result)
    {
        datum = cur->datum;
        cur = cur->next;
    }

    return result;
}

Файл main.cpp:

#include <iostream>
#include "stack.h"

using namespace std;

class ConsoleStack: public Stack
{
    public:
        void output(ostream &out) const;
};

ostream &operator<<(ostream &out, const ConsoleStack &stack);

int main()
{
    ConsoleStack stack;
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    cout << stack << endl;
    return 0;
}

ostream &operator<<(ostream &out, const ConsoleStack &stack)
{
    stack.output(out);
    return out;
}

void ConsoleStack::output(ostream &out) const
{
    Data datum;

    if(nextDatum(true, datum))
        out << datum << " ";

    while(nextDatum(false, datum))
        out << datum << " ";
}
ReadyToRoll commented 10 years ago

Спасибо! Вот переделал вроде, пока еще без ConsoleSet правда, но вот операторы все равно не хотят работать :(