'Study/Quiz'에 해당되는 글 3건

  1. 2007/12/01 한글 문자열을 뒤집어라.
  2. 2007/03/27 (Quiz) Cabin이 출력되게 하여라.
  3. 2007/03/16 (Quiz)어떤 회사의 입사문제.

한글 문자열을 뒤집어라.

from Study/Quiz 2007/12/01 18:28 view 26672

한글도 되도록 하자.

// 200447214 김준주
#include <iostream>
using namespace std;

char* ReverseString(const char* src, int len)
{
 // 새로운 문자열을 보관할 메모리를 할당한다.
 char* reverse = new char[len + 1];

 // 문자열을 역순으로 복사한다.
 for(int i = 0; i < len; ++i)
 {
  if( src[len-i-1] < 0 && src[len-i-2] < 0)
  {
   reverse[i] = src[len - i - 2];
   reverse[i+1] = src[len - i - 1];

   ++i;
  }
  else
   reverse[i] = src[len - i - 1];
 }

 // 새 문자열의 끝에 NULL을 넣어준다.
 reverse[len] = NULL;

 // 새 문자열을 반환한다.
 return reverse;
}

int main()
{
 // 문자열을 하나 만든다.
 char original[] = "한글 뒤집기";

 // 함수를 호출한다.
 char* copy = ReverseString( original, strlen(original) );

 // 두문자열을 출력한다.
 cout << original << "\n";
 cout << copy << "\n";

 // 새 문자열의 메모리를 해제한다.
 delete[] copy;
 copy = NULL;

 return 0;
}

Tag | ,

(Quiz) Cabin이 출력되게 하여라.

from Study/Quiz 2007/03/27 17:12 view 20576

#include <TurboC.h>

 

void InputName(char *pName)

{

    pName=(char *)malloc(12);

    strcpy(pName,"Cabin");

}

 

void main()

{

    char *Name;

 

    InputName(Name);

 

    printf("이름은%s입니다\n",Name);

 

    free(Name);

}


메모리 누수를 막고, Name 주소를 할당해야 한다.

맞나?

more..


Tag |

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

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

출처 : 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 |