Skip to content

Commit 4ce246d

Browse files
committed
VS2022 version
1 parent f50af48 commit 4ce246d

File tree

81 files changed

+5250
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+5250
-31
lines changed

Ex02_DataTypes/main.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
// 변수를 정의할 때 자료형을 미리 지정해야 합니다.
12+
// 자료형은 바꿀 수 없습니다.
13+
14+
// 내부적으로 메모리를 이미 갖고 있습니다.
15+
int i; // 변수 정의
16+
i = 123; // 변수에 값 지정 (객체 레퍼런스 아님)
17+
18+
// sizeof 소개
19+
cout << i << " " << sizeof(i) << endl; // 추측해보세요.
20+
21+
float f = 123.456f; // 마지막 f 주의
22+
double d = 123.456; // f 불필요
23+
24+
cout << f << " " << sizeof(f) << endl; // 123.456 4
25+
cout << d << " " << sizeof(d) << endl; // 123.456 8
26+
27+
// C++는 글자 하나와 문자열을 구분합니다.
28+
char c = 'a';
29+
30+
cout << c << " " << sizeof(c) << endl; // a 1
31+
32+
// 그 외에도 다양한 자료형이 존재합니다.
33+
34+
// 형변환
35+
i = 987.654; // double을 int에 강제로 저장
36+
37+
cout << "int from double " << i << endl; // 추측해보세요.
38+
39+
f = 567.89; // 이것도 형변환
40+
41+
// 기본 연산자
42+
43+
// i = 987;
44+
i += 100; // i = i + 100;
45+
i++; // i = i + 1;
46+
47+
cout << i << endl; // 추측해보세요.
48+
49+
// 불리언
50+
bool is_good = true;
51+
is_good = false;
52+
53+
cout << is_good << endl; // 0
54+
55+
cout << boolalpha << true << endl; // true
56+
cout << is_good << endl; // false
57+
cout << noboolalpha << true << endl; // true
58+
59+
// 논리 연산 몇 가지 소개 (참고 문서 사용)
60+
// https://en.cppreference.com/w/cpp/language/operator_precedence
61+
62+
cout << boolalpha;
63+
cout << (true && true) << endl; // 추측해보세요.
64+
cout << (true || false) << endl; // 추측해보세요.
65+
66+
// 비교
67+
68+
cout << (1 > 3) << endl;
69+
cout << (3 == 3) << endl;
70+
cout << (i >= 3) << endl;
71+
cout << ('a' != 'c') << endl;
72+
cout << ('a' != 'c') << endl;
73+
74+
// 영역(scope)
75+
76+
i = 123; // 더 넓은 영역
77+
78+
{
79+
int i = 345; // <- 더 좁은 영역의 다른 변수
80+
cout << i << endl; // 추측해보세요.
81+
}
82+
83+
cout << i << endl; // 추측해보세요.
84+
85+
return 0;
86+
}

Ex03_Array/main.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
int a = 1;
12+
int b = 2;
13+
int c = 3;
14+
// ...
15+
16+
// 같은 자료형의 데이터를 저장하기 위해 메모리를 미리 잡아놓은 것
17+
int my_array[3] = {1, 2, 3}; // 초기화할 때는 {} 안에 값 나열
18+
19+
// 인덱싱 (zero-based)
20+
cout << my_array[0] << " "
21+
<< my_array[1] << " "
22+
<< my_array[2] << endl; // 추측해보세요
23+
24+
// 인덱싱으로 하나 짜리 변수 처럼 사용 가능
25+
my_array[1] = 5;
26+
27+
cout << my_array[0] << " "
28+
<< my_array[1] << " "
29+
<< my_array[2] << endl; // 추측해보세요
30+
31+
// cout << my_array[10000] << endl;
32+
33+
// 문자열은 기본적으로 문자의 배열
34+
char name[] = "Hello, World!"; // 문자''와 문자열"" 구분
35+
// Null character '\0'
36+
cout << name << " " << sizeof(name) << endl; // 추측해보세요
37+
38+
name[0] = 'A';
39+
name[1] = 'B';
40+
name[2] = 'C';
41+
42+
cout << name << endl; // 추측해보세요
43+
44+
name[2] = '\0'; // 추측해보세요
45+
46+
cout << name << endl; // 추측해보세요
47+
48+
return 0;
49+
}

Ex04_InputOutput/main.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
// cin은 데이터를 흘려넣어 보내는 스트림이고
12+
// 그 데이터를 해석하는 것은 자료형
13+
// 자료형에 따라서 알아서 처리해주기 때문에 scanf()보다 편리
14+
15+
char user_input[100];
16+
17+
// cin과 getline의 차이
18+
19+
/*
20+
cout << "원하는 문장을 입력해주세요." << endl;
21+
cout << "입력: ";
22+
23+
// cin >> user_input;
24+
25+
cin.getline(user_input, sizeof(user_input));
26+
27+
cout << "메아리: " << user_input << endl;
28+
*/
29+
30+
int number = -1;
31+
32+
cin >> user_input;
33+
// cin.getline(user_input, sizeof(user_input));
34+
cin.ignore(100, '\n');
35+
36+
cin >> number;
37+
38+
cout << user_input << " " << number << endl;
39+
40+
// 참고: cin.ignore(numeric_limits<streamsize>::max(),'\n')
41+
42+
return 0;
43+
}

Ex06_Iteration/main.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
// For 기본 예제
12+
for (int i = 0; i < 10; i++)
13+
{
14+
cout << i << " ";
15+
}
16+
cout << endl;
17+
18+
// 배열 데이터 출력 연습 문제로 제공
19+
// 힌트: sizeof(my_array)
20+
int my_array[] = {1, 2, 3, 4, 5, 4, 3, 2, 1};
21+
// for (...)
22+
{
23+
// TODO: 완성
24+
}
25+
cout << endl;
26+
27+
// 문자열 출력
28+
char my_string[] = "Hello, World!"; // 배열 크기를 알아서 결정
29+
30+
// 문자열을 한 글자씩 출력하기
31+
// cout << my_string << endl; 사용 X
32+
// 힌트: sizeof(), '\0', break,
33+
34+
// for (...)
35+
{
36+
// TODO: 완성
37+
}
38+
cout << endl;
39+
40+
// while 기본 예제
41+
/*
42+
int i = 0;
43+
while (i < 10)
44+
{
45+
cout << i << " ";
46+
i++; // 무한반복 주의 안내
47+
}
48+
cout << endl;
49+
*/
50+
51+
// 실습 문제
52+
// while (true)
53+
{
54+
// 이 구조에서 똑같이 정수 출력하도록 만들게 하기 (break)
55+
}
56+
57+
// 런타임오류 주의
58+
// while문으로 문자열 한글자씩 출력하기
59+
// 힌트 && logical and
60+
61+
/*
62+
int i = 0;
63+
while (...)
64+
{
65+
// TODO:
66+
}
67+
cout << endl;
68+
*/
69+
70+
return 0;
71+
}

Ex07_NumberGuess/main.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
#include <random> // 난수 생성
7+
8+
using namespace std;
9+
10+
int main()
11+
{
12+
// 난수 생성
13+
// https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
14+
std::random_device rd;
15+
std::mt19937 gen(rd());
16+
std::uniform_int_distribution<> distrib(1, 99); // [1, 99]
17+
18+
int number = distrib(gen);
19+
20+
while (1) // true 대신 숫자 1로 무한 반복도 많이 사용합니다.
21+
{
22+
int guess;
23+
cout << "입력: ";
24+
cin >> guess;
25+
26+
/*if ()
27+
{
28+
}
29+
else if ()
30+
{
31+
}
32+
else
33+
{
34+
}*/
35+
}
36+
37+
// 보충: 하나씩 다 비교하는 방법과 이진 탐색 비교
38+
39+
return 0;
40+
}

Ex08_Pointer/main.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
int a = 123;
12+
13+
// address of 주소 연산자 & ampersand
14+
cout << a << " " << &a << endl;
15+
16+
// 참고: 파이썬 id()와 비교
17+
18+
int *b = &a; // b에 a의 주소 대입
19+
20+
// deference operator (역참조 연산자)
21+
cout << *b << endl;
22+
23+
*b = 567;
24+
25+
cout << a << " " << b << " " << *b << endl; // 추측해보세요
26+
27+
// 포인터 자체의 주소 크기와 자료형의 크기 (주소의 크기는 항상 동일하다.)
28+
double *c = nullptr; // 아무 주소도 가리키고 있지 않다는 의미로 초기화, 0도 많이 사용
29+
30+
cout << sizeof(int) << " " << sizeof(double) << endl; // 4 8
31+
cout << sizeof(int *) << " " << sizeof(double *) << endl; // 추측해보세요
32+
cout << sizeof(b) << " " << sizeof(c) << endl;
33+
34+
// 포인터 연산과 배열
35+
36+
// size_t 안내 (여기서는 주소를 10진수로 변환 용도)
37+
cout << sizeof(size_t) << endl; // 8
38+
cout << size_t(b) << " " << size_t(b + 1) << " " << size_t(b + 2) << endl;
39+
cout << size_t(c) << " " << size_t(c + 1) << " " << size_t(c + 2) << endl;
40+
41+
// sizeof(char) == 1입니다. char* e = 0; e + 15 는 몇일까요?
42+
// 확인도 직접 해보세요
43+
44+
// 문자열, 배열 연결시키기
45+
46+
char my_str[] = {'h', 'e', 'l', 'l', 'o'}; // "Hello"
47+
48+
char *ptr = my_str; // 배열의 이름은 포인터
49+
50+
cout << *(ptr + 3) << endl; // 추측해보세요.
51+
52+
return 0;
53+
}

Ex09_Function/main.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
홍정모 연구소 https://honglab.co.kr/
3+
*/
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
const int kMaxStr = 100; // 전역 변수 소개
10+
11+
// 함수 (선언과 정의 분리 가능하다고 안내)
12+
int Add(int a, int b)
13+
{
14+
return a + b; // 반환값 안내
15+
}
16+
17+
// 반환 자료형이 지정되지 않았음
18+
void Add(int a, int b, int *c)
19+
{
20+
*c = a + b;
21+
}
22+
23+
int main()
24+
{
25+
cout << Add(1, 2) << endl;
26+
27+
int sum;
28+
Add(4, 5, &sum);
29+
30+
cout << sum << endl;
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)