#include <iostream>
#include<string>
using namespace std;
class node;
class Engine;
class Engine
{
protected:
int RoundPerSecond;
string Name;
public:
static node* first;
Engine()
{
RoundPerSecond = 0;
Name = "";
}
Engine(string Name, int RoundPerSecond)
{
this->Name = Name;
this->RoundPerSecond = RoundPerSecond;
}
void EngInit()
{
}
~Engine()
{
}
void print()
{
};
static void Show()
{
node::Show();//C2027
}
void Add(Engine* temp)
{
if (!first)
first = new node(temp);//C2027
else
first->Add(temp);//C2027
};
}* listEng;
class ICE:public Engine // Двигатель ДВС
{
int Cylinders;
float HorsePower;
public:
ICE() :Engine()
{
Cylinders = 0;
HorsePower = 0.f;
}
ICE(int Cylinders,float HorsePower,string Name,int RoundPerSecond) :Engine(Name,RoundPerSecond)//число цилинров ,лошадинные силы,имя ,оборотов в секунду
{
this->Cylinders = Cylinders;
this->HorsePower = HorsePower;
}
~ICE()
{
cout << "\nICE destructor\n";
}
void print()
{
cout << Name << " " << Cylinders << " " << RoundPerSecond << " "<< HorsePower <<" ICE ";
}
void Add()
{
listEng->Add(this);
}
operator Engine* ()
{
return dynamic_cast<Engine*>(this);
}
};
class JetEngine:public Engine //Рeактивный двигатель
{
float Thrust;
public:
JetEngine():Engine()
{
Thrust = 0.f;
}
JetEngine(float Thrust, string Name, int RoundPerSecond) :Engine(Name, RoundPerSecond)// Тяга ,Имя,Число оборотов
{
this->Thrust = Thrust;
}
~JetEngine()
{
cout << "\njet destructor\n";
}
void Add()
{
listEng->Add(this);
}
void print()
{
cout << Name << " " << Thrust << " " << RoundPerSecond<< " jet engine ";
}
operator Engine* ()
{
return dynamic_cast<Engine*>(this);
}
};
class node
{
friend Engine;
Engine* temp;
node* next, * prev;
short num = 0;
static short size;
public:
node(Engine* temp)
{
this->temp = temp;
next = prev = NULL;
num++;
size++;
}
node()
{
next = prev = NULL;
num = 0;
temp = NULL;
size++;
}
~node()
{
next = prev = NULL;
temp->~Engine();
size--;
}
void Add(Engine* temp)
{
try
{
if (this->next == NULL)
{
node* newone = new node(temp);
this->next = newone;
newone->prev = this;
newone->num += this->num;
return;
}
else
{
node* pnode = this;
while (pnode->next != NULL)
{
pnode = pnode->next;
}
node* newone = new node(temp);
newone->prev = pnode;
pnode->next = newone;
newone->num += pnode->num;
return;
}
}
catch (exception ex)
{
throw "\nelement you are trying to add has no adress in memory\n";
}
}
void Init()
{
node* pnode = listEng->first;
pnode->num = 1;
pnode = pnode->next;
while (pnode != NULL)
{
pnode->num = 1;
pnode->num += pnode->prev->num;
pnode = pnode->next;
}
}
void Delete(int& number)
{
if (this != NULL)
{
node* pnode = this;
while (pnode != NULL)
{
if (pnode->num == number)
{
if (pnode == listEng->first)
{
listEng->first = pnode->next;
listEng->first->prev = NULL;
pnode->~node();
this->Init();
return;
}
else
{
(pnode->prev)->next = pnode->next;
(pnode->next)->prev = pnode->prev;
pnode->~node();
this->Init();
return;
}
}
pnode = pnode->next;
}
}
else
cout << "\nlist if empty\n";
}
static void Show()
{
node* pnode = listEng->first;
while (pnode != NULL)
{
pnode->temp->print();
cout << " #" << pnode->num << endl;
pnode = pnode->next;
}
}
static short Size()
{
return size;
}
Engine* GetElement(int& number)
{
if (number < this->size)
{
node* pnode = this;
while (pnode != NULL)
{
if (pnode->num == number)
return pnode->temp;
pnode = pnode->next;
}
}
else
throw "\nthere is no element that number at list\n";
}
};
short node::size = 0;
node* Engine::first = NULL;
int main()
{
listEng = new Engine();
JetEngine VTR(9000.f,"VTR",15000);
ICE dvs(8, 150, "V5", 3000);
ICE dvs1(2, 30, "M2", 2500);
JetEngine VTR1(8400.f, "VTR1", 13000);
dvs.Add();
VTR.Add();
dvs1.Add();
VTR1.Add();
Engine::Show();
int i;
cout <<node::Size()<<"\nenter number of element which will be deleted ";
cin >> i;
listEng->first->Delete(i);
Engine::Show();
cout << "enter number of element which will be extract ";
cin >> i;
Engine* temp=listEng->first->GetElement(i);
temp->print();
return 0;
}