(Quiz)어떤 회사의 입사문제.

from Study/Quiz 2007/03/16 08:57 view 80341

출처 : http://blog.naver.com/comsik76/30005559569

컴파일시 에러 날(?) 곳을 찾아보아요.(문법적 오류 or 논리적 오류)

// composite.h

#include <list>
#include <memory>

// Composite 패턴을 구현하도록 하는 클래스
template <typename Type, typename Pointer>
class  xListComposite : public std::list<Pointer>
{
public :
 // 생성자
 xListComposite();
 // 소멸자
 ~xListComposite();

 // 부모 composite를 설정한다.
 inline void       setParent(const xListComposite* parent);
 // 부모 composite를 반환한다.
 inline xListComposite*    getParent();

 // 자식을 추가한다.
 void        insertChild(Type* child);

 // 자식들을 순환 호출한다.
 template <typename Function>
 void        order(Function& func);
 // 자식들을 순환으로 찾는다.
 template <typename Function>
 Type*        find(Function& func);

 xListComposite*      parent_;
};

// 생성자
template <typename Type, typename Pointer>
xListComposite<Type, Pointer>::xListComposite()
{
}

// 소멸자
template <typename Type, typename Pointer>
xListComposite<Type, Pointer>::~xListComposite()
{
}

// 부모 composite를 설정한다.
template <typename Type, typename Pointer>
void   xListComposite<Type, Pointer>::setParent(const xListComposite* parent)
{
 parent_ = parent;
}

// 부모 composite를 반환한다.
template <typename Type, typename Pointer>
xListComposite<Type, Pointer>* xListComposite<Type, Pointer>::getParent()
{
 return parent_;            
}

// 자식을 추가한다.
template <typename Type, typename Pointer>
void   xListComposite<Type, Pointer>::insertChild(Type* child)
{
 Pointer ptr(child);
 ptr->setParent(this);
 push_back(ptr);
}

// 자식들을 순환 호출한다.
template <typename Type, typename Pointer, typename Function>
void   xListComposite<Type, Pointer, Function>::order(Function& func)
{
 func(this);
 for (iterator i = begin(); i != end(); i ++)
  i->order(func);
}

// 자식들을 순환으로 찾는다.
template <typename Type, typename Pointer, typename Function>
Type*   xListComposite<Type, Pointer, Function>::find(Function& func)
{
 if (func(this))
  return this;

 for (iterator i = begin(); i != end(); i ++)
  if (i->find(func))
   return i->get();
}


/// leaf.h
                 
#include "composite.h"
// composite를 이용하는 노드를 생성한다.
class  xLeaf : public xListComposite<xLeaf>
{
public :
 xLeaf(const char* name)
 {
  name_ = new char [128];
  strcpy(name_, name);          
 }

 ~xLeaf()              
 {
  delete name_;
 }

 char*    name_;          
}; // xLeaf

/// program.cpp

#include "leaf.h"

struct  Ldisplay
{
 void operator () (xLeaf* leaf)
 {
  std::cout << leaf->name_;
 }
};

struct  Lfind
{
 Lfind(char* find)
 {
  strcpy(find_, find);
 }
 bool operator () (xLeaf* leaf)
 {
  return strcmp(leaf->name_, find_) == 0;
 }
 char  find_[128];
};

void  main()
{
 xLeaf root("과일");

 root.insertChild(new xLeaf("사과"));
 root.insertChild(new xLeaf("바나나"));
 root.insertChild(new xLeaf("복숭아"));
 root.insertChild(new xLeaf("배"));

 xLeaf* leaf = root.find(Lfind("사과"));
 leaf->insertChild(new xLeaf("부사"));
 leaf->insertChild(new xLeaf("국광"));

 root.order(Ldisplay());
}

// 버그 수정및 결과 예측

Tag |

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다