2015. 12. 2. 20:48 Programing/C++

ReverseString

#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)

reverse[i] = src[len - i - 1];

reverse[len] = NULL;

return reverse;

}



int main()

{

char ch[] = "hail to the king";

char* rch = ReverseString(ch, 16);


cout << ch << endl;

cout << rch << endl;

delete[] rch;

rch = NULL;

return 0;

}


첨고: 뇌를 자극하는 C++

'Programing > C++' 카테고리의 다른 글

클래스 템플릿 비교  (0) 2015.09.04
두 정수가 같으면 true 다르면 false를 리턴하는 Equal 클래스  (0) 2015.09.04
함수 객체  (0) 2015.09.04
멤버 함수 포인터 선언  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
Posted by kimmayer

//

//  main.cpp

//  ExampleCpp

//

//  Created by 김일호 on 2015. 7. 10..

//  Copyright (c) 2015 김일호. All rights reserved.



//  클래스 템플릿 사용 전 각 자료형에 맞는 클래스를 생성


#include <iostream>

#include <string>

using namespace std;


class IntArray

{

    int *buf;

    int size;

    int capacity;

    

public:

    explicit IntArray(int cap = 100):buf(0), size(0), capacity(cap)

    {

        buf = new int[capacity];

    }

    

    ~IntArray() { delete[] buf;}

    

    void Add(int data)

    {

        buf[size++] = data;

    }

    

    int operator[](int idx) const

    {

        return buf[idx];

    }

    

    int GetSize() const

    {

        return size;

    }

};


class DoubleArray

{

    double *buf;

    int size;

    int capacity;

public:

    explicit DoubleArray(int cap=100):buf(0), size(0), capacity(cap)

    {

        buf = new double[capacity];

    }

    ~DoubleArray() {delete[] buf;}

    

    void Add(double data)

    {

        buf[size++] = data;

    }

    

    double operator[] (int idx) const

    {

        return buf[idx];

    }

    

    int GetSize() const {return size;}

};


class StringArray

{

    string *buf;

    int size;

    int capacity;

public:

    

    explicit StringArray(int cap = 100):buf(0), size(0), capacity(cap)

    {

        buf = new string[capacity];

    }

    void Add(string data)

    {

        buf[size++] = data;

    }

    

    string operator[] (int idx) const

    {

        return buf[idx];

    }

    

    int GetSize() const {return size;}

};


int main()

{

    IntArray iarr;

    

    iarr.Add(10);

    iarr.Add(20);

    iarr.Add(30);

    

    for (int i = 0; i<iarr.GetSize(); ++i)

    {

        cout << iarr[i] << endl;

    }

    

    DoubleArray darr;

    

    darr.Add(10.12);

    darr.Add(20.12);

    darr.Add(30.12);

    

    for (int i = 0; i<darr.GetSize(); ++i)

    {

        cout << darr[i] << endl;

    }

    

    StringArray sarr;

    

    sarr.Add("abc");

    sarr.Add("ABC");

    sarr.Add("sdfads");

    

    for (int i = 0; i<sarr.GetSize(); ++i)

    {

        cout << sarr[i] << endl;

    }

    

    

    return 0;

}


//클래스 템플릿 버전


//

//  main.cpp

//  ExampleCpp

//

//  Created by 김일호 on 2015. 7. 10..

//  Copyright (c) 2015 김일호. All rights reserved.

//


#include <iostream>

#include <string>

using namespace std;


template<typename T>

class Array

{

    T *buf;

    int size;

    int capacity;

    

public:

    explicit Array(int cap = 100):buf(0), size(0), capacity(cap)

    {

        buf = new T[capacity];

    }

    

    ~Array() { delete[] buf;}

    

    void Add(T data)

    {

        buf[size++] = data;

    }

    

    T operator[](int idx) const

    {

        return buf[idx];

    }

    

    int GetSize() const

    {

        return size;

    }

};



int main()

{

    Array<int> iarr;

    

    iarr.Add(10);

    iarr.Add(20);

    iarr.Add(30);

    

    for (int i = 0; i<iarr.GetSize(); ++i)

    {

        cout << iarr[i] << endl;

    }

    

    Array<double> darr;

    

    darr.Add(10.12);

    darr.Add(20.12);

    darr.Add(30.12);

    

    for (int i = 0; i<darr.GetSize(); ++i)

    {

        cout << darr[i] << endl;

    }

    

    Array<string> sarr;

    

    sarr.Add("abc");

    sarr.Add("ABC");

    sarr.Add("sdfads");

    

    for (int i = 0; i<sarr.GetSize(); ++i)

    {

        cout << sarr[i] << endl;

    }

    

    

    return 0;

}


클래스를 템플릿화 시키면서 클라이언트에서는 자료형에 상관없이 클래스에 사용될 타입을 정의할 수 있다.

'Programing > C++' 카테고리의 다른 글

ReverseString  (0) 2015.12.02
두 정수가 같으면 true 다르면 false를 리턴하는 Equal 클래스  (0) 2015.09.04
함수 객체  (0) 2015.09.04
멤버 함수 포인터 선언  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
Posted by kimmayer

#include <iostream>

#include <algorithm>

using namespace std;


/**********************/

/*

class Equal {

int x, y;

public :

Equal() {}

Equal(int x, int y) { x = this->x; y = this->y; }

~Equal() {}

bool cmp_func()

{

return x == y  ? true : false;

}

};


int main()

{

Equal cmp(2, 2);

cout << cmp.cmp_func() << endl;

return 0;

}

*/ 

/******* 함수 객체 사용 ***************/ 

class Equal {

int x, y;

public:

Equal() {}

~Equal() {}

bool operator() (int x, int y)

{

return x == y ? true : false;

}

};


int main()

{

Equal cmp;

if (cmp(2, 2))

{

cout << "같다" << endl;

}

else

{

cout << "다르다" << endl;

}


return 0;

}

'Programing > C++' 카테고리의 다른 글

ReverseString  (0) 2015.12.02
클래스 템플릿 비교  (0) 2015.09.04
함수 객체  (0) 2015.09.04
멤버 함수 포인터 선언  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
Posted by kimmayer

2015. 9. 4. 11:23 Programing/C++

함수 객체

함수 객체는 함수처럼 동작하는 객체, 함수처럼 동작하려면 객체가 '()' 연산자를 정의해야 한다.

즉 () 연산자를 오버로딩한 객체가 함수 객체


#include <iostream>

#include <algorithm>

using namespace std;


struct Functor1 {

void operator() (int n)

{

cout << n << " ";

}

};

struct Functor2 {

void operator() (int n)

{

cout << n*n << " ";

}

};

struct Functor3 {

void operator() (int n)

{

cout << "정수 : " << n << endl;

}

};


int main()

{

int arr[5] = { 10,20,30,40,50 };


for_each(arr, arr + 5, Functor1());

cout << endl << endl;

for_each(arr, arr + 5, Functor2());

cout << endl << endl;

for_each(arr, arr + 5, Functor3());


return 0;

}

Posted by kimmayer

함수 포인터는 함수의 시작 주소를 저장하는 포인터

함수의 이름은 함수가 시작하는 시작 주소를 나타내며 함수 포인터는 이 함수의 주소를 저장하는 포인터!


#include <iostream>

#include <cstring>

using namespace std;


class Point

{

int x;

int y;

public:

explicit Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}

void Print() const { cout << x << ", " << y << endl; }

void PrintInt(int n) { cout << "테스트정수: "<< n << endl; }

};


int main()

{

Point pt(2, 3);

Point *p = &pt;


void (Point::*pf1)() const; //멤버 함수 포인터 선언

pf1 = &Point::Print;


void(Point::*pf2)(int); //멤버 함수 포인터 선언

pf2 = &Point::PrintInt;


pt.Print();

pt.PrintInt(10);

cout << endl;


(pt.*pf1)();

(pt.*pf2)(10);

cout << endl;


(p->*pf1)();

(p->*pf2)(10);


return 0;

}

'Programing > C++' 카테고리의 다른 글

두 정수가 같으면 true 다르면 false를 리턴하는 Equal 클래스  (0) 2015.09.04
함수 객체  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
-> , * 연산자 오버로딩  (0) 2015.09.02
간단한 오버라이딩  (0) 2015.09.02
Posted by kimmayer

#include <iostream>

#include <cstring>

using namespace std;


class String {

char buf[1000];

public:

String(const char* sz) 

{

strcpy(buf, sz);

}

~String() {}


operator const char* () const 

{

return buf;

}

};


int main()

{

String s("Hello");

const char* sz = s;


cout << sz << endl;

}



operator const char* () const
{
 return buf;
}

형변환 연산자 오버로딩

return buf; 이므로 String 클래스 안에 있는 맴버 buf를 return


형변환 연산자 오버로딩은 

operator 타입명 () const

으로 원형을 정의한다.



'Programing > C++' 카테고리의 다른 글

함수 객체  (0) 2015.09.04
멤버 함수 포인터 선언  (0) 2015.09.04
-> , * 연산자 오버로딩  (0) 2015.09.02
간단한 오버라이딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
Posted by kimmayer

#include <iostream>

using namespace std;


class Point {

int x;

int y;

public:

Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}

void Print() const { cout << x << ", " << endl; }

};


class PointPtr

{

Point *ptr;

public:

PointPtr(Point *p) :ptr(p) {}

~PointPtr() { delete ptr; }


Point* operator->() const

{

return ptr;

}

Point& operator*() const

{

return *ptr;

}

};


int main()

{

Point* p1 = new Point(2, 3);

PointPtr p2 = new Point(5, 5);


p1->Print();

p2->Print();


return 0;

}


p1은 일반 포인터, p2가 스마트 포인터

'Programing > C++' 카테고리의 다른 글

멤버 함수 포인터 선언  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
간단한 오버라이딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
Posted by kimmayer

#include <iostream>

using namespace std;


class Point {

int x, y;


public:

Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}

void Print() const {

cout << x << ", " << y << endl;

}

const Point operator+(const Point &arg) const

{

Point pt;

pt.x = this->x + arg.x;

pt.y = this->y + arg.y;


return pt;

}


const Point& operator++()

