📄 demo.cpp
字号:
// demo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "NodeList.h"
using namespace std;
//---------------list------------------------
template <class Item>
List<Item>::List (long size)
{
count = 0;
Items = (Item *)malloc(sizeof(Item *)*size);
}
template <class Item>
List<Item>::~List()
{
free(Items);
}
template <class Item>
long List<Item>::Count () const
{
return count;
}
template <class Item>
Item List<Item>::Get(long index) const
{
if (index >= count || index < 0) return 0;
return Items[index];
}
template <class Item>
long List<Item>::Add(Item item)
{
Items[count] = item;
count++;
return count;
}
template <class Item>
void List<Item>::Remove(Item item)
{
bool bFound = false;
for (int i = 0; i < count; i++)
{
if (bFound)
{
Items[i - 1] = Items[i];
continue;
}
if (Items[i] == item)
{
bFound = true;
}
}
count --;
}
//----------------------iterator----------------------------
template <class Item>
ListIterator<Item>::ListIterator(const List<Item> *aList)
: _list(aList),
_current(-1)
{
}
template <class Item>
void ListIterator<Item>::First()
{
if (_list->Count() > 0)
_current = 0;
else
_current = -1;
}
template <class Item>
void ListIterator<Item>::Next()
{
_current ++;
}
template <class Item>
bool ListIterator<Item>::IsDone() const
{
return (_current >= _list->Count() || _current < 0)?true:false;
}
template <class Item>
Item ListIterator<Item>::CurrentItem() const
{
if (_current < _list->Count() && _current >= 0)
return _list->Get(_current);
else
return NULL;
}
//--------------------node base--------------------------
class Node
{
public:
Node(const char *szName, const char *szIP, int nChannel)
{
pSelf = new NodeInfo();
sprintf(pSelf->name, "%s", szName);
sprintf(pSelf->ip, "%s", szIP);
pSelf->channel = nChannel;
pChilds = new List<Node *>();
}
virtual ~Node()
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
//printf("|-%s\n", i->CurrentItem()->GetInfo()->name);
delete i->CurrentItem();
}
delete i;
delete pChilds;
delete pSelf;
}
virtual NodeInfo *GetInfo()
{
return pSelf;
}
virtual void Add(Node * pNode)
{
pChilds->Add(pNode);
}
virtual void AddChild(char *ParentName, char *ChildName, int nNodeType = 0) = 0; //NodeType = 0为机构 1为设备
virtual void Remove(char *szName)
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
if (strcmp(szName, i->CurrentItem()->GetInfo()->name) == 0)
{
i->CurrentItem()->RemoveChild();
Node *pNode = i->CurrentItem();
pChilds->Remove(i->CurrentItem());
delete pNode;
}
else
{
i->CurrentItem()->Remove(szName);
}
}
delete i;
}
virtual void RemoveChild()
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
i->CurrentItem()->RemoveChild();
Node *pNode = i->CurrentItem();
pChilds->Remove(i->CurrentItem());
delete pNode;
}
delete i;
}
virtual Rename(const char *szOld, const char *szNew)
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
if (strcmp(szOld, i->CurrentItem()->GetInfo()->name) == 0)
{
sprintf(i->CurrentItem()->GetInfo()->name, "%s", szNew);
break;
}
else
{
i->CurrentItem()->Rename(szOld, szNew);
}
}
delete i;
}
virtual void Modify(const char *szName, const char *szIP, const int nChannel) = 0;
virtual void Draw(int nLevel = 1) = 0;
virtual void Reboot(void) = 0;
virtual Iterator<Node *> *CreateIterator()
{
return new ListIterator<Node *>(pChilds);
}
protected:
NodeInfo *pSelf;
List<Node *> *pChilds;
};
class Dev : public Node
{
public:
Dev(const char *szName, const char *szIP, int nChannel)
:Node(szName, szIP, nChannel)
{
}
virtual void AddChild(char *ParentName, char *ChildName, int nNodeType)
{
}
virtual void Modify(const char *szName, const char *szIP, const int nChannel)
{
if (strcmp(szName, pSelf->name) == 0)
{
sprintf(pSelf->ip, "%s", szIP);
pSelf->channel = nChannel;
}
}
virtual void Draw(int nLevel)
{
printf("|-*%s\n", pSelf->name);
/*Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
for (int j=0; j < nLevel; j++)
printf(" ");
i->CurrentItem()->Draw(nLevel+1);
}
delete i;*/
}
virtual void Reboot(void)
{
printf("reboot %s \n", pSelf->ip);
}
};
//---------------depatement-----------------
class Dep : public Node
{
public:
Dep(const char *szName, const char *szIP, int nChannel)
:Node(szName, szIP, nChannel)
{
}
virtual void AddChild(char *ParentName, char *ChildName, int nNodeType = 0)
{
if (strcmp(ParentName, pSelf->name) == 0)
{
Node *pNode;
if (nNodeType == 0)
pNode= new Dep(ChildName, "", 0);
else
pNode = new Dev(ChildName, "127.0.0.1", 0);
this->Add(pNode);
}
else
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
i->CurrentItem()->AddChild(ParentName, ChildName, nNodeType);
}
delete i;
}
}
virtual void Modify(const char *szName, const char *szIP, const int nChannel)
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
i->CurrentItem()->Modify(szName, szIP, nChannel);
}
delete i;
}
virtual void Draw(int nLevel = 1)
{
printf("|-%s\n", pSelf->name);
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
for (int j=0; j < nLevel; j++)
printf(" ");
i->CurrentItem()->Draw(nLevel+1);
}
delete i;
}
virtual void Reboot(void)
{
Iterator <Node *> *i = CreateIterator();
for (i->First(); !i->IsDone();i->Next())
{
i->CurrentItem()->Reboot();
}
delete i;
}
};
int main(int argc, char *argv[])
{
Dep *pRoot = new Dep("N1", "", 0);
char cmd[2], p1[256], p2[256];
int num;
bool bLoop = true;
while(bLoop)
{
printf("------------\n");
scanf("%s", cmd);
switch(cmd[0])
{
case 'p':
case 'P':
pRoot->Draw();
break;
case 'a':
case 'A':
scanf("%s %s %d", p1, p2, &num);
pRoot->AddChild(p1, p2, num);
break;
case 'd':
case 'D':
scanf("%s", p1);
pRoot->Remove(p1);
break;
case 'r':
case 'R':
scanf("%s %s", p1, p2);
pRoot->Rename(p1, p2);
break;
case 'm':
case 'M':
scanf("%s %s %d", p1, p2, &num);
pRoot->Modify(p1, p2, num);
break;
case 'b':
case 'B':
pRoot->Reboot();
break;
case 'q':
case 'Q':
bLoop = false;
break;
}
//printf("%s %s %s\n", cmd, p1, p2);
}
//pRoot->Draw();
delete pRoot;
system("PAUSE");
return EXIT_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -