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
#include <iostream>
#include <string>
using namespace std;
 
class Histogram {
    string sentence;
public:
    Histogram(string sentence) { this->sentence = sentence; }
    void put(string s) { sentence += s; }
    void putc(char c) { sentence += c; }
    void print();
};
 
void Histogram::print() {
    int count = 0size = sentence.length();
    int alpha[26];
    fill_n(alpha, 260);    // 배열 전체를 0으로 초기화
    cout << sentence << endl << endl;
    for (int i = 0; i < size; i++) {
        if (isalpha(sentence[i])) {
            count++;
        }
    }
    cout << "총 알파벳 수 " << count << endl << endl;
    for (int i = 0; i < size; i++) {
        for (int j = 97; j <= 122; j++) {
            if (tolower(sentence[i]) == (char)j)
                alpha[j - 97]++;
        }
    }
    for (int i = 0; i < 26; i++) {
        cout << (char)(i + 97<< " (" << alpha[i] << ")\t: ";
        for (int j = 0; j < alpha[i]; j++) {
            cout << "*";
        }
        cout << endl;
    }
}
 
int main() {
    Histogram elvisHisto("Wise men say, only fools rush in But I can't help, ");
    elvisHisto.put("falling in love with you");
    elvisHisto.putc('-');
    elvisHisto.put("Elvis Presley");
    elvisHisto.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#define PI 3.141592
using namespace std;
 
class Circle {
    int radius;
    string name;
public:
    void setCircle(string name, int radius) { this->name = name; this->radius = radius; }
    double getArea() { return PI * radius*radius; }
    string getName() { return name; }
};
 
class CircleManager {
    Circle *p;
    int size;
public:
    CircleManager(int size);
    ~CircleManager() { delete[]p; }
    void searchByName();
    void searchByArea();
};
 
CircleManager::CircleManager(int size) {
    string name;
    int radius;
    this->size = size;
    p = new Circle[size];
    for (int i = 0; i < size; i++) {
        cout << "원 " << i + 1 << "의 이름과 반지름 >> ";
        cin >> name;
        cin >> radius;
        p[i].setCircle(name, radius);
    }
}
 
void CircleManager::searchByName() {
    string name;
    cout << "검색하고자 하는 원의 이름 >> ";
    cin >> name;
    for (int i = 0; i < size; i++) {
        if (p[i].getName() == name) {
            cout << p[i].getName() << "의 면적은 " << p[i].getArea() << endl;
        }
    }
}
 
void CircleManager::searchByArea() {
    int area;
    cout << "최소 면적을 정수로 입력하세요 >> ";
    cin >> area;
    cout << area << "보다큰 원을 검색합니다." << endl;
    for (int i = 0; i < size; i++) {
        if (p[i].getArea() > area) {
            cout << p[i].getName() << "의 면적은 " << p[i].getArea() << ",";
        }
    }
    cout << endl;
}
 
int main() {
    int size;
    cout << "원의 개수 >> ";
    cin >> size;
    CircleManager p(size);
    p.searchByName();
    p.searchByArea();
}
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
#include <iostream>
#include <string>
using namespace std;
 
class Person {
    string name;
public:
    Person(){}
    Person(string name) { this->name = name; }
    string getName() { return name; }
    void setName(string name) { this->name = name; }
};
 
class Family {
    Person *p;
    int size;
    string f_name;
public:
    Family(string name, int size) { f_name = name; this->size = size; p = new Person[size]; }
    void setName(int index, string name) { p[index].setName(name); }
    void show();
    ~Family() { delete[]p; }
};
 
void Family::show() {
    cout << f_name << "가족은 다음과 같이 " << size << "명 입니다." << endl;
    for (int i = 0; i < size; i++) {
        cout << p[i].getName() << "\t";
    }
    cout << endl;
}
 
int main() {
    Family *simpson = new Family("Simpson"3);
    simpson->setName(0"Mr. Simpson");
    simpson->setName(1"Mrs. Simpson");
    simpson->setName(2"Bart Simpson");
    simpson->show();
    delete simpson;
}
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
#include <iostream>
#include <string>
using namespace std;
 
class Person {
    string name;
    string tel;
public:
    Person() {}
    string getName() { return name; }
    string getTel() { return tel; }
    void set(string name, string tel) { this->name = name; this->tel = tel; }
};
 
int main() {
    string name, tel;
    Person p[3];
    cout << "이름과 전화 번호를 입력해 주세요" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "사람 " << i + 1 << ">> ";
        getline(cin, name, ' ');
        getline(cin, tel, '\n');
        p[i].set(name, tel);
    }
    cout << "모든 사람의 이름은 ";
    for (int i = 0; i < 3; i++) {
        cout << p[i].getName() << ' ';
    }
    cout << endl;
    cout << "전화번호 검색합니다. 이름을 입력하세요>>";
    cin >> name;
    for (int i = 0; i < 3; i++) {
        if (p[i].getName() == name) {
            cout << "전화 번호는 " << p[i].getTel() << endl;
            break;
        }
    }
}
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
#include <iostream>
#define PI 3.141592
using namespace std;
 
class Circle {
    int radius;
public:
    Circle() {}
    void setRadius(int radius) { this->radius = radius; }
    double getArea() { return PI * radius*radius; }
};
 
int main() {
    int number, radius, count = 0;
    cout << "원의 개수 >> ";
    cin >> number;
    Circle *= new Circle[number];
    for (int i = 0; i < number; i++) {
        cout << "원 " << i + 1 << "의 반지름 >> ";
        cin >> radius;
        r[i].setRadius(radius);
        if (r[i].getArea() > 100) {
            count++;
        }
    }
    cout << "면적이 100보다 큰 원은 " << count << "개 입니다" << endl;
    delete[]r;
}
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
#include <iostream>
#define PI 3.141592
using namespace std;
 
class Circle {
    int radius;
public:
    Circle() {}
    void setRadius(int radius) { this->radius = radius; }
    double getArea() { return PI * radius*radius; }
};
 
int main() {
    int radius, count = 0;
    Circle r[3];
    for (int i = 0; i < 3; i++) {
        cout << "원 " << i + 1 << "의 반지름 >> ";
        cin >> radius;
        r[i].setRadius(radius);
        if (r[i].getArea() > 100) {
            count++;
        }
    }
    cout << "면적이 100보다 큰 원은 " << count << "개 입니다" << 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
#include <iostream>
#include <string>
using namespace std;
 
int main() {
    string line;
    cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
    while (1) {
        cout << ">>";
        getline(cin, line);
        if (line == "exit") {
            break;
        }
        int size = line.length();
        int h_size = line.length() / 2;
        for (int i = 0; i < h_size; i++) {
            char tmp = line[size - 1 - i];
            line[size - 1 - i] = line[i];
            line[i] = tmp;
        }
        cout << line << 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int main() {
    string line;
    cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
    while (1) {
        cout << ">>";
        getline(cin, line);
        if (line == "exit") {
            break;
        }
        srand((unsigned)time(0));
        int size = line.length();
        int r = rand() % size;
        int random = rand();        // char c 대소문자 구분을 위한 난수
        char c;
        while (1) {
            if (isalpha(line[r])) {        // 선택한 문자가 영문자인지 확인
                if (random % 2 == 0) {    // 난수가 짝수면
                    c = (char)(rand() % 26 + 65);    // c 대문자
                    while (1) {
                        if (line[r] == c) {        // 랜덤하게 선택한 문자가 랜덤문자와 같다면
                            c = (char)(rand() % 26 + 65);    // 다시 랜덤문자 생성
                        }
                        else {                    // 랜덤하게 선택한 문자가 랜덤문자와 다르다면
                            break;                            // break
                        }
                    }
                }
                else {    // 난수가 홀수면
                    c = (char)(rand() % 26 + 97);    // c 소문자
                    while (1) {
                        if (line[r] == c) {        // 랜덤하게 선택한 문자가 랜덤문자와 같다면
                            c = (char)(rand() % 26 + 97);    // 다시 랜덤문자 생성
                        }
                        else {                // 랜덤하게 선택한 문자가 랜덤문자와 다르다면
                            break;                            // break
                        }
                    }
                }
                line[r] = c;    // 최종 c값 대입
                break;
            }
            else {                    // 선택문자가 영문자가 아니면
                r = rand() % size;    // 영문자 다시 찾으러가자~
                continue;
            }
        }
        cout << line << 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
#include <iostream>
using namespace std;
 
class Sample {
    int *p;
    int size;
public:
    Sample(int n) {
        size = n; p = new int[n];
    }
    void read();
    void write();
    int big();
    ~Sample();
};
 
void Sample::read() {
    for (int i = 0; i < size; i++) {
        cin >> p[i];
    }
}
 
void Sample::write() {
    for (int i = 0; i < size; i++) {
        cout << p[i] << ' ';
    }
    cout << endl;
}
 
int Sample::big() {
    int max = p[0];
    for (int i = 1; i < size; i++) {
        if (max < p[i]) {
            max = p[i];
        }
    }
    return max;
}
 
Sample::~Sample() {
    if (p) {
        delete[]p;
    }
}
 
int main() {
    Sample s(10);
    s.read();
    s.write();
    cout << "가장 큰 수는 " << s.big() << 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
#include <iostream>
using namespace std;
 
class Color {
    int red, green, blue;
public:
    Color() { red = green = blue = 0; }
    Color(int r, int g, int b) { red = r; green = g; blue = b; }
    void setColor(int r, int g, int b) { red = r; green = g; blue = b; }
    void show() { cout << red << ' ' << green << ' ' << blue << endl; }
};
 
int main() {
    Color screenColor(25500);
    Color *p;
    p = &screenColor;
    p->show();
    Color Colors[3];
    p = Colors;
 
    p[0].setColor(25500);
    p[1].setColor(02550);
    p[2].setColor(00255);
 
    for (int i = 0; i < 3; i++) {
        p[i].show();
    }
}
cs

+ Recent posts