Главная Случайная страница Контакты | Мы поможем в написании вашей работы! | ||
|
(обязательное)
Полный код программы.
Файл TList.h. Интерфейсная часть.
#include <iostream>
using namespace std;
template<typename T>
struct Elem
{
T row;
Elem<T>* next;
};
template<typename T>
class TListBasicIterator;
template<typename T>
class TList
{
friend TListBasicIterator<T>;
private:
Elem<T>* first;
Elem<T>* cur;
TList<T>& CreateFirst(T val);
public:
void operator=(const TList<T>&);
TList(const TList<T>&);
bool isEmpty();
bool isEnd();
TList& Rewind();
TList& Set(T val);
T Get();
TList& Next();
TList& InsertAfterCurrent(T val);
TList& AddLast(T val);
TList& DeleteCurrent();
TList(void);
~TList(void);
friend ostream& operator<<(ostream& os, TList<T>& li)
{
if(li.isEmpty())
{
os << "Empty";
return os;
}
li.Rewind();
while(!li.isEnd())
{
os << li.Get() << " ";
li.Next();
}
li.Rewind();
return os;
}
};
Файл TList.cpp. Реализационная часть.
#include "stdafx.h"
#include "TList.h"
template<typename T>
TList<T>& TList<T>::CreateFirst(T val)
{
this->first = new Elem<T>;
this->first->row = val;
this->first->next = nullptr;
this->cur = this->first;
return (*this);
}
template<typename T>
void TList<T>::operator=(const TList<T>&)
{
throw TListException("\"=\": forbidden operator.", 3);
}
template<typename T>
TList<T>::TList(const TList<T>&)
{
throw TListException("copying list: forbidden act.", 3);
}
template<typename T>
bool TList<T>::isEmpty()
{
return (this->first == nullptr);
}
template<typename T>
bool TList<T>::isEnd()
{
return (this->cur == nullptr);
}
template<typename T>
TList<T>& TList<T>::Rewind()
{
this->cur = this->first;
return (*this);
}
template<typename T>
TList<T>& TList<T>::Set(T val)
{
if(this->isEnd())
throw TListException("Set(): Out of List", 1);
this->cur->row = val;
return (*this);
}
template<typename T>
T TList<T>::Get()
{
if(this->isEnd())
throw TListException("Get(): Out of List", 1);
return this->cur->row;
}
template<typename T>
TList<T>& TList<T>::Next()
{
if(this->isEnd())
throw TListException("Next(): Out of List", 1);
this->cur = this->cur->next;
return (*this);
}
template<typename T>
TList<T>& TList<T>::InsertAfterCurrent(T val)
{
if(this->isEmpty())
return this->CreateFirst(val);
Elem<T>* temp = this->cur->next;
this->cur->next = new Elem<T>;
this->cur->next->next = temp;
this->cur->next->row = val;
return (*this);
}
template<typename T>
TList<T>& TList<T>::AddLast(T val)
{
this->Rewind();
if(this->isEmpty())
return this->CreateFirst(val);
while(this->cur->next!= nullptr)
{
this->cur = this->cur->next;
}
return this->InsertAfterCurrent(val);
}
template<typename T>
TList<T>& TList<T>::DeleteCurrent()
{
if(this->isEnd())
throw TListException("Delete(): Out of List", 1);
if(this->cur == this->first)
{
this->first = this->cur->next;
delete this->cur;
this->cur = this->first;
return (*this);
}
Elem* ToDelete = this->cur;
Elem* Nex = this->cur->next;
this->cur = this->first;
while(this->cur->next!= ToDelete)
this->cur = this->cur->next;
Elem* Prev = this->cur;
Prev->next = Nex;
delete ToDelete;
this->cur = this->first;
return (*this);
}
template<typename T>
TList<T>::TList(void)
{
this->first = nullptr;
this->cur = this->first;
}
template<typename T>
TList<T>::~TList(void)
{
this->Rewind();
while(this->cur!= nullptr)
{
Elem<T>* temp = this->cur->next;
delete this->cur;
this->cur = temp;
}
}
Файл TLineIterator.h. Интерфейсная часть.
#pragma once
#include "TListBasicIterator.h"
template<typename T>
class TLineIterator: public TListBasicIterator<T>
{
private:
Elem<T> *cur, *first;
virtual void nothing() {}
public:
TLineIterator();
TLineIterator(const TList<T>& l);
TLineIterator<T>& operator++();
T& operator*();
TLineIterator<T>& operator~();
bool operator!();
~TLineIterator(void);
};
Файл TLineIterator. cpp. Реализационная часть.
#include "stdafx.h"
#include "TLineIterator.h"
template<typename T>
TLineIterator<T>::TLineIterator()
{
}
template<typename T>
TLineIterator<T>::TLineIterator(const TList<T>& l): TListBasicIterator(l)
{
this->first = basic_first;
this->cur = this->first;
}
template<typename T>
TLineIterator<T>& TLineIterator<T>::operator++()
{
this->cur = this->cur->next;
return (*this);
}
template<typename T>
T& TLineIterator<T>::operator*()
{
return this->cur->row;
}
template<typename T>
TLineIterator<T>& TLineIterator<T>::operator~()
{
this->cur = this->first;
return (*this);
}
template<typename T>
bool TLineIterator<T>::operator!()
{
return (this->cur == nullptr);
}
template<typename T>
TLineIterator<T>::~TLineIterator(void)
{
}
Файл TArrayIterator.h. Интерфейсная часть.
#pragma once
#include "tlistbasiciterator.h"
template<typename T>
class TArrayIterator:
public TListBasicIterator<T>
{
private:
virtual void nothing() {};
public:
TArrayIterator(void);
~TArrayIterator(void);
TArrayIterator(const TList<T>& l);
int Count();
T& operator[](int pos);
};
Файл TArrayIterator. cpp. Реализационная часть.
#include "stdafx.h"
#include "TArrayIterator.h"
template<typename T>
TArrayIterator<T>::TArrayIterator(void)
{
}
template<typename T>
TArrayIterator<T>::~TArrayIterator(void)
{
}
template<typename T>
TArrayIterator<T>::TArrayIterator(const TList<T>& l): TListBasicIterator(l)
{
}
template<typename T>
int TArrayIterator<T>::Count()
{
basic_cur = basic_first;
int c = 0;
while(basic_cur!= nullptr)
{
basic_cur = basic_cur->next;
c++;
}
return c;
}
template<typename T>
T& TArrayIterator<T>::operator[](int pos)
{
basic_cur = basic_first;
for(int i = 1; i <= pos - 1; i++)
{
basic_cur = basic_cur->next;
}
return basic_cur->row;
}
Файл TReverseIterator.h. Интерфейсная часть.
#pragma once
#include "tlistbasiciterator.h"
template<typename T>
class TReverseIterator:
public TListBasicIterator<T>
{
private:
Elem<T> *cur, *first;
virtual void nothing() {}
public:
TReverseIterator(void);
TReverseIterator(const TList<T>& l);
~TReverseIterator(void);
TReverseIterator<T>& operator++();
T& operator*();
TReverseIterator<T>& operator~();
bool operator!();
};
Файл TReverseIterator. cpp. Реализационная часть.
#include "stdafx.h"
#include "TReverseIterator.h"
template<typename T>
TReverseIterator<T>::TReverseIterator(void)
{
}
template<typename T>
TReverseIterator<T>::TReverseIterator(const TList<T>& l): TListBasicIterator(l)
{
while(basic_cur->next!= nullptr)
{
basic_cur = basic_cur->next;
}
this->first = basic_cur;
this->cur = this->first;
}
template<typename T>
TReverseIterator<T>::~TReverseIterator(void)
{
}
template<typename T>
TReverseIterator<T>& TReverseIterator<T>::operator++()
{
if(this->cur == nullptr)
{
throw TListException("Out of List in Reverse Iterator", 2);
}
if(this->cur == basic_first)
{
this->cur = nullptr;
return (*this);
}
basic_cur = basic_first;
while(basic_cur->next!= this->cur)
{
basic_cur = basic_cur->next;
}
this->cur = basic_cur;
return (*this);
}
template<typename T>
T& TReverseIterator<T>::operator*()
{
return this->cur->row;
}
template<typename T>
TReverseIterator<T>& TReverseIterator<T>::operator~()
{
this->cur = this->first;
return (*this);
}
template<typename T>
bool TReverseIterator<T>::operator!()
{
return (this->cur == nullptr);
}
Файл TListBasicIterator.h. Интерфейсная часть.
#pragma once
#include "TList.h"
template<typename T>
class TListBasicIterator
{
private:
virtual void nothing() = 0;
protected:
Elem<T>* basic_cur;
Elem<T>* basic_first;
public:
TListBasicIterator();
TListBasicIterator(const TList<T>& l);
~TListBasicIterator(void);
};
Файл TListBasicIterator.cpp. Реализационная часть.
#include "stdafx.h"
#include "TListBasicIterator.h"
template<typename T>
TListBasicIterator<T>::TListBasicIterator()
{
}
template<typename T>
TListBasicIterator<T>::TListBasicIterator(const TList<T>& l)
{
this->basic_first = l.first;
this->basic_cur = this->basic_first;
}
template<typename T>
TListBasicIterator<T>::~TListBasicIterator(void)
{
}
Файл TListException.h. Интерфейсная часть.
#pragma once
#include <iostream>
using namespace std;
class TListException
{
private:
char* errmsg;
int errnum;
public:
TListException();
TListException(char* m, int c);
char* GetMessage() const;
int GetCode() const;
~TListException();
friend ostream& operator<<(ostream& os, const TListException& ex)
{
os << endl << "\tCaught exception: " << ex.GetMessage() << " (" << ex.GetCode() << ")" << endl;
return os;
}
};
Файл TListException. cpp. Реализационная часть.
#include "stdafx.h"
#include "TListException.h"
#include <iostream>
using namespace std;
TListException::TListException()
{
this->errmsg = "";
this->errnum = 0;
}
TListException::TListException(char* m, int c)
{
this->errmsg = m;
this->errnum = c;
}
char* TListException::GetMessage() const
{
return this->errmsg;
}
int TListException::GetCode() const
{
return this->errnum;
}
TListException::~TListException()
{
}
Файл Main.cpp (основная программа)
// Effective.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "TListBasicIterator.h"
#include "TArrayIterator.h"
#include "TLineIterator.h"
#include "TReverseIterator.h"
#include "TListException.h"
int _tmain(int argc, _TCHAR* argv[])
{
TList<int> l;
l.AddLast(1).AddLast(2).AddLast(3);
TLineIterator<int> iter(l);
cout << "List contains: ";
for(~iter;!!iter; ++iter)
{
cout << *iter << " ";
}
//cout << endl << "Reversive: ";
TReverseIterator<int> rev(l);
for(~rev;!!rev; ++rev)
{
*rev = *rev + 1;
}
cout << endl << "Like an Array: ";
TArrayIterator<int> arr(l);
int c = arr.Count();
for(int i = 1; i <= c; i++)
{
cout << arr[i] << " ";
}
cout << endl;
cout << "Through <cout>: " << l << endl;
scanf_s("%d");
return 0;
}
Дата публикования: 2015-02-18; Прочитано: 234 | Нарушение авторского права страницы | Мы поможем в написании вашей работы!