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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
 
class Trace {
    static string tag[100];
    static string info[100];
    static int index;
public:
    static void put(string info, string tag);
    static void print(string tag);
    static void print();
};
 
int Trace::index = 0;
string Trace::tag[100= {};
string Trace::info[100= {};
 
void Trace::put(string _tag, string _info) {
    if (index == 100cout << "스택 꽉 참" << '\n';
    tag[index] = _tag;
    info[index] = _info;
    index++;
}
 
void Trace::print(string _tag) {
    cout << "-------" << _tag << "태그의 Trace 정보를 출력합니다. -------" << '\n';
    for (int i = 0; i < index; i++) {
        if (tag[i] == _tag)
            cout << _tag << ":" << info[i] << '\n';
    }
}
 
void Trace::print() {
    cout << "------- 모든 Trace 정보를 출력합니다. -------" << '\n';
    for (int i = 0; i < index; i++)
        cout << tag[i] << ":" << info[i] << '\n';
}
 
void f() {
    int a, b, c;
    cout << "두 개의 정수를 입력하세요>>";
    cin >> a >> b;
    Trace::put("f()""정수를 입력 받았음");
    c = a + b;
    Trace::put("f()""합 계산");
    cout << "합은 " << c << endl;
}
 
int main() {
    Trace::put("main()""프로그램 시작합니다.");
    f();
    Trace::put("main()""종료");
 
    Trace::print("f()");
    Trace::print();
}
cs


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
class Random {
public:
    static void seed() { srand((unsigned)time(0)); }
    static int nextInt(int min = 0int max = 32767);
    static char nextAlphabet();
    static double nextDouble();
};
 
int Random::nextInt(int min, int max) {
    int random = (rand() % (max - min + 1)) + min;
    return random;
}
 
char Random::nextAlphabet() {
    char c_random;
    int random = rand();
    random % 2 == 0 ? c_random = (char)(nextInt(6590)) : c_random = (char)(nextInt(97122));
    return c_random;
}
 
double Random::nextDouble() {
    double d_random = rand() / (double)RAND_MAX;
    return d_random;
}
 
int main() {
    Random r;
    r.seed();
 
    cout << "1에서 100까지 랜덤한 정수 10개를 출력합니다" << endl;
    for (int i = 0; i < 10; i++cout << r.nextInt(1100<< ' ';
    cout << endl;
    cout << "알파벳을 랜덤하게 10개를 출력합니다" << endl;
    for (int i = 0; i < 10; i++cout << r.nextAlphabet() << ' ';
    cout << endl;
    cout << "랜덤한 실수를 10개 출력합니다" << endl;
    for (int i = 0; i < 10; i++) {
        if (i == 5cout << endl;
        cout << r.nextDouble() << ' ';
    }
    cout << endl;
}
cs


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
 
class ArrayUtility2 {
public:
    static int* concat(int s1[], int s2[], int size);
    static int* remove(int s1[], int s2[], int sizeint& retSize);
};
 
int* ArrayUtility2::concat(int s1[], int s2[], int size) {
    int *buf = new int[size * 2];
    for (int i = 0; i < size * 2; i++) {
        i < size ? buf[i] = s1[i] : buf[i] = s2[i - size];
    }
    return buf;
}
 
int* ArrayUtility2::remove(int s1[], int s2[], int sizeint& retSize) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (s1[i] == s2[j]) s1[i] = NULL;
        }
    }
    for (int i = 0; i < size; i++)
        if (s1[i] != NULL) retSize++;
 
    if (retSize == 0return NULL;
 
    int *buf = new int[retSize];
    int n = 0;
 
    for (int i = 0; i < size; i++) {
        if (s1[i] != NULL) {
            buf[n] = s1[i];
            n++;
        }
    }
    return buf;
}
 
int main() {
    int x[5], y[5];
    int *z;
    int retSize = 0;
    cout << "정수를 5 개 입력하라. 배열 x에 삽입한다>>";
    for (int i = 0; i < 5; i++cin >> x[i];
    cout << "정수를 5 개 입력하라. 배열 y에 삽입한다>>";
    for (int i = 0; i < 5; i++cin >> y[i];
 
    cout << "합친 정수 배열을 출력한다" << endl;
    z = ArrayUtility2::concat(x, y, 5);
    for (int i = 0; i < 10; i++) {
        cout << z[i] << ' ';
    }
    cout << endl;
 
    z = ArrayUtility2::remove(x, y, 5, retSize);
    cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << retSize << endl;
    if (retSize == NULLcout << "두 배열이 같습니다." << endl;
    else {
        for (int i = 0; i < retSize; i++) {
            cout << z[i] << ' ';
        }
        cout << endl;
    }
}
cs


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
32
33
34
#include <iostream>
using namespace std;
 
class ArrayUtility {
public:
    static void intToDouble(int source[], double dest[], int size);
    static void doubleToInt(double source[], int dest[], int size);
};
 
void ArrayUtility::intToDouble(int source[], double dest[], int size) {
    for (int i = 0; i < size; i++) {
        dest[i] = (double)source[i];
    }
}
 
void ArrayUtility::doubleToInt(double source[], int dest[], int size) {
    for (int i = 0; i < size; i++) {
        dest[i] = (int)source[i];
    }
}
 
int main() {
    int x[] = { 1,2,3,4,5 };
    double y[5];
    double z[] = { 9.9,8.8,7.7,6.6,5.6 };
 
    ArrayUtility::intToDouble(x, y, 5);
    for (int i = 0; i < 5; i++cout << y[i] << ' ';
    cout << endl;
 
    ArrayUtility::doubleToInt(z, x, 5);
    for (int i = 0; i < 5; i++cout << x[i] << ' ';
    cout << endl;
}
cs


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
#include <iostream>
using namespace std;
 
class MyVector {
    int *mem;
    int size;
public:
    MyVector(int n = 100int val = 0) { mem = new int[n]; size = n; for (int i = 0; i < size; i++) mem[i] = val; }
    ~MyVector() { delete[]mem; }
    void show() {
        int count = 0;
        for (int i = 0; i < size; i++) {
            cout << mem[i] << ' ';
            count++;
        }
        cout << endl << "총 " << count << "번 출력됨" << endl << endl;
    }
};
 
int main() {
    MyVector a;
    MyVector b(10);
    MyVector c(1510);
    a.show();
    b.show();
    c.show();
}
cs


AP3_(1).cpp

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
#include <iostream>
using namespace std;
 
int big(int x, int y) {
    if (y > x) {
        if (y < 100return y;
    }
    else {
        if (x < 100return x;
    }
    return 100;
}
 
int big(int x, int y, int limit) {
    if (y > x) {
        if (y < limit) return y;
    }
    else {
        if (x < limit) return x;
    }
    return limit;
}
 
int main() {
    int x = big(35);
    int y = big(30060);
    int z = big(306050);
    cout << x << ' ' << y << ' ' << z << endl;
}
cs

AP3_(2).cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
 
int big(int x, int y, int limit = 100) {
    if (y > x) {
        if (y < limit) return y;
    }
    else {
        if (x < limit) return x;
    }
    return limit;
}
 
int main() {
    int x = big(35);
    int y = big(30060);
    int z = big(306050);
    cout << x << ' ' << y << ' ' << z << endl;
}
cs


AP2_(1).cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;
 
class Person {
    int id;
    double weight;
    string name;
public:
    Person() { id = 1; weight = 20.5; name = "Grace"; }
    Person(int id, string name) { this->id = id; this->name = name; weight = 20.5; }
    Person(int id, string name, double weight) { this->id = id; this->name = name; this->weight = weight; }
    void show() { cout << id << ' ' << weight << ' ' << name << endl; }
};
 
int main() {
    Person grace, ashley(2"Ashley"), helen(3"Helen"32.5);
    grace.show();
    ashley.show();
    helen.show();
}
cs

AP2_(2).cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;
 
class Person {
    int id;
    double weight;
    string name;
public:
    Person(int id = 1string name = "Grace"double weight = 20.5) { this->id = id; this->name = name; this->weight = weight; }
    void show() { cout << id << ' ' << weight << ' ' << name << endl; }
};
 
int main() {
    Person grace, ashley(2"Ashley"), helen(3"Helen"32.5);
    grace.show();
    ashley.show();
    helen.show();
}
cs


AP1_(1).cpp

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
#include <iostream>
using namespace std;
 
int add(int a[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += a[i];
    }
    return sum;
}
 
int add(int a[], int sizeint b[]) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += a[i];
        sum += b[i];
    }
    return sum;
}
 
int main() {
    int a[] = { 1,2,3,4,5 };
    int b[] = { 6,7,8,9,10 };
    int c = add(a, 5);
    int d = add(a, 5, b);
    cout << c << endl;
    cout << d << endl;
}
cs

AP1_(2).cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
 
int add(int a[], int sizeint b[] = NULL) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += a[i];
        if (b != NULL) sum += b[i];
    }
    return sum;
}
 
int main() {
    int a[] = { 1,2,3,4,5 };
    int b[] = { 6,7,8,9,10 };
    int c = add(a, 5);
    int d = add(a, 5, b);
    cout << c << endl;
    cout << d << endl;
}
cs


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
class Person {
    string p[2];
    int index;
public:
    Person() { p[0= "류현진"; p[1= "손연재"; index = 0; }
    string getName();
};
 
string Person::getName() {
    if (index == 0) {
        index++;
        return p[index - 1];
    }
    else if (index == 1) {
        index = 0;
        return p[index + 1];
    }
}
 
class UpAndDownGame {
    static int low, high;
public:
    UpAndDownGame() { srand((unsigned)time(0)); }
    static void playGame();
};
 
int UpAndDownGame::low = 0;
int UpAndDownGame::high = 99;
 
void UpAndDownGame::playGame() {
    Person p;
    string name = p.getName();
    int number, result = rand() % 100;
    cout << "UP & Down 게임을 시작합니다." << endl;
    while (1) {
        cout << "답은 " << low << "과 " << high << "사이에 있습니다." << endl;
        cout << name << ">>";
        cin >> number;
        if (number == result) {
            cout << name << "이(가) 이겼습니다!!" << endl;
            break;
        }
        number > result ? high = number : low = number;
        name = p.getName();
    }
}
 
int main() {
    UpAndDownGame user;
    user.playGame();
}
cs


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
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;
 
class Book {
    char *title;
    int price;
public:
    Book(const char* title, int price) {
        this->price = price;
        int size = strlen(title);
        this->title = new char[size + 1];
        strcpy(this->title, title);
    }
    Book(Book &b) {
        this->price = b.price;
        int size = strlen(b.title);
        this->title = new char[size + 1];
        strcpy(this->title, b.title);
    }
    ~Book() { delete[]title; }
    /* (2) 디폴트 복사생성자 
    Book(Book &b){
        this->title = title;
        this->price = price;
    } */
    void set(const char* title, int price) {
        this->price = price;
        int size = strlen(title);
        this->title = new char[size + 1];
        strcpy(this->title, title);
    }
    void show() { cout << title << ' ' << price << "원" << endl; }
};
 
int main() {
    Book cpp("명품 C++"10000);
    Book java = cpp;
    java.set("명품자바"12000);
    cpp.show();
    java.show();
}
cs


+ Recent posts