Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* игра с двойно свързана циклична структура - прочитане на числа (брой топчета), които държат в ръце хора подредени в кръг. първият дава по едно от своите топчета на всички по часовниковата стрелка. когато му свършат “предава щафетата” на следващия след него. играта свършва, когато се достигне до играч, който няма топчета. */
- #include <iostream>
- #include <fstream>
- using namespace std;
- template <typename T>
- class DoubleLink
- {
- DoubleLink* previous;
- T payload;
- DoubleLink* next;
- public:
- DoubleLink ()
- {
- previous = this;
- next = this;
- }
- DoubleLink (T init)
- {
- previous = this;
- next = this;
- payload = init;
- }
- void AddLink (DoubleLink* new_elem)
- {
- DoubleLink* a = this;
- DoubleLink* b = this->next;
- a->next = new_elem;
- new_elem->previous = a;
- b->previous = new_elem;
- new_elem->next = b;
- }
- inline DoubleLink* GetPrevious()
- {
- return previous;
- }
- inline DoubleLink* GetNext()
- {
- return next;
- }
- inline DoubleLink* SetNext(DoubleLink* new_next )
- {
- return (next = new_next);
- }
- inline DoubleLink* SetPrev(DoubleLink* new_prev )
- {
- return (previous = new_prev);
- }
- inline T GetData ()
- {
- return payload;
- }
- inline T SetData (T new_data)
- {
- return (payload = new_data);
- }
- void Delete ()
- {
- if ( this != this->GetNext() ) {
- this->GetPrevious()->SetNext( this->GetNext() );
- this->GetNext()->SetPrev( this->GetPrevious() );
- }
- delete this;
- }
- };
- void PrintElements (DoubleLink<int> *dl)
- {
- DoubleLink<int> *curr = dl->GetNext();
- do
- {
- cout << curr->GetPrevious()->GetData() << " ";
- curr = curr->GetNext();
- }
- while (curr != dl);
- cout << curr->GetPrevious()->GetData() << endl;
- }
- void PlayGame (DoubleLink<int>* dl)
- {
- bool flag = true;
- DoubleLink<int>* curr = dl;
- cout << "-- start game -- " << endl;
- while ( curr != curr->GetNext() ) {
- while ( curr->GetData() ) {
- DoubleLink<int>* p = curr->GetNext();
- cout << (flag ? '>' : '<');
- while (curr->GetData() ) {
- curr->SetData( curr->GetData() - 1);
- p->SetData (p->GetData() + 1);
- if (flag)
- p = p->GetNext();
- else
- p = p->GetPrevious();
- }
- curr = p;
- flag = !flag;
- PrintElements(dl);
- }
- DoubleLink<int>* newc = flag ? curr->GetNext() : curr->GetPrevious();
- if (curr == dl)
- dl = newc;
- curr->Delete();
- curr = newc;
- }
- }
- int main ()
- {
- ifstream file ("input.txt");
- if (!file)
- {
- cerr << "File could not be opened.\n";
- return -1;
- }
- DoubleLink<int> *curr = NULL;
- while (!file.eof())
- {
- int x;
- file >> x;
- if (curr)
- curr->AddLink(new DoubleLink<int> (x));
- else
- curr = new DoubleLink<int> (x);
- curr = curr->GetNext();
- }
- PrintElements(curr->GetNext());
- PlayGame(curr->GetNext());
- // system ("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement