//

//  main.cpp

//  ExampleCpp

//

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

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

//


#include <iostream>

#include <string>

using namespace std;


void selection(int *arr);

void swap(int *num1, int *num2);


int main()

{

    int arr[5] = {4,3,9,1,2};

    selection(arr);

}


void selection(int arr[])

{

    int i, j;

    int min;

    for(i = 0; i<5-1; i++)

    {

        min = i;

        for(j = i+1; j < 5; j++)

        {

            if(arr[j] < arr[min])

                min = j;

        }

        swap(&arr[i], &arr[min]);

        

        cout <<i+1 <<"회전: ";

        for(int k = 0; k< 5; k++)

            cout << arr[k] << " " ;

        cout << endl;

    }

}



void swap(int *num1, int *num2)

{

    int temp;

    temp = *num1;

    *num1 = *num2;

    *num2 = temp;

}

'Programing > Algorithm' 카테고리의 다른 글

프로젝트 오일러 10번  (0) 2015.07.29
프로젝트 오일러 9번  (0) 2015.04.25
Recursive Fibonacci  (0) 2015.04.06
순환/재귀 팩토리알 구현  (0) 2015.04.06
3개의 숫자를 내림차순으로 정렬  (0) 2015.04.06
Posted by kimmayer


int *Stack;

int Size;

int top=0;


void initStack(int aSize)

{

Size = aSize;

Stack = (int *)malloc(Size*sizeof(int));

}


void push(int item)

{

if (Size == top)

{

printf("Stack is Full!\n");

}

else

{

Stack[top++] = item;

printf("top is %d!\n", top);

}

}


void pop()

{

Stack[top] = 0;

}

int main()

{

initStack(10);

push(1);

push(2);

push(3);

push(4);

push(5);

push(6);

push(7);

push(8);

pop();

pop();

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

{

printf("%d\n", Stack[i]);

}

}

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

socket operation on non-socket 에러  (0) 2015.12.08
scanf를 %s와 %c로 받는 차이  (0) 2015.02.10
2차원 배열 함수 인자로 넘기기 예제  (0) 2015.02.10
전치행렬 구현  (0) 2015.02.10
피보나치 수열 재귀 함수, For loop  (0) 2015.02.10
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
이전버튼 1 2 3 4 5 6 7 ··· 20 이전버튼

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

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.3
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
31

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함