{

++x;

++y;


return *this;

}

const Point operator++(int)

{

Point pt(x, y);

++x;

++y;

return pt;

}

};


int main()

{

Point pt1(2, 3);

Point pt2(3, 5);

++pt2;

Point pt3;

pt3 = pt1 + pt2;


pt3.Print();

pt2.Print();

return 0;

}

'Programing > C++' 카테고리의 다른 글

형변환 연산자 오버로딩  (0) 2015.09.04
-> , * 연산자 오버로딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
Posted by kimmayer

#include <iostream>

#include <Windows.h>

#include <random>

#include <time.h>

#include "Console.h"

//#include "enemy.h"

//#include "player.h"

#define ENEMY_COUNT 10

#define PLAYER_LIFE 1

using namespace std;


typedef struct enemy{

int x, y;

char name = 'e';

char blank = ' ';

}enemy;


typedef struct player{

int x= 40, y= 19;

char name = 'p';

int life = PLAYER_LIFE;

}player;


typedef struct player_shoot{

int x, y;

char name = '.';

};


typedef struct enemy_shoot{

int x, y;

int flag = 0;

char name = '*';

};


void enemy_shoot_initial();

void enemy_shoot_move();

int move_flag = 0;

int shoot_flag = 0;

int _gameover = 1;

void enemy_initial();

void enemy_move();

void player_shoot_initial();

void player_shoot_move();

void player_shoot_check();


enemy e;

player p;

player_shoot p_s;

enemy_shoot e_s[10];

enemy e_member[ENEMY_COUNT];


int main()

{

enemy_initial();

while (_gameover)

{

Initial();

for (int i = 0; i < 10; i++){

MoveCursor(e_member[i].x, e_member[i].y);

cout << e_member[i].name;

}

enemy_move();

MoveCursor(p.x, p.y);

cout << p.name;

MoveCursor(p_s.x, p_s.y);

if (GetAsyncKeyState(VK_CONTROL)){

player_shoot_initial();

}

cout << p_s.name;

if (GetAsyncKeyState(VK_UP)){ p.y -= 1; }

if (GetAsyncKeyState(VK_DOWN)){ p.y += 1; }

if (GetAsyncKeyState(VK_LEFT)){ p.x -= 1; }

if (GetAsyncKeyState(VK_RIGHT)){ p.x += 1; }


enemy_shoot_initial();

enemy_shoot_move();

player_shoot_move();

player_shoot_check();

ClearScreen();

}

cout << "gameover";

}


void enemy_move()

{

if (move_flag == 0){

for (int i = 0; i < 10; i++)

{

e_member[i].x += 1;

}

if (e_member[9].x == 80)

move_flag = 1;

}

if (move_flag == 1){

for (int i = 0; i < 10; i++)

{

e_member[i].x -= 1;

}

if (e_member[0].x == 2)

move_flag = 0;

}

}

void enemy_initial()

{

for (int i = 0; i < 10; i++)

{

e_member[i].x = 20 + (i*2) ;

e_member[i].y = 5;

e_member[i].name = 'e';

}

}


void player_shoot_initial()

{

p_s.x = p.x;

p_s.y = p.y;

}

void player_shoot_move()

{

p_s.y -= 1;

}



void player_shoot_check()

{

for (int i = 0; i < 10; i++){

if (p_s.x == e_member[i].x && p_s.y == e_member[i].y)

e_member[i].name = e_member[i].blank;

}

}



void enemy_shoot_initial()

{

int select;

srand(time(NULL));

select = rand() % 10;

e_s[select].flag = 1;

e_s[select].x = e_member[select].x;

e_s[select].y = e_member[select].y;

}

void enemy_shoot_move()

{

for (int i = 0; i < 10; i++)

{

if (e_s[i].flag == 1)

{

MoveCursor(e_s[i].x, e_s[i].y);

cout << e_s[i].name;

e_s[i].y += 1;

if (e_s[i].y == 22 )

{

e_s[i].name = ' ';

e_s[i].y = 0;

e_s[i].flag = 0;

}

if ((p.x == e_s[i].x && p.y == e_s[i].y))

{

p.life -= 1;

if (p.life == 0){

_gameover = 0;

}

}

}

}

}


구조체로 구현한 Invader 게임입니다. 버퍼를 따로 생성하지 않아 끊기는 현상이 있는데, 버퍼를 구현해서 끊기지 않도록 해야겠습니다.



'Programing > C++' 카테고리의 다른 글

-> , * 연산자 오버로딩  (0) 2015.09.02
간단한 오버라이딩  (0) 2015.09.02
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
Posted by kimmayer

2015. 4. 27. 23:56 Programing/C++

클래스 template

Template <typename T>

class C{

public:

.

.

.

.

};


로 구현한다. 클래스 선언 앞에 키워드만 붙이면 된다.


Template <typename T = double, int size = 10> 처럼 디폴트 매개변수의 값을 지정 할 수 있다.

'Programing > C++' 카테고리의 다른 글

간단한 오버라이딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
함수 Template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
Tree 구현하기  (0) 2015.02.10
Posted by kimmayer
이전버튼 1 2 이전버튼

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함