├── .gitignore ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── .vscode_gcc ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── .vscode_mac ├── c_cpp_properties.json ├── launch.json └── tasks.json ├── .vscode_msvc ├── c_cpp_properties.json ├── launch.json └── tasks.json ├── .vscode_wsl(ubuntu) ├── c_cpp_properties.json ├── launch.json └── tasks.json ├── Ex01_HelloWorld └── main.cpp ├── Ex02_DataTypes └── main.cpp ├── Ex03_Array └── main.cpp ├── Ex04_InputOutput └── main.cpp ├── Ex05_Branching └── main.cpp ├── Ex06_Iteration ├── exercises.md └── main.cpp ├── Ex07_NumberGuess └── main.cpp ├── Ex08_Pointer └── main.cpp ├── Ex09_Function └── main.cpp ├── Ex10_StringMatching └── main.cpp ├── Ex11_DynamicAllocation └── main.cpp ├── Ex12_Structure └── main.cpp ├── Ex13_Class └── main.cpp ├── Ex14_StringClass └── main.cpp ├── Ex15_Header ├── MyClass.cpp ├── MyClass.h └── main.cpp ├── Ex16_FileIO └── main.cpp ├── Ex17_PhoneBook ├── PhoneBook ├── PhoneBook.cpp ├── PhoneBook.h └── main.cpp ├── Ex18_Template └── main.cpp ├── README.md ├── VS2022 ├── Ex01_HelloWorld │ ├── Ex01_HelloWorld.vcxproj │ ├── Ex01_HelloWorld.vcxproj.filters │ └── main.cpp ├── Ex02_DataTypes │ ├── Ex02_DataTypes.vcxproj │ ├── Ex02_DataTypes.vcxproj.filters │ └── main.cpp ├── Ex03_Array │ ├── Ex03_Array.vcxproj │ ├── Ex03_Array.vcxproj.filters │ └── main.cpp ├── Ex04_InputOutput │ ├── Ex04_InputOutput.vcxproj │ ├── Ex04_InputOutput.vcxproj.filters │ └── main.cpp ├── Ex05_Branching │ ├── Ex05_Branching.vcxproj │ ├── Ex05_Branching.vcxproj.filters │ └── main.cpp ├── Ex06_Iteration │ ├── Ex06_Iteration.vcxproj │ ├── Ex06_Iteration.vcxproj.filters │ └── main.cpp ├── Ex07_NumberGuess │ ├── Ex07_NumberGuess.vcxproj │ ├── Ex07_NumberGuess.vcxproj.filters │ └── main.cpp ├── Ex08_Pointer │ ├── Ex08_Pointer.vcxproj │ ├── Ex08_Pointer.vcxproj.filters │ └── main.cpp ├── Ex09_Function │ ├── Ex09_Function.vcxproj │ ├── Ex09_Function.vcxproj.filters │ └── main.cpp ├── Ex10_StringMatching │ ├── Ex10_StringMatching.vcxproj │ ├── Ex10_StringMatching.vcxproj.filters │ └── main.cpp ├── Ex11_DynamicAllocation │ ├── Ex11_DynamicAllocation.vcxproj │ ├── Ex11_DynamicAllocation.vcxproj.filters │ └── main.cpp ├── Ex12_Structure │ ├── Ex12_Structure.vcxproj │ ├── Ex12_Structure.vcxproj.filters │ └── main.cpp ├── Ex13_Class │ ├── Ex13_Class.vcxproj │ ├── Ex13_Class.vcxproj.filters │ └── main.cpp ├── Ex14_StringClass │ ├── Ex14_StringClass.vcxproj │ ├── Ex14_StringClass.vcxproj.filters │ └── main.cpp ├── Ex15_Header │ ├── Ex15_Header.vcxproj │ ├── Ex15_Header.vcxproj.filters │ ├── MyClass.cpp │ ├── MyClass.h │ └── main.cpp ├── Ex16_FileIO │ ├── Ex16_FileIO.vcxproj │ ├── Ex16_FileIO.vcxproj.filters │ └── main.cpp ├── Ex17_PhoneBook │ ├── Ex17_PhoneBook.vcxproj │ ├── Ex17_PhoneBook.vcxproj.filters │ ├── PhoneBook.cpp │ ├── PhoneBook.h │ └── main.cpp ├── Ex18_Template │ ├── Ex18_Template.vcxproj │ ├── Ex18_Template.vcxproj.filters │ └── main.cpp └── HongLabCppSummary.sln └── english ├── .vscode ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── .vscode_gcc ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── .vscode_msvc ├── c_cpp_properties.json ├── launch.json └── tasks.json ├── .vscode_wsl(ubuntu) ├── c_cpp_properties.json ├── launch.json └── tasks.json ├── Ex01_HelloWorld └── main.cpp ├── Ex02_DataTypes └── main.cpp ├── Ex03_Array └── main.cpp ├── Ex04_InputOutput └── main.cpp ├── Ex05_Branching └── main.cpp ├── Ex06_Iteration └── main.cpp ├── Ex07_NumberGuess └── main.cpp ├── Ex08_Pointer └── main.cpp ├── Ex09_Function └── main.cpp ├── Ex10_StringMatching └── main.cpp ├── Ex11_DynamicAllocation └── main.cpp ├── Ex12_Structure └── main.cpp ├── Ex13_Class └── main.cpp ├── Ex14_StringClass └── main.cpp ├── Ex15_Header ├── MyClass.cpp ├── MyClass.h └── main.cpp ├── Ex16_FileIO └── main.cpp ├── Ex17_PhoneBook ├── PhoneBook.cpp ├── PhoneBook.h └── main.cpp └── Ex18_Template └── main.cpp /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++ build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}/${fileBasenameNoExtension}", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "setupCommands": [ 15 | { 16 | "description": "Enable pretty-printing for gdb", 17 | "text": "-enable-pretty-printing", 18 | "ignoreFailures": true 19 | }, 20 | { 21 | "description": "Set Disassembly Flavor to Intel", 22 | "text": "-gdb-set disassembly-flavor intel", 23 | "ignoreFailures": true 24 | } 25 | ], 26 | "preLaunchTask": "C/C++: g++ build active file", 27 | "miDebuggerPath": "/usr/bin/gdb" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${fileDirname}/*.cpp", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /.vscode_gcc/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${vcpkgRoot}/x64-windows/include" 8 | ], 9 | "defines": [ 10 | "_DEBUG", 11 | "UNICODE", 12 | "_UNICODE" 13 | ], 14 | "windowsSdkVersion": "10.0.22000.0", 15 | "compilerPath": "C:/msys64/mingw64/bin/g++.exe", 16 | "cStandard": "c17", 17 | "cppStandard": "c++17", 18 | "intelliSenseMode": "windows-gcc-x64" 19 | } 20 | ], 21 | "version": 4 22 | } -------------------------------------------------------------------------------- /.vscode_gcc/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++.exe build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "C:/msys64/mingw64/bin", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | }, 21 | { 22 | "description": "Set Disassembly Flavor to Intel", 23 | "text": "-gdb-set disassembly-flavor intel", 24 | "ignoreFailures": true 25 | } 26 | ], 27 | "preLaunchTask": "C/C++: g++.exe build active file" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /.vscode_gcc/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "typeinfo": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /.vscode_gcc/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++.exe build active file", 6 | "command": "C:/msys64/mingw64/bin/g++.exe", 7 | "args": [ 8 | "-finput-charset=UTF-8", 9 | "-fdiagnostics-color=always", 10 | "-g", 11 | "-Wall", 12 | "${fileDirname}/*.cpp", 13 | "-o", 14 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 15 | ], 16 | "options": { 17 | "cwd": "C:/msys64/mingw64/bin" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /.vscode_mac/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Mac", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "macFrameworkPath": [ 10 | "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" 11 | ], 12 | "compilerPath": "/usr/bin/clang++", 13 | "cStandard": "c17", 14 | "cppStandard": "c++17", 15 | "intelliSenseMode": "macos-clang-arm64" 16 | } 17 | ], 18 | "version": 4 19 | } -------------------------------------------------------------------------------- /.vscode_mac/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: clang++ build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}/${fileBasenameNoExtension}", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "lldb", 14 | "preLaunchTask": "C/C++: clang++ build active file" 15 | } 16 | ], 17 | "version": "2.0.0" 18 | } -------------------------------------------------------------------------------- /.vscode_mac/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++17", 9 | "-finput-charset=UTF-8", 10 | "-fcolor-diagnostics", 11 | "-fansi-escape-codes", 12 | "-g", 13 | "${fileDirname}/*.cpp", 14 | "-o", 15 | "${fileDirname}/${fileBasenameNoExtension}" 16 | ], 17 | "options": { 18 | "cwd": "${fileDirname}" 19 | }, 20 | "problemMatcher": [ 21 | "$gcc" 22 | ], 23 | "group": { 24 | "kind": "build", 25 | "isDefault": true 26 | }, 27 | "detail": "Task generated by Debugger." 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /.vscode_msvc/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${vcpkgRoot}/x64-windows/include" 8 | ], 9 | "defines": [ 10 | "_DEBUG", 11 | "UNICODE", 12 | "_UNICODE" 13 | ], 14 | "windowsSdkVersion": "10.0.22000.0", 15 | "compilerPath": "cl.exe", 16 | "cStandard": "c17", 17 | "cppStandard": "c++17", 18 | "intelliSenseMode": "windows-msvc-x64" 19 | } 20 | ], 21 | "version": 4 22 | } -------------------------------------------------------------------------------- /.vscode_msvc/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: cl.exe build and debug active file", 5 | "type": "cppvsdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "console": "integratedTerminal", 13 | "preLaunchTask": "C/C++: cl.exe 활성 파일 빌드" 14 | } 15 | ], 16 | "version": "2.0.0" 17 | } -------------------------------------------------------------------------------- /.vscode_msvc/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: cl.exe 활성 파일 빌드", 6 | "command": "cl.exe", 7 | "args": [ 8 | "/Zi", 9 | "/EHsc", 10 | "/nologo", 11 | "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe", 12 | "${fileDirname}/*.cpp", 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$msCompile" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "디버거에서 생성된 작업입니다." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /.vscode_wsl(ubuntu)/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c17", 11 | "cppStandard": "gnu++14", 12 | "intelliSenseMode": "linux-gcc-x64" 13 | } 14 | ], 15 | "version": 4 16 | } -------------------------------------------------------------------------------- /.vscode_wsl(ubuntu)/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++ build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}/${fileBasenameNoExtension}", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "setupCommands": [ 15 | { 16 | "description": "Enable pretty-printing for gdb", 17 | "text": "-enable-pretty-printing", 18 | "ignoreFailures": true 19 | }, 20 | { 21 | "description": "Set Disassembly Flavor to Intel", 22 | "text": "-gdb-set disassembly-flavor intel", 23 | "ignoreFailures": true 24 | } 25 | ], 26 | "preLaunchTask": "C/C++: g++ build active file", 27 | "miDebuggerPath": "/usr/bin/gdb" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /.vscode_wsl(ubuntu)/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${fileDirname}/*.cpp", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /Ex01_HelloWorld/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include // iostream이라는 헤더를 포함(include) 6 | 7 | using namespace std; // 네임스페이스 설명 std::cout 8 | 9 | int main() // entry point 10 | { 11 | // 주석(comment) 다는 방법 12 | 13 | cout << "Hello, World!" << endl; 14 | // printf("Hello World!!! by printf"); 15 | 16 | // 입출력에 대해서는 뒤에 다시 나와요. 17 | char user_input[100]; 18 | cin >> user_input; 19 | cout << user_input; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ex02_DataTypes/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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; // 1 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' != 'a') << 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 | } 87 | -------------------------------------------------------------------------------- /Ex03_Array/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 50 | -------------------------------------------------------------------------------- /Ex04_InputOutput/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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::max(),'\n') 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Ex05_Branching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // 0이 아니면 true다 안내 12 | // 나머지 연산자 안내 13 | // 줄 바꿈 '\n' newline 14 | // 블럭 내용이 한 줄일 경우에는 {} 생략 가능 15 | 16 | int number; 17 | 18 | cin >> number; 19 | 20 | if (number % 2 == 0) 21 | cout << "짝수입니다.\n"; 22 | else 23 | cout << "홀수입니다.\n"; 24 | 25 | // 조건 연산자 (삼항 연산자) 26 | cout << (number % 2 == 0 ? "짝수입니다." : "홀수입니다.") << endl; 27 | 28 | // switch - case 29 | 30 | switch (number) 31 | { 32 | case 0: 33 | cout << "정수 0입니다." << endl; 34 | break; // 주의 35 | case 1: 36 | cout << "정수 1입니다." << endl; 37 | break; 38 | default: 39 | cout << "그 외의 숫자입니다." << endl; 40 | break; // 마지막은 생략 가능 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Ex06_Iteration/exercises.md: -------------------------------------------------------------------------------- 1 | ___ 2 |

3 | ___ 4 |
Content Copyright by HongLab, Inc.
5 | 6 | ## [파이썬 추월코스](https://honglab.co.kr/courses/python) 연습문제 중 일부를 발췌한 것입니다. 7 | 8 | 구조체, 클래스, 포인터 등을 사용하지 **않고** 간단하게 풀어볼 수 있습니다. 9 | 10 | 정답이나 풀이는 제공하지 않습니다. 11 | 12 | ##### [문제] 평균 점수 출력하기 13 | 14 | 다음 숫자들의 평균을 계산해서 출력해보세요. 15 | 정답: ```59.8``` (float로 변환해서 계산) 16 | 17 | int scores[] = {20, 40, 80, 60, 99}; 18 | 19 | ##### [문제] 제곱의 합 20 | 21 | for문을 이용해서 1부터 100까지 숫자들의 제곱의 합을 구해보세요. 22 | $1^2 + 2^2 + \cdots + 100^2$를 구하면 됩니다. 23 | | 입력 | 출력 | 24 | | ---- | ------ | 25 | | 없음 | 338350 | 26 | 27 | ##### [문제] 계절 판별하기 28 | 1 이상 12 이하의 정수를 입력받아서 계절을 출력해봅시다. 29 | 30 | | 계절 | 월 | 31 | | ---- | --------- | 32 | | 봄 | 3, 4, 5 | 33 | | 여름 | 6, 7, 8 | 34 | | 가을 | 9, 10, 11 | 35 | | 겨울 | 12, 1, 2 | 36 | 37 | 결과 예시: 3을 입력했을 경우 "3월은 봄입니다." 출력 38 | 39 | ##### [문제] 구구단 출력 40 | 41 | 원하는 2이상 9이하의 정수에 대해 아래와 같이 곱셈표를 출력하는 프로그램을 만들어 보세요.아래는 3을 입력했을 때 3단이 출력되는 예시입니다. 42 | ``` 43 | 3 x 1 = 3 44 | 3 x 2 = 6 45 | 3 x 3 = 9 46 | 3 x 4 = 12 47 | 3 x 5 = 15 48 | 3 x 6 = 18 49 | 3 x 7 = 21 50 | 3 x 8 = 24 51 | 3 x 9 = 27 52 | ``` 53 | 54 | ##### [문제] 회문(Palindrome) 검사 55 | 56 | level, madam과 같이 순서를 글자 단위로 뒤집었을 때도 동일한 단어들을 회문(palindrome)이라고 부릅니다. 입력받은 문자열이 회문인지 검사하는 프로그램을 작성해보세요. 57 | 58 | | 입력 | 출력 | 59 | | ----- | ----- | 60 | | level | True | 61 | | abc | False | 62 | | refer | True | 63 | 64 | ##### [문제] [플로이드의 삼각형](https://ko.wikipedia.org/wiki/%ED%94%8C%EB%A1%9C%EC%9D%B4%EB%93%9C%EC%9D%98_%EC%82%BC%EA%B0%81%ED%98%95) 65 | 66 | 별찍기와 유사한 문제입니다. 아래와 같이 숫자가 증가하는 삼각형을 출력해보세요. 67 | ``` 68 | 1 69 | 2 3 70 | 4 5 6 71 | 7 8 9 10 72 | 11 12 13 14 15 73 | ``` 74 | 75 | -------------------------------------------------------------------------------- /Ex06_Iteration/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 72 | -------------------------------------------------------------------------------- /Ex07_NumberGuess/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include // 난수 생성 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 | } 41 | -------------------------------------------------------------------------------- /Ex08_Pointer/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 54 | -------------------------------------------------------------------------------- /Ex09_Function/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | // 반환 자료형이 지정되지 않았음 (void) 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 | } 34 | -------------------------------------------------------------------------------- /Ex10_StringMatching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int kMaxStr = 100; // 전역 상수 소개 10 | 11 | // 문자열을 매개변수로 넣기 12 | // 여기서는 모든 문자열 배열의 길이가 동일하다고 가정 13 | bool IsEqual(const char str1[], const char str2[]) 14 | { 15 | // 크기 출력 확인 (배열 크기가 아님 주의!) - 문자열의 길이를 별도로 저장해야 합니다! 16 | // cout << sizeof(str1) << " " << sizeof(str2) << " " << endl; 17 | // exit(-1); 18 | 19 | // 힌트: ==, != 같지 않다 비교 연산자 20 | // 힌트: 문자열 종료 조건 21 | // 디버깅 힌트: 문자를 정수로 바꿔서 출력해보기 22 | 23 | return true; 24 | } 25 | 26 | int main() 27 | { 28 | // 영어 사용이 디버깅에 유리합니다. 29 | const char str1[kMaxStr] = "stop"; 30 | 31 | while (1) 32 | { 33 | // TODO: 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Ex11_DynamicAllocation/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int kMaxStr = 100; // 전역 상수 11 | 12 | int Min(int a, int b) 13 | { 14 | return a < b ? a : b; // 조건연산자(삼항연산자) 15 | } 16 | 17 | int main() 18 | { 19 | // 문자열 복사 20 | char str1[] = "Hello, World!"; 21 | char str2[kMaxStr]; 22 | 23 | // dest, src 안내 (복사할 메모리 크기 주의) 24 | memcpy(str2, str1, Min(sizeof(str1), sizeof(str2))); 25 | cout << str2 << endl; 26 | 27 | char *dynamic_array = new char[kMaxStr]; 28 | 29 | // 주의: 동적할당 메모리는 변수 사이즈가 포인터 사이즈임 (배열이 아님) 30 | memcpy(dynamic_array, str1, Min(sizeof(str1), sizeof(kMaxStr))); 31 | // memcpy(dynamic_array, str2, kMaxStr); 32 | cout << dynamic_array << endl; 33 | 34 | cout << str1 << " " << str2 << " " << dynamic_array << endl; 35 | cout << size_t(str1) << " " << size_t(str2) << " " << size_t(dynamic_array) << endl; 36 | 37 | // 보통 크기를 별도로 저장함 38 | 39 | delete[] dynamic_array; // 배열 삭제시 [] 40 | 41 | // 지우지 않고 재할당할 경우 잃어버림 42 | // dynamic_array = new char[원하는크기]; 43 | // delete[] dynamic_array; 다시 지워줘야 함 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Ex12_Structure/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | struct MyStruct 11 | { 12 | int first; 13 | int second; 14 | // ... 추가 가능 15 | 16 | int Sum() 17 | { 18 | return first + second; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | // member(.) operator 25 | MyStruct a; 26 | a.first = 123; 27 | a.second = 456; 28 | 29 | cout << sizeof(a) << endl; 30 | 31 | // 포인터는 member(->) operator가 화살표 32 | MyStruct *ptr_a = &a; 33 | 34 | cout << ptr_a->first << " " << ptr_a->second << " " << ptr_a->Sum() << endl; 35 | 36 | // 배열도 가능 37 | MyStruct pairs[10]; 38 | 39 | for (int i = 0; i < 10; i++) 40 | { 41 | // pairs->first = i; // 주의 42 | // pairs->second = i * 10; 43 | 44 | pairs[i].first = i; 45 | pairs[i].second = i * 10; 46 | } 47 | 48 | for (int i = 0; i < 10; i++) 49 | { 50 | cout << pairs[i].Sum() << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Ex13_Class/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 확인 11 | 12 | class MyClass 13 | { 14 | public: 15 | MyClass() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyClass()" << endl; 19 | } 20 | 21 | MyClass(int number) 22 | { 23 | cout << "MyClass(int number)" << endl; 24 | 25 | // this pointer 소개 26 | this->number_ = number; 27 | } 28 | 29 | ~MyClass() 30 | { 31 | // 호출 시점 확인 32 | cout << "~MyClass()" << endl; 33 | } 34 | 35 | void Increment(int a) 36 | { 37 | number_ += a; 38 | } 39 | 40 | void Print() 41 | { 42 | cout << number_ << endl; 43 | } 44 | 45 | private: 46 | int number_ = 0; // 초기값 47 | }; 48 | 49 | int main() 50 | { 51 | MyClass my_class1; 52 | MyClass my_class2(123); 53 | 54 | my_class1.Print(); 55 | my_class2.Print(); 56 | 57 | my_class1.Increment(1); 58 | my_class1.Print(); 59 | 60 | // 배열 사용 가능 61 | // 포인터 사용 가능 등 안내 62 | // 기본 자료형과 비교 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Ex14_StringClass/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 안내 11 | 12 | class MyString 13 | { 14 | public: 15 | MyString() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyString()" << endl; 19 | 20 | size_ = 1; 21 | str_ = new char[size_]; 22 | } 23 | 24 | MyString(const char *init_str) // init_str이 유효한 메모리라고 가정 25 | { 26 | cout << "MyString(const char *init_str)" << endl; 27 | 28 | // 1. 글자 수 먼저 확인 29 | size_ = 0; 30 | while (init_str[size_] != '\0') 31 | { 32 | size_++; // ++ 연산자 안내 33 | } 34 | 35 | // 2. 글자 수가 0이 아니면 메모리 할당 36 | if (size_ > 0) 37 | { 38 | str_ = new char[size_]; 39 | } 40 | 41 | // 3. 복사 42 | for (int i = 0; i < size_; i++) 43 | { 44 | str_[i] = init_str[i]; 45 | } 46 | // memcpy() 사용 가능 47 | } 48 | 49 | ~MyString() 50 | { 51 | // 호출 시점 확인 52 | cout << "Destructor" << endl; 53 | 54 | size_ = 0; 55 | if (str_) 56 | delete[] str_; 57 | } 58 | 59 | void Resize(int new_size) 60 | { 61 | char *new_str = new char[new_size]; 62 | 63 | // memcpy() 사용 가능 64 | for (int i = 0; i < (new_size < size_ ? new_size : size_); i++) 65 | { 66 | new_str[i] = str_[i]; 67 | } 68 | 69 | delete[] str_; 70 | str_ = new_str; 71 | size_ = new_size; 72 | 73 | // new_str 지우면 안되요! 74 | } 75 | 76 | void Print() 77 | { 78 | for (int i = 0; i < size_; i++) 79 | { 80 | cout << str_[i]; 81 | } 82 | cout << endl; 83 | } 84 | 85 | void Append(MyString *app_str) // 같은 타입을 매개변수로 사용 가능 86 | { 87 | int old_size = size_; 88 | 89 | // 다른 멤버 함수 호출 가능 90 | // Resize(...); 91 | 92 | // 중요한 개념 93 | for (int i = old_size; i < size_; i++) 94 | { 95 | // TODO: 복사 96 | } 97 | } 98 | 99 | private: 100 | int size_ = 0; // m_size 101 | char *str_ = nullptr; // m_str (여기서는 구글 스타일) 102 | }; 103 | 104 | int main() 105 | { 106 | // 클래스 기본 문법 안내 107 | 108 | MyString str1("ABCDE"); // 생성자 이용 109 | MyString str2("123"); 110 | 111 | str1.Append(&str2); // 주소 넣어줌 112 | 113 | str1.Print(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /Ex15_Header/MyClass.cpp: -------------------------------------------------------------------------------- 1 | #include "MyClass.h" // 선언 헤더 꼭 include 2 | 3 | #include 4 | 5 | using namespace std; // 보통 .h 파일 대신 .cpp에 사용 6 | 7 | MyClass::MyClass() 8 | { 9 | // 호출 시점 확인 10 | cout << "MyClass()" << endl; 11 | } 12 | 13 | MyClass::MyClass(int number) // init_str이 유효한 메모리라고 가정 14 | { 15 | cout << "MyClass(int number)" << endl; 16 | 17 | // this pointer 소개 18 | this->number_ = number; 19 | } 20 | 21 | MyClass::~MyClass() 22 | { 23 | // 호출 시점 확인 24 | cout << "~MyClass()" << endl; 25 | } 26 | 27 | void MyClass::Increment(int a) 28 | { 29 | number_ += a; 30 | } 31 | 32 | void MyClass::Print() 33 | { 34 | cout << number_ << endl; 35 | } -------------------------------------------------------------------------------- /Ex15_Header/MyClass.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_CLASS_H 2 | #define MY_CLASS_H 3 | 4 | // TODO: 새로운 멤버 함수 추가해보기 연습 5 | 6 | class MyClass 7 | { 8 | public: 9 | MyClass(); // 멤버 함수의 몸체(body) 모두 삭제, 깔끔 10 | MyClass(int number); 11 | ~MyClass(); 12 | 13 | void Increment(int a); 14 | // void Decrement(int a); // 구현 실습 15 | void Print(); 16 | 17 | private: 18 | int number_ = 0; // 초기값 19 | }; 20 | 21 | #endif // MY_CLASS_H -------------------------------------------------------------------------------- /Ex15_Header/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "MyClass.h" // <- 설명 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | MyClass my_class1; 15 | MyClass my_class2(123); 16 | 17 | my_class1.Print(); 18 | my_class2.Print(); 19 | 20 | my_class1.Increment(1); 21 | my_class1.Print(); 22 | 23 | // 배열 사용 가능 24 | // 포인터 사용 가능 등 안내 25 | // 기본 자료형과 비교 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Ex16_FileIO/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | // 파일 출력 13 | /*{ 14 | ofstream ofile; 15 | 16 | ofile.open("my_contacts.txt"); 17 | ofile << "안녕하세요? 반갑습니다.\n"; 18 | ofile << "두 번째 줄입니다.\n"; 19 | ofile << "세 번째 줄입니다.\n"; 20 | ofile.close(); 21 | }*/ 22 | 23 | // 파일 입력 24 | { 25 | char line[100]; 26 | 27 | ifstream ifile; 28 | ifile.open("my_contacts.txt"); 29 | 30 | int line_number = 0; 31 | while (ifile.getline(line, sizeof(line))) 32 | { 33 | cout << line_number << " : "; 34 | cout << line << endl; 35 | 36 | line_number += 1; 37 | } 38 | 39 | ifile.close(); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Ex17_PhoneBook/PhoneBook: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HongLabInc/HongLabCppSummary/11ebc18c3a92b87dada48409689341e803027fbc/Ex17_PhoneBook/PhoneBook -------------------------------------------------------------------------------- /Ex17_PhoneBook/PhoneBook.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include "PhoneBook.h" 6 | 7 | #include 8 | #include 9 | #include // memcpy(.) 10 | 11 | using namespace std; 12 | 13 | PhoneBook::PhoneBook() 14 | { 15 | contacts_ = new Contact[capacity_]; 16 | } 17 | 18 | PhoneBook::~PhoneBook() 19 | { 20 | if (contacts_) 21 | delete[] contacts_; 22 | } 23 | 24 | bool PhoneBook::IsEmpty() 25 | { 26 | assert(num_contacts_ >= 0); 27 | 28 | if (num_contacts_ == 0) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | bool PhoneBook::IsFull() 35 | { 36 | if (num_contacts_ == capacity_) 37 | return true; 38 | else 39 | return false; 40 | } 41 | 42 | void PhoneBook::PrintAll() 43 | { 44 | for (int i = 0; i < num_contacts_; i++) 45 | { 46 | PrintContact(i); 47 | } 48 | } 49 | 50 | void PhoneBook::PrintContact(int index) 51 | { 52 | cout << index << " "; 53 | cout << contacts_[index].name; 54 | cout << ", " << contacts_[index].phone << endl; 55 | } 56 | 57 | void PhoneBook::AddContact(const char name[], const char phone[]) 58 | { 59 | assert(!IsFull()); // 디버깅할 때 assert 괄호 안쪽 조건이 false가 되면 오류 처리 60 | 61 | memcpy(contacts_[num_contacts_].name, name, sizeof(contacts_[num_contacts_].name)); 62 | memcpy(contacts_[num_contacts_].phone, phone, sizeof(contacts_[num_contacts_].phone)); 63 | 64 | num_contacts_ += 1; 65 | } 66 | 67 | void PhoneBook::AddContact() 68 | { 69 | // capacity_가 고정된 경우 70 | if (IsFull()) 71 | { 72 | cout << "더 이상 추가할 수 없습니다." << endl; 73 | return; 74 | } 75 | 76 | // 더 해볼 것: 메모리를 재할당 받아서 연락처 개수 제한 없애기 77 | 78 | char new_name[kMaxStr]; 79 | char new_phone[kMaxStr]; 80 | 81 | cout << "이름을 입력해주세요 : "; 82 | cin.getline(new_name, sizeof(new_name)); 83 | 84 | cout << "전화번호를 입력해주세요 : "; 85 | cin.getline(new_phone, sizeof(new_phone)); 86 | 87 | AddContact(new_name, new_phone); 88 | } 89 | 90 | int PhoneBook::FindByName() 91 | { 92 | char search_name[kMaxStr]; 93 | 94 | cout << "검색할 이름을 입력해주세요 : "; 95 | cin.getline(search_name, sizeof(search_name)); 96 | 97 | // TODO: IsEqual(), PrintContact(i), return i 98 | 99 | cout << search_name << " 님을 찾지 못했습니다." << endl; 100 | 101 | return -1; 102 | } 103 | 104 | bool PhoneBook::IsEqual(const char str1[], const char str2[]) 105 | { 106 | for (int i = 0; i < kMaxStr; i++) 107 | { 108 | if (str1[i] != str2[i]) 109 | return false; 110 | 111 | if (str1[i] == '\0') 112 | return true; 113 | } 114 | 115 | return true; 116 | } 117 | 118 | void PhoneBook::DeleteByName() 119 | { 120 | // 삭제할 때 메모리를 줄이지는 않는 것으로 할께요. 121 | 122 | int index = FindByName(); 123 | 124 | if (index >= 0) 125 | { 126 | // TODO: 중간에서 삭제했을 경우 데이터 정리 127 | 128 | // TODO: num_contacts_ 하나 감소 129 | 130 | num_contacts_ -= 1; 131 | } 132 | } -------------------------------------------------------------------------------- /Ex17_PhoneBook/PhoneBook.h: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #ifndef PHONE_BOOK_H_ 6 | #define PHONE_BOOK_H_ 7 | 8 | const int kMaxStr = 20; // 문자열의 최대 글자 수 9 | 10 | struct Contact 11 | { 12 | char name[kMaxStr]; 13 | char phone[kMaxStr]; 14 | }; 15 | 16 | class PhoneBook 17 | { 18 | public: 19 | PhoneBook(); 20 | ~PhoneBook(); 21 | 22 | bool IsEmpty(); 23 | bool IsFull(); 24 | void PrintAll(); 25 | void PrintContact(int i); 26 | void AddContact(); 27 | void AddContact(const char name[], const char phone[]); 28 | int FindByName(); 29 | bool IsEqual(const char str1[], const char str2[]); 30 | void DeleteByName(); 31 | 32 | private: 33 | int capacity_ = 3; // 연락처 최대 개수 (변경 가능) 34 | int num_contacts_ = 0; 35 | Contact *contacts_ = nullptr; 36 | }; 37 | 38 | #endif -------------------------------------------------------------------------------- /Ex17_PhoneBook/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "PhoneBook.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | PhoneBook my_phonebook; 15 | 16 | // 초기 데이터 17 | my_phonebook.AddContact("홍정모", "1234-1234"); 18 | my_phonebook.AddContact("둘리", "8585-2324"); 19 | my_phonebook.AddContact("아이언맨", "7432-9897"); 20 | 21 | int menu_number; 22 | 23 | while (true) 24 | { 25 | cout << "1: 모두 출력\n"; 26 | cout << "2: 검색\n"; 27 | cout << "3: 추가\n"; 28 | cout << "4: 삭제\n"; 29 | cout << "X: 종료\n"; 30 | cout << "메뉴를 선택해주세요 : "; 31 | 32 | cin >> menu_number; 33 | cin.ignore(); 34 | 35 | // if-else 대신에 switch 사용 가능 36 | if (menu_number == 1) // 모두 출력 37 | { 38 | my_phonebook.PrintAll(); 39 | } 40 | else if (menu_number == 2) // 검색 41 | { 42 | my_phonebook.FindByName(); 43 | } 44 | else if (menu_number == 3) // 추가 45 | { 46 | my_phonebook.AddContact(); 47 | } 48 | else if (menu_number == 4) // 삭제 49 | { 50 | my_phonebook.DeleteByName(); 51 | } 52 | else 53 | { 54 | cout << "종료합니다." << endl; 55 | break; 56 | } 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | /* 63 | 전화번호부 필요한 기능들 64 | - 모두 출력, 이름으로 찾기(검색) 65 | - 추가, 삭제 66 | 67 | 더 해볼 것들 68 | - 파일 저장 및 읽어들이기 69 | */ 70 | -------------------------------------------------------------------------------- /Ex18_Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class MyInt 7 | { 8 | public: 9 | int data_; 10 | }; 11 | 12 | class MyDouble 13 | { 14 | public: 15 | double data_; 16 | }; 17 | 18 | template 19 | class MyClass 20 | { 21 | public: 22 | T data_; 23 | }; 24 | 25 | int main() 26 | { 27 | MyClass my_int; 28 | MyClass my_double; 29 | 30 | my_int.data_; 31 | my_double.data_; 32 | 33 | cout << sizeof(my_int) << " " << sizeof(my_double) << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [홍정모 연구소](https://honglab.co.kr/) 2 | 3 | - 온라인 강의노트에는 참고자료에 "[홍정모 연구소](https://honglab.co.kr/)"를 꼭 적어주세요. 4 | - 다른 학생들의 공부를 위해서 실습 문제의 정답이나 풀이를 온라인에 공개하지 마세요. 5 | 6 | ## [코테용 C++ 요약 강의](https://honglab.co.kr/courses/cppsummary) FREE 7 | 8 | - [파이썬 추월코스](https://honglab.co.kr/courses/python)로 약간의 프로그래밍 연습을 해보신 분들이 빠르게 C++을 터득하고 자료구조/알고리즘 공부를 시작하실 수 있도록 도와드리는 실습 중심 **요약** 강의입니다. 따배씨++와는 스타일이 완전히 다르니 오해 없으시길 바랍니다. 9 | - 하나하나 다 설명해주는 방식이 아니라 머리를 깨워드리는 강의입니다. 어렵다거나 뭔가 이상하다면 혼자 고민하지 말고 [디스코드](https://discord.gg/kgR9xJkbsV)로 오세요. (주의: 예의 없는 행위시 경고 없이 즉시 영구 퇴장) 10 | - 코테 준비 요령, 강의 목표, 환경 설정 ([유튜브 영상](https://youtu.be/UqCZda8DLGc)) 11 | 12 | ## 예제 사용 방법 13 | - **한글 사용이 어렵다면 VS2022나 enlish 폴더의 예제들을 사용하세요.** 14 | - 한글 입출력이 없는 버전은 english 폴더 아래에 있습니다. 15 | - VS2022: HongLabCppSummary.sln 더블클릭 16 | - VSCode: 해당되는 환경의 .vscode_XXX 폴더를 .vscode로 변경 (강의 영상에 설명 있어요) 17 | 18 | ## 맥 사용 추가 안내 19 | - 2024년 2월 6일 현재 VSCode가 1.86.0로 업데이트 되면서 문제가 생긴 것 같습니다. 20 | - 현재 확인한 가장 간단한 해결책은 "C/C++ Runner v9.4.7 (제작자 franneck94)" 확장을 사용하는 것입니다. 사용법은 확장 자체 설명 영상 보시면 간단합니다. 간단히 요약하면, 바닥의 툴바에서 경로명 클릭하면 어떤 예제를 빌드할지 선택 -> 톱니바퀴로 빌드 -> 삼각형(play) 버튼으로 실행 21 | - .vscode 폴더를 삭제한 후에 C/C++ Runner 확장 설치 후에 나타나는 하단의 톱니바퀴 아이콘을 클릭하면 .vscode 폴더가 다시 생기면서 .json 설정파일들이 만들어집니다. 이 파일들 안에 c++17 설정만 해주면 됩니다. 22 | 23 | settings.json 24 | ``` 25 | "C_Cpp_Runner.cppStandard": "c++17", 26 | ``` 27 | c_cpp_properties.json 28 | ``` 29 | "cppStandard": "c++17", 30 | ``` 31 | launch.json 32 | ``` 33 | "args": ["-std=c++17"], 34 | ``` 35 | 36 | - 그 외에 XCode를 설치했는데 필수인지는 확인을 안해봤습니다. VCode 터미널에 XCode 사용 동의하라고 뜨면서 사용자 권한으로 다시하라고 명령어를 안내해줄텐데 맥 자체 터미널을 만들어서 그 명령어대로 실행하고 동의하면 됩니다. 37 | - 혹시 더 좋은 방법 발견하신 분들은 [디스코드](https://discord.gg/kgR9xJkbsV)로 제보 부탁드립니다. 38 | -------------------------------------------------------------------------------- /VS2022/Ex01_HelloWorld/Ex01_HelloWorld.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {dccfba3e-df8f-4f00-a5bf-af56712ede8c} 28 | Ex01HelloWorld 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex01_HelloWorld/Ex01_HelloWorld.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex01_HelloWorld/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include // iostream이라는 헤더를 포함(include) 6 | 7 | using namespace std; // 네임스페이스 설명 std::cout 8 | 9 | int main() // entry point 10 | { 11 | // 주석(comment) 다는 방법 12 | 13 | cout << "Hello, World!" << endl; 14 | // printf("Hello World!!! by printf"); 15 | 16 | // 입출력에 대해서는 뒤에 다시 나와요. 17 | char user_input[100]; 18 | cin >> user_input; 19 | cout << user_input; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /VS2022/Ex02_DataTypes/Ex02_DataTypes.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {807f07fb-4d93-4940-951f-c4d7f72a091e} 25 | Ex02DataTypes 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex02_DataTypes/Ex02_DataTypes.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex02_DataTypes/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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; // 1 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' != 'a') << 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 | } 87 | -------------------------------------------------------------------------------- /VS2022/Ex03_Array/Ex03_Array.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {196218d5-9aa7-49f8-b0e4-4e70095bfd5a} 28 | Ex03Array 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex03_Array/Ex03_Array.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex03_Array/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 50 | -------------------------------------------------------------------------------- /VS2022/Ex04_InputOutput/Ex04_InputOutput.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {d5f36aa5-2f17-459f-be54-260675b13a79} 28 | Ex04InputOutput 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex04_InputOutput/Ex04_InputOutput.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex04_InputOutput/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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::max(),'\n') 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /VS2022/Ex05_Branching/Ex05_Branching.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {b9431aed-cb46-4155-9215-1d7bdb996e3e} 25 | Ex05Branching 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex05_Branching/Ex05_Branching.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex05_Branching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // 0이 아니면 true다 안내 12 | // 나머지 연산자 안내 13 | // 줄 바꿈 '\n' newline 14 | // 블럭 내용이 한 줄일 경우에는 {} 생략 가능 15 | 16 | int number; 17 | 18 | cin >> number; 19 | 20 | if (number % 2 == 0) 21 | cout << "짝수입니다.\n"; 22 | else 23 | cout << "홀수입니다.\n"; 24 | 25 | // 조건 연산자 (삼항 연산자) 26 | cout << (number % 2 == 0 ? "짝수입니다." : "홀수입니다.") << endl; 27 | 28 | // switch - case 29 | 30 | switch (number) 31 | { 32 | case 0: 33 | cout << "정수 0입니다." << endl; 34 | break; // 주의 35 | case 1: 36 | cout << "정수 1입니다." << endl; 37 | break; 38 | default: 39 | cout << "그 외의 숫자입니다." << endl; 40 | break; // 마지막은 생략 가능 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /VS2022/Ex06_Iteration/Ex06_Iteration.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {3f22865e-0d54-4054-887c-868adb15cce6} 25 | Ex06Iteration 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex06_Iteration/Ex06_Iteration.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex06_Iteration/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 72 | -------------------------------------------------------------------------------- /VS2022/Ex07_NumberGuess/Ex07_NumberGuess.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {956fec8c-370d-4d91-8bce-cffe84e41720} 25 | Ex07NumberGuess 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex07_NumberGuess/Ex07_NumberGuess.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex07_NumberGuess/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include // 난수 생성 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 | } 41 | -------------------------------------------------------------------------------- /VS2022/Ex08_Pointer/Ex08_Pointer.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {b0e090a5-5fc8-429e-9dff-935a7816f7a8} 25 | Ex08Pointer 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex08_Pointer/Ex08_Pointer.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex08_Pointer/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 54 | -------------------------------------------------------------------------------- /VS2022/Ex09_Function/Ex09_Function.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {3b236a4d-4921-4917-9bba-8d1c1ee54e85} 25 | Ex09Function 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | true 89 | true 90 | true 91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | true 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | true 105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | true 118 | true 119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Console 124 | true 125 | true 126 | true 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex09_Function/Ex09_Function.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex09_Function/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 34 | -------------------------------------------------------------------------------- /VS2022/Ex10_StringMatching/Ex10_StringMatching.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex10_StringMatching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int kMaxStr = 100; // 전역 상수 소개 10 | 11 | // 문자열을 매개변수로 넣기 12 | // 여기서는 모든 문자열 배열의 길이가 동일하다고 가정 13 | bool IsEqual(const char str1[], const char str2[]) 14 | { 15 | // 크기 출력 확인 (배열 크기가 아님 주의!) - 문자열의 길이를 별도로 저장해야 합니다! 16 | // cout << sizeof(str1) << " " << sizeof(str2) << " " << endl; 17 | // exit(-1); 18 | 19 | // 힌트: ==, != 같지 않다 비교 연산자 20 | // 힌트: 문자열 종료 조건 21 | // 디버깅 힌트: 문자를 정수로 바꿔서 출력해보기 22 | 23 | return true; 24 | } 25 | 26 | int main() 27 | { 28 | // 영어 사용이 디버깅에 유리합니다. 29 | const char str1[kMaxStr] = "stop"; 30 | 31 | while (1) 32 | { 33 | // TODO: 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /VS2022/Ex11_DynamicAllocation/Ex11_DynamicAllocation.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex11_DynamicAllocation/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int kMaxStr = 100; // 전역 상수 11 | 12 | int Min(int a, int b) 13 | { 14 | return a < b ? a : b; // 삼항연산자 15 | } 16 | 17 | int main() 18 | { 19 | // 문자열 복사 20 | char str1[] = "Hello, World!"; 21 | char str2[kMaxStr]; 22 | // dest, src 안내 (복사할 메모리 크기 주의 안내) 23 | memcpy(str2, str1, Min(sizeof(str1), sizeof(str2))); 24 | cout << str2 << endl; 25 | 26 | char *dynamic_array = new char[kMaxStr]; 27 | 28 | // 주의: 동적할당 메모리는 변수 사이즈가 포인터 사이즈임 (배열이 아님) 29 | memcpy(dynamic_array, str1, Min(sizeof(str1), sizeof(kMaxStr))); 30 | // memcpy(dynamic_array, str2, kMaxStr); 31 | cout << dynamic_array << endl; 32 | 33 | cout << str1 << " " << str2 << " " << dynamic_array << endl; 34 | cout << size_t(str1) << " " << size_t(str2) << " " << size_t(dynamic_array) << endl; 35 | 36 | // 보통 크기를 별도로 저장함 37 | 38 | delete[] dynamic_array; // 배열 삭제시 [] 39 | 40 | // 지우지 않고 재할당할 경우 잃어버림 41 | // dynamic_array = new char[원하는크기]; 42 | // delete[] dynamic_array; 다시 지워줘야 함 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /VS2022/Ex12_Structure/Ex12_Structure.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {168229f6-8e2a-412b-92ce-a60c972d47ca} 28 | Ex12Structure 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex12_Structure/Ex12_Structure.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex12_Structure/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | struct MyStruct 11 | { 12 | int first; 13 | int second; 14 | // ... 추가 가능 15 | 16 | int Sum() 17 | { 18 | return first + second; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | // member(.) operator 25 | MyStruct a; 26 | a.first = 123; 27 | a.second = 456; 28 | 29 | cout << sizeof(a) << endl; 30 | 31 | // 포인터는 member(->) operator가 화살표 32 | MyStruct *ptr_a = &a; 33 | 34 | cout << ptr_a->first << " " << ptr_a->second << " " << ptr_a->Sum() << endl; 35 | 36 | // 배열도 가능 37 | MyStruct pairs[10]; 38 | 39 | for (int i = 0; i < 10; i++) 40 | { 41 | // pairs->first = i; // 주의 42 | // pairs->second = i * 10; 43 | 44 | pairs[i].first = i; 45 | pairs[i].second = i * 10; 46 | } 47 | 48 | for (int i = 0; i < 10; i++) 49 | { 50 | cout << pairs[i].Sum() << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /VS2022/Ex13_Class/Ex13_Class.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {5641b66b-fc76-4d92-b899-be8d5a7a01f5} 28 | Ex13Class 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex13_Class/Ex13_Class.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex13_Class/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 확인 11 | 12 | class MyClass 13 | { 14 | public: 15 | MyClass() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyClass()" << endl; 19 | } 20 | 21 | MyClass(int number) 22 | { 23 | cout << "MyClass(int number)" << endl; 24 | 25 | // this pointer 소개 26 | this->number_ = number; 27 | } 28 | 29 | ~MyClass() 30 | { 31 | // 호출 시점 확인 32 | cout << "~MyClass()" << endl; 33 | } 34 | 35 | void Increment(int a) 36 | { 37 | number_ += a; 38 | } 39 | 40 | void Print() 41 | { 42 | cout << number_ << endl; 43 | } 44 | 45 | private: 46 | int number_ = 0; // 초기값 47 | }; 48 | 49 | int main() 50 | { 51 | MyClass my_class1; 52 | MyClass my_class2(123); 53 | 54 | my_class1.Print(); 55 | my_class2.Print(); 56 | 57 | my_class1.Increment(1); 58 | my_class1.Print(); 59 | 60 | // 배열 사용 가능 61 | // 포인터 사용 가능 등 안내 62 | // 기본 자료형과 비교 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /VS2022/Ex14_StringClass/Ex14_StringClass.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex14_StringClass/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 안내 11 | 12 | class MyString 13 | { 14 | public: 15 | MyString() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyString()" << endl; 19 | 20 | size_ = 1; 21 | str_ = new char[size_]; 22 | } 23 | 24 | MyString(const char* init_str) // init_str이 유효한 메모리라고 가정 25 | { 26 | cout << "MyString(const char *init_str)" << endl; 27 | 28 | // 1. 글자 수 먼저 확인 29 | size_ = 0; 30 | while (init_str[size_] != '\0') 31 | { 32 | size_++; // ++ 연산자 안내 33 | } 34 | 35 | // 2. 글자 수가 0이 아니면 메모리 할당 36 | if (size_ > 0) 37 | { 38 | str_ = new char[size_]; 39 | } 40 | 41 | // 3. 복사 42 | for (int i = 0; i < size_; i++) 43 | { 44 | str_[i] = init_str[i]; 45 | } 46 | // memcpy() 사용 가능 47 | } 48 | 49 | ~MyString() 50 | { 51 | // 호출 시점 확인 52 | cout << "Destructor" << endl; 53 | 54 | size_ = 0; 55 | if (str_) 56 | delete[] str_; 57 | } 58 | 59 | void Resize(int new_size) 60 | { 61 | char* new_str = new char[new_size]; 62 | 63 | // memcpy() 사용 가능 64 | for (int i = 0; i < (new_size < size_ ? new_size : size_); i++) 65 | { 66 | new_str[i] = str_[i]; 67 | } 68 | 69 | delete[] str_; 70 | str_ = new_str; 71 | size_ = new_size; 72 | 73 | // new_str 지우면 안되요! 74 | } 75 | 76 | void Print() 77 | { 78 | for (int i = 0; i < size_; i++) 79 | { 80 | cout << str_[i]; 81 | } 82 | cout << endl; 83 | } 84 | 85 | void Append(MyString* app_str) // 같은 타입을 매개변수로 사용 가능 86 | { 87 | int old_size = size_; 88 | 89 | // 다른 멤버 함수 호출 가능 90 | // Resize(...); 91 | 92 | // 중요한 개념 93 | for (int i = old_size; i < size_; i++) 94 | { 95 | // TODO: 복사 96 | } 97 | } 98 | 99 | private: 100 | int size_ = 0; // m_size 101 | char* str_ = nullptr; // m_str (여기서는 구글 스타일) 102 | }; 103 | 104 | int main() 105 | { 106 | // 클래스 기본 문법 안내 107 | 108 | MyString str1("ABCDE"); // 생성자 이용 109 | MyString str2("123"); 110 | 111 | str1.Append(&str2); // 주소 넣어줌 112 | 113 | str1.Print(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /VS2022/Ex15_Header/Ex15_Header.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | -------------------------------------------------------------------------------- /VS2022/Ex15_Header/MyClass.cpp: -------------------------------------------------------------------------------- 1 | #include "MyClass.h" // 선언 헤더 꼭 include 2 | 3 | #include 4 | 5 | using namespace std; // 보통 .h 파일 대신 .cpp에 사용 6 | 7 | MyClass::MyClass() 8 | { 9 | // 호출 시점 확인 10 | cout << "MyClass()" << endl; 11 | } 12 | 13 | MyClass::MyClass(int number) // init_str이 유효한 메모리라고 가정 14 | { 15 | cout << "MyClass(int number)" << endl; 16 | 17 | // this pointer 소개 18 | this->number_ = number; 19 | } 20 | 21 | MyClass::~MyClass() 22 | { 23 | // 호출 시점 확인 24 | cout << "~MyClass()" << endl; 25 | } 26 | 27 | void MyClass::Increment(int a) 28 | { 29 | number_ += a; 30 | } 31 | 32 | void MyClass::Print() 33 | { 34 | cout << number_ << endl; 35 | } -------------------------------------------------------------------------------- /VS2022/Ex15_Header/MyClass.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_CLASS_H 2 | #define MY_CLASS_H 3 | 4 | // TODO: 새로운 멤버 함수 추가해보기 연습 5 | 6 | class MyClass 7 | { 8 | public: 9 | MyClass(); // 멤버 함수의 몸체(body) 모두 삭제, 깔끔 10 | MyClass(int number); 11 | ~MyClass(); 12 | 13 | void Increment(int a); 14 | // void Decrement(int a); // 구현 실습 15 | void Print(); 16 | 17 | private: 18 | int number_ = 0; // 초기값 19 | }; 20 | 21 | #endif // MY_CLASS_H -------------------------------------------------------------------------------- /VS2022/Ex15_Header/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "MyClass.h" // <- 설명 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | MyClass my_class1; 15 | MyClass my_class2(123); 16 | 17 | my_class1.Print(); 18 | my_class2.Print(); 19 | 20 | my_class1.Increment(1); 21 | my_class1.Print(); 22 | 23 | // 배열 사용 가능 24 | // 포인터 사용 가능 등 안내 25 | // 기본 자료형과 비교 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /VS2022/Ex16_FileIO/Ex16_FileIO.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {ebb78256-4804-4567-bd74-ea6b517e602c} 28 | Ex16FileIO 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex16_FileIO/Ex16_FileIO.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex16_FileIO/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | // 파일 출력 13 | { 14 | ofstream ofile; 15 | 16 | ofile.open("my_contacts.txt"); 17 | ofile << "안녕하세요? 반갑습니다.\n"; 18 | ofile << "두 번째 줄입니다.\n"; 19 | ofile << "세 번째 줄입니다.\n"; 20 | ofile.close(); 21 | } 22 | 23 | // 파일 입력 24 | { 25 | char line[100]; 26 | 27 | ifstream ifile; 28 | ifile.open("my_contacts.txt"); 29 | 30 | int line_number = 0; 31 | while (ifile.getline(line, sizeof(line))) 32 | { 33 | cout << line_number << " : "; 34 | cout << line << endl; 35 | 36 | line_number += 1; 37 | } 38 | 39 | ifile.close(); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /VS2022/Ex17_PhoneBook/Ex17_PhoneBook.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | -------------------------------------------------------------------------------- /VS2022/Ex17_PhoneBook/PhoneBook.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include "PhoneBook.h" 6 | 7 | #include 8 | #include 9 | #include // memcpy(.) 10 | 11 | using namespace std; 12 | 13 | PhoneBook::PhoneBook() 14 | { 15 | contacts_ = new Contact[capacity_]; 16 | } 17 | 18 | PhoneBook::~PhoneBook() 19 | { 20 | if (contacts_) 21 | delete[] contacts_; 22 | } 23 | 24 | bool PhoneBook::IsEmpty() 25 | { 26 | assert(num_contacts_ >= 0); 27 | 28 | if (num_contacts_ == 0) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | bool PhoneBook::IsFull() 35 | { 36 | if (num_contacts_ == capacity_) 37 | return true; 38 | else 39 | return false; 40 | } 41 | 42 | void PhoneBook::PrintAll() 43 | { 44 | for (int i = 0; i < num_contacts_; i++) 45 | { 46 | PrintContact(i); 47 | } 48 | } 49 | 50 | void PhoneBook::PrintContact(int index) 51 | { 52 | cout << index << " "; 53 | cout << contacts_[index].name; 54 | cout << ", " << contacts_[index].phone << endl; 55 | } 56 | 57 | void PhoneBook::AddContact(const char name[], const char phone[]) 58 | { 59 | assert(!IsFull()); // 디버깅할 때 assert 괄호 안쪽 조건이 false가 되면 오류 처리 60 | 61 | memcpy(contacts_[num_contacts_].name, name, sizeof(contacts_[num_contacts_].name)); 62 | memcpy(contacts_[num_contacts_].phone, phone, sizeof(contacts_[num_contacts_].phone)); 63 | 64 | num_contacts_ += 1; 65 | } 66 | 67 | void PhoneBook::AddContact() 68 | { 69 | // capacity_가 고정된 경우 70 | if (IsFull()) 71 | { 72 | cout << "더 이상 추가할 수 없습니다." << endl; 73 | return; 74 | } 75 | 76 | // 더 해볼 것: 메모리를 재할당 받아서 연락처 개수 제한 없애기 77 | 78 | char new_name[kMaxStr]; 79 | char new_phone[kMaxStr]; 80 | 81 | cout << "이름을 입력해주세요 : "; 82 | cin.getline(new_name, sizeof(new_name)); 83 | 84 | cout << "전화번호를 입력해주세요 : "; 85 | cin.getline(new_phone, sizeof(new_phone)); 86 | 87 | AddContact(new_name, new_phone); 88 | } 89 | 90 | int PhoneBook::FindByName() 91 | { 92 | char search_name[kMaxStr]; 93 | 94 | cout << "검색할 이름을 입력해주세요 : "; 95 | cin.getline(search_name, sizeof(search_name)); 96 | 97 | // TODO: IsEqual(), PrintContact(i), return i 98 | 99 | cout << search_name << " 님을 찾지 못했습니다." << endl; 100 | 101 | return -1; 102 | } 103 | 104 | bool PhoneBook::IsEqual(const char str1[], const char str2[]) 105 | { 106 | for (int i = 0; i < kMaxStr; i++) 107 | { 108 | if (str1[i] != str2[i]) 109 | return false; 110 | 111 | if (str1[i] == '\0') 112 | return true; 113 | } 114 | 115 | return true; 116 | } 117 | 118 | void PhoneBook::DeleteByName() 119 | { 120 | // 삭제할 때 메모리를 줄이지는 않는 것으로 할께요. 121 | 122 | int index = FindByName(); 123 | 124 | if (index >= 0) 125 | { 126 | // TODO: 중간에서 삭제했을 경우 데이터 정리 127 | 128 | // TODO: num_contacts_ 하나 감소 129 | 130 | num_contacts_ -= 1; 131 | } 132 | } -------------------------------------------------------------------------------- /VS2022/Ex17_PhoneBook/PhoneBook.h: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #ifndef PHONE_BOOK_H_ 6 | #define PHONE_BOOK_H_ 7 | 8 | const int kMaxStr = 20; // 문자열의 최대 글자 수 9 | 10 | struct Contact 11 | { 12 | char name[kMaxStr]; 13 | char phone[kMaxStr]; 14 | }; 15 | 16 | class PhoneBook 17 | { 18 | public: 19 | PhoneBook(); 20 | ~PhoneBook(); 21 | 22 | bool IsEmpty(); 23 | bool IsFull(); 24 | void PrintAll(); 25 | void PrintContact(int i); 26 | void AddContact(); 27 | void AddContact(const char name[], const char phone[]); 28 | int FindByName(); 29 | bool IsEqual(const char str1[], const char str2[]); 30 | void DeleteByName(); 31 | 32 | private: 33 | int capacity_ = 3; // 연락처 최대 개수 (변경 가능) 34 | int num_contacts_ = 0; 35 | Contact *contacts_ = nullptr; 36 | }; 37 | 38 | #endif -------------------------------------------------------------------------------- /VS2022/Ex17_PhoneBook/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "PhoneBook.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | PhoneBook my_phonebook; 15 | 16 | // 초기 데이터 17 | my_phonebook.AddContact("홍정모", "1234-1234"); 18 | my_phonebook.AddContact("둘리", "8585-2324"); 19 | my_phonebook.AddContact("아이언맨", "7432-9897"); 20 | 21 | int menu_number; 22 | 23 | while (true) 24 | { 25 | cout << "1: 모두 출력\n"; 26 | cout << "2: 검색\n"; 27 | cout << "3: 추가\n"; 28 | cout << "4: 삭제\n"; 29 | cout << "X: 종료\n"; 30 | cout << "메뉴를 선택해주세요 : "; 31 | 32 | cin >> menu_number; 33 | cin.ignore(); 34 | 35 | // if-else 대신에 switch 사용 가능 36 | if (menu_number == 1) // 모두 출력 37 | { 38 | my_phonebook.PrintAll(); 39 | } 40 | else if (menu_number == 2) // 검색 41 | { 42 | my_phonebook.FindByName(); 43 | } 44 | else if (menu_number == 3) // 추가 45 | { 46 | my_phonebook.AddContact(); 47 | } 48 | else if (menu_number == 4) // 삭제 49 | { 50 | my_phonebook.DeleteByName(); 51 | } 52 | else 53 | { 54 | cout << "종료합니다." << endl; 55 | break; 56 | } 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | /* 63 | 전화번호부 필요한 기능들 64 | - 모두 출력, 이름으로 찾기(검색) 65 | - 추가, 삭제 66 | 67 | 더 해볼 것들 68 | - 파일 저장 및 읽어들이기 69 | */ 70 | -------------------------------------------------------------------------------- /VS2022/Ex18_Template/Ex18_Template.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 16.0 26 | Win32Proj 27 | {5beb00b8-cbde-496e-b918-c60b6ccb9611} 28 | Ex18Template 29 | 10.0 30 | 31 | 32 | 33 | Application 34 | true 35 | v143 36 | Unicode 37 | 38 | 39 | Application 40 | false 41 | v143 42 | true 43 | Unicode 44 | 45 | 46 | Application 47 | true 48 | v143 49 | Unicode 50 | 51 | 52 | Application 53 | false 54 | v143 55 | true 56 | Unicode 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Level3 79 | true 80 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 81 | true 82 | 83 | 84 | Console 85 | true 86 | 87 | 88 | 89 | 90 | Level3 91 | true 92 | true 93 | true 94 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | 97 | 98 | Console 99 | true 100 | true 101 | true 102 | 103 | 104 | 105 | 106 | Level3 107 | true 108 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /VS2022/Ex18_Template/Ex18_Template.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /VS2022/Ex18_Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class MyInt 7 | { 8 | public: 9 | int data_; 10 | }; 11 | 12 | class MyDouble 13 | { 14 | public: 15 | double data_; 16 | }; 17 | 18 | template 19 | class MyClass 20 | { 21 | public: 22 | T data_; 23 | }; 24 | 25 | int main() 26 | { 27 | MyClass my_int; 28 | MyClass my_double; 29 | 30 | my_int.data_; 31 | my_double.data_; 32 | 33 | cout << sizeof(my_int) << " " << sizeof(my_double) << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /english/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c17", 11 | "cppStandard": "gnu++14", 12 | "intelliSenseMode": "linux-gcc-x64" 13 | } 14 | ], 15 | "version": 4 16 | } -------------------------------------------------------------------------------- /english/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++ build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}/${fileBasenameNoExtension}", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "setupCommands": [ 15 | { 16 | "description": "Enable pretty-printing for gdb", 17 | "text": "-enable-pretty-printing", 18 | "ignoreFailures": true 19 | }, 20 | { 21 | "description": "Set Disassembly Flavor to Intel", 22 | "text": "-gdb-set disassembly-flavor intel", 23 | "ignoreFailures": true 24 | } 25 | ], 26 | "preLaunchTask": "C/C++: g++ build active file", 27 | "miDebuggerPath": "/usr/bin/gdb" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /english/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /english/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${fileDirname}/*.cpp", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /english/.vscode_gcc/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${vcpkgRoot}/x64-windows/include" 8 | ], 9 | "defines": [ 10 | "_DEBUG", 11 | "UNICODE", 12 | "_UNICODE" 13 | ], 14 | "windowsSdkVersion": "10.0.22000.0", 15 | "compilerPath": "C:/msys64/mingw64/bin/g++.exe", 16 | "cStandard": "c17", 17 | "cppStandard": "c++17", 18 | "intelliSenseMode": "windows-gcc-x64" 19 | } 20 | ], 21 | "version": 4 22 | } -------------------------------------------------------------------------------- /english/.vscode_gcc/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++.exe build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "C:/msys64/mingw64/bin", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | }, 21 | { 22 | "description": "Set Disassembly Flavor to Intel", 23 | "text": "-gdb-set disassembly-flavor intel", 24 | "ignoreFailures": true 25 | } 26 | ], 27 | "preLaunchTask": "C/C++: g++.exe build active file" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /english/.vscode_gcc/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "typeinfo": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /english/.vscode_gcc/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++.exe build active file", 6 | "command": "C:/msys64/mingw64/bin/g++.exe", 7 | "args": [ 8 | "-finput-charset=UTF-8", 9 | "-fdiagnostics-color=always", 10 | "-g", 11 | "-Wall", 12 | "${fileDirname}/*.cpp", 13 | "-o", 14 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 15 | ], 16 | "options": { 17 | "cwd": "C:/msys64/mingw64/bin" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /english/.vscode_msvc/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${vcpkgRoot}/x64-windows/include" 8 | ], 9 | "defines": [ 10 | "_DEBUG", 11 | "UNICODE", 12 | "_UNICODE" 13 | ], 14 | "windowsSdkVersion": "10.0.22000.0", 15 | "compilerPath": "cl.exe", 16 | "cStandard": "c17", 17 | "cppStandard": "c++17", 18 | "intelliSenseMode": "windows-msvc-x64" 19 | } 20 | ], 21 | "version": 4 22 | } -------------------------------------------------------------------------------- /english/.vscode_msvc/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: cl.exe build and debug active file", 5 | "type": "cppvsdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "console": "integratedTerminal", 13 | "preLaunchTask": "C/C++: cl.exe 활성 파일 빌드" 14 | } 15 | ], 16 | "version": "2.0.0" 17 | } -------------------------------------------------------------------------------- /english/.vscode_msvc/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: cl.exe 활성 파일 빌드", 6 | "command": "cl.exe", 7 | "args": [ 8 | "/Zi", 9 | "/EHsc", 10 | "/nologo", 11 | "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe", 12 | "${fileDirname}/*.cpp", 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$msCompile" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "디버거에서 생성된 작업입니다." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /english/.vscode_wsl(ubuntu)/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c17", 11 | "cppStandard": "gnu++14", 12 | "intelliSenseMode": "linux-gcc-x64" 13 | } 14 | ], 15 | "version": 4 16 | } -------------------------------------------------------------------------------- /english/.vscode_wsl(ubuntu)/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "C/C++: g++ build and debug active file", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "${fileDirname}/${fileBasenameNoExtension}", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "gdb", 14 | "setupCommands": [ 15 | { 16 | "description": "Enable pretty-printing for gdb", 17 | "text": "-enable-pretty-printing", 18 | "ignoreFailures": true 19 | }, 20 | { 21 | "description": "Set Disassembly Flavor to Intel", 22 | "text": "-gdb-set disassembly-flavor intel", 23 | "ignoreFailures": true 24 | } 25 | ], 26 | "preLaunchTask": "C/C++: g++ build active file", 27 | "miDebuggerPath": "/usr/bin/gdb" 28 | } 29 | ], 30 | "version": "2.0.0" 31 | } -------------------------------------------------------------------------------- /english/.vscode_wsl(ubuntu)/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${fileDirname}/*.cpp", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /english/Ex01_HelloWorld/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include // iostream이라는 헤더를 포함(include) 6 | 7 | using namespace std; // 네임스페이스 설명 std::cout 8 | 9 | int main() // entry point 10 | { 11 | // 주석(comment) 다는 방법 12 | 13 | cout << "Hello, World!" << endl; 14 | // printf("Hello World!!! by printf"); 15 | 16 | // 입출력에 대해서는 뒤에 다시 나와요. 17 | char user_input[100]; 18 | cin >> user_input; 19 | cout << user_input; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /english/Ex02_DataTypes/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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; // 1 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' != 'a') << 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 | } 87 | -------------------------------------------------------------------------------- /english/Ex03_Array/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 50 | -------------------------------------------------------------------------------- /english/Ex04_InputOutput/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 << "Input texts." << endl; 21 | cout << "Here : "; 22 | 23 | // cin >> user_input; 24 | 25 | cin.getline(user_input, sizeof(user_input)); 26 | 27 | cout << "Echo : " << 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::max(),'\n') 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /english/Ex05_Branching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // 0이 아니면 true다 안내 12 | // 나머지 연산자 안내 13 | // 줄 바꿈 '\n' newline 14 | // 블럭 내용이 한 줄일 경우에는 {} 생략 가능 15 | 16 | int number; 17 | 18 | cin >> number; 19 | 20 | if (number % 2 == 0) 21 | cout << "Even\n"; 22 | else 23 | cout << "Odd\n"; 24 | 25 | // 조건 연산자 (삼항 연산자) 26 | cout << (number % 2 == 0 ? "Even" : "Odd") << endl; 27 | 28 | // switch - case 29 | 30 | switch (number) 31 | { 32 | case 0: 33 | cout << "Number 0" << endl; 34 | break; // 주의 35 | case 1: 36 | cout << "Number 1" << endl; 37 | break; 38 | default: 39 | cout << "Other" << endl; 40 | break; // 마지막은 생략 가능 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /english/Ex06_Iteration/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 72 | -------------------------------------------------------------------------------- /english/Ex07_NumberGuess/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include // 난수 생성 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 << "Input : "; 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 | } 41 | -------------------------------------------------------------------------------- /english/Ex08_Pointer/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | } 54 | -------------------------------------------------------------------------------- /english/Ex09_Function/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 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 | // 반환 자료형이 지정되지 않았음 (void) 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 | } 34 | -------------------------------------------------------------------------------- /english/Ex10_StringMatching/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int kMaxStr = 100; // 전역 상수 소개 10 | 11 | // 문자열을 매개변수로 넣기 12 | // 여기서는 모든 문자열 배열의 길이가 동일하다고 가정 13 | bool IsEqual(const char str1[], const char str2[]) 14 | { 15 | // 크기 출력 확인 (배열 크기가 아님 주의!) - 문자열의 길이를 별도로 저장해야 합니다! 16 | // cout << sizeof(str1) << " " << sizeof(str2) << " " << endl; 17 | // exit(-1); 18 | 19 | // 힌트: ==, != 같지 않다 비교 연산자 20 | // 힌트: 문자열 종료 조건 21 | // 디버깅 힌트: 문자를 정수로 바꿔서 출력해보기 22 | 23 | return true; 24 | } 25 | 26 | int main() 27 | { 28 | // 영어 사용이 디버깅에 유리합니다. 29 | const char str1[kMaxStr] = "stop"; 30 | 31 | while (1) 32 | { 33 | // TODO: 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /english/Ex11_DynamicAllocation/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int kMaxStr = 100; // 전역 상수 11 | 12 | int Min(int a, int b) 13 | { 14 | return a < b ? a : b; // 조건연산자(삼항연산자) 15 | } 16 | 17 | int main() 18 | { 19 | // 문자열 복사 20 | char str1[] = "Hello, World!"; 21 | char str2[kMaxStr]; 22 | 23 | // dest, src 안내 (복사할 메모리 크기 주의) 24 | memcpy(str2, str1, Min(sizeof(str1), sizeof(str2))); 25 | cout << str2 << endl; 26 | 27 | char *dynamic_array = new char[kMaxStr]; 28 | 29 | // 주의: 동적할당 메모리는 변수 사이즈가 포인터 사이즈임 (배열이 아님) 30 | memcpy(dynamic_array, str1, Min(sizeof(str1), sizeof(kMaxStr))); 31 | // memcpy(dynamic_array, str2, kMaxStr); 32 | cout << dynamic_array << endl; 33 | 34 | cout << str1 << " " << str2 << " " << dynamic_array << endl; 35 | cout << size_t(str1) << " " << size_t(str2) << " " << size_t(dynamic_array) << endl; 36 | 37 | // 보통 크기를 별도로 저장함 38 | 39 | delete[] dynamic_array; // 배열 삭제시 [] 40 | 41 | // 지우지 않고 재할당할 경우 잃어버림 42 | // dynamic_array = new char[원하는크기]; 43 | // delete[] dynamic_array; 다시 지워줘야 함 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /english/Ex12_Structure/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | struct MyStruct 11 | { 12 | int first; 13 | int second; 14 | // ... 추가 가능 15 | 16 | int Sum() 17 | { 18 | return first + second; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | // member(.) operator 25 | MyStruct a; 26 | a.first = 123; 27 | a.second = 456; 28 | 29 | cout << sizeof(a) << endl; 30 | 31 | // 포인터는 member(->) operator가 화살표 32 | MyStruct *ptr_a = &a; 33 | 34 | cout << ptr_a->first << " " << ptr_a->second << " " << ptr_a->Sum() << endl; 35 | 36 | // 배열도 가능 37 | MyStruct pairs[10]; 38 | 39 | for (int i = 0; i < 10; i++) 40 | { 41 | // pairs->first = i; // 주의 42 | // pairs->second = i * 10; 43 | 44 | pairs[i].first = i; 45 | pairs[i].second = i * 10; 46 | } 47 | 48 | for (int i = 0; i < 10; i++) 49 | { 50 | cout << pairs[i].Sum() << endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /english/Ex13_Class/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 확인 11 | 12 | class MyClass 13 | { 14 | public: 15 | MyClass() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyClass()" << endl; 19 | } 20 | 21 | MyClass(int number) 22 | { 23 | cout << "MyClass(int number)" << endl; 24 | 25 | // this pointer 소개 26 | this->number_ = number; 27 | } 28 | 29 | ~MyClass() 30 | { 31 | // 호출 시점 확인 32 | cout << "~MyClass()" << endl; 33 | } 34 | 35 | void Increment(int a) 36 | { 37 | number_ += a; 38 | } 39 | 40 | void Print() 41 | { 42 | cout << number_ << endl; 43 | } 44 | 45 | private: 46 | int number_ = 0; // 초기값 47 | }; 48 | 49 | int main() 50 | { 51 | MyClass my_class1; 52 | MyClass my_class2(123); 53 | 54 | my_class1.Print(); 55 | my_class2.Print(); 56 | 57 | my_class1.Increment(1); 58 | my_class1.Print(); 59 | 60 | // 배열 사용 가능 61 | // 포인터 사용 가능 등 안내 62 | // 기본 자료형과 비교 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /english/Ex14_StringClass/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // public, private 접근 권한 안내 11 | 12 | class MyString 13 | { 14 | public: 15 | MyString() 16 | { 17 | // 호출 시점 확인 18 | cout << "MyString()" << endl; 19 | 20 | size_ = 1; 21 | str_ = new char[size_]; 22 | } 23 | 24 | MyString(const char *init_str) // init_str이 유효한 메모리라고 가정 25 | { 26 | cout << "MyString(const char *init_str)" << endl; 27 | 28 | // 1. 글자 수 먼저 확인 29 | size_ = 0; 30 | while (init_str[size_] != '\0') 31 | { 32 | size_++; // ++ 연산자 안내 33 | } 34 | 35 | // 2. 글자 수가 0이 아니면 메모리 할당 36 | if (size_ > 0) 37 | { 38 | str_ = new char[size_]; 39 | } 40 | 41 | // 3. 복사 42 | for (int i = 0; i < size_; i++) 43 | { 44 | str_[i] = init_str[i]; 45 | } 46 | // memcpy() 사용 가능 47 | } 48 | 49 | ~MyString() 50 | { 51 | // 호출 시점 확인 52 | cout << "Destructor" << endl; 53 | 54 | size_ = 0; 55 | if (str_) 56 | delete[] str_; 57 | } 58 | 59 | void Resize(int new_size) 60 | { 61 | char *new_str = new char[new_size]; 62 | 63 | // memcpy() 사용 가능 64 | for (int i = 0; i < (new_size < size_ ? new_size : size_); i++) 65 | { 66 | new_str[i] = str_[i]; 67 | } 68 | 69 | delete[] str_; 70 | str_ = new_str; 71 | size_ = new_size; 72 | 73 | // new_str 지우면 안되요! 74 | } 75 | 76 | void Print() 77 | { 78 | for (int i = 0; i < size_; i++) 79 | { 80 | cout << str_[i]; 81 | } 82 | cout << endl; 83 | } 84 | 85 | void Append(MyString *app_str) // 같은 타입을 매개변수로 사용 가능 86 | { 87 | int old_size = size_; 88 | 89 | // 다른 멤버 함수 호출 가능 90 | // Resize(...); 91 | 92 | // 중요한 개념 93 | for (int i = old_size; i < size_; i++) 94 | { 95 | // TODO: 복사 96 | } 97 | } 98 | 99 | private: 100 | int size_ = 0; // m_size 101 | char *str_ = nullptr; // m_str (여기서는 구글 스타일) 102 | }; 103 | 104 | int main() 105 | { 106 | // 클래스 기본 문법 안내 107 | 108 | MyString str1("ABCDE"); // 생성자 이용 109 | MyString str2("123"); 110 | 111 | str1.Append(&str2); // 주소 넣어줌 112 | 113 | str1.Print(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /english/Ex15_Header/MyClass.cpp: -------------------------------------------------------------------------------- 1 | #include "MyClass.h" // 선언 헤더 꼭 include 2 | 3 | #include 4 | 5 | using namespace std; // 보통 .h 파일 대신 .cpp에 사용 6 | 7 | MyClass::MyClass() 8 | { 9 | // 호출 시점 확인 10 | cout << "MyClass()" << endl; 11 | } 12 | 13 | MyClass::MyClass(int number) // init_str이 유효한 메모리라고 가정 14 | { 15 | cout << "MyClass(int number)" << endl; 16 | 17 | // this pointer 소개 18 | this->number_ = number; 19 | } 20 | 21 | MyClass::~MyClass() 22 | { 23 | // 호출 시점 확인 24 | cout << "~MyClass()" << endl; 25 | } 26 | 27 | void MyClass::Increment(int a) 28 | { 29 | number_ += a; 30 | } 31 | 32 | void MyClass::Print() 33 | { 34 | cout << number_ << endl; 35 | } -------------------------------------------------------------------------------- /english/Ex15_Header/MyClass.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_CLASS_H 2 | #define MY_CLASS_H 3 | 4 | // TODO: 새로운 멤버 함수 추가해보기 연습 5 | 6 | class MyClass 7 | { 8 | public: 9 | MyClass(); // 멤버 함수의 몸체(body) 모두 삭제, 깔끔 10 | MyClass(int number); 11 | ~MyClass(); 12 | 13 | void Increment(int a); 14 | // void Decrement(int a); // 구현 실습 15 | void Print(); 16 | 17 | private: 18 | int number_ = 0; // 초기값 19 | }; 20 | 21 | #endif // MY_CLASS_H -------------------------------------------------------------------------------- /english/Ex15_Header/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "MyClass.h" // <- 설명 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | MyClass my_class1; 15 | MyClass my_class2(123); 16 | 17 | my_class1.Print(); 18 | my_class2.Print(); 19 | 20 | my_class1.Increment(1); 21 | my_class1.Print(); 22 | 23 | // 배열 사용 가능 24 | // 포인터 사용 가능 등 안내 25 | // 기본 자료형과 비교 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /english/Ex16_FileIO/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | // 파일 출력 13 | /*{ 14 | ofstream ofile; 15 | 16 | ofile.open("my_contacts.txt"); 17 | ofile << "안녕하세요? 반갑습니다.\n"; 18 | ofile << "두 번째 줄입니다.\n"; 19 | ofile << "세 번째 줄입니다.\n"; 20 | ofile.close(); 21 | }*/ 22 | 23 | // 파일 입력 24 | { 25 | char line[100]; 26 | 27 | ifstream ifile; 28 | ifile.open("my_contacts.txt"); 29 | 30 | int line_number = 0; 31 | while (ifile.getline(line, sizeof(line))) 32 | { 33 | cout << line_number << " : "; 34 | cout << line << endl; 35 | 36 | line_number += 1; 37 | } 38 | 39 | ifile.close(); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /english/Ex17_PhoneBook/PhoneBook.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include "PhoneBook.h" 6 | 7 | #include 8 | #include 9 | #include // memcpy(.) 10 | 11 | using namespace std; 12 | 13 | PhoneBook::PhoneBook() 14 | { 15 | contacts_ = new Contact[capacity_]; 16 | } 17 | 18 | PhoneBook::~PhoneBook() 19 | { 20 | if (contacts_) 21 | delete[] contacts_; 22 | } 23 | 24 | bool PhoneBook::IsEmpty() 25 | { 26 | assert(num_contacts_ >= 0); 27 | 28 | if (num_contacts_ == 0) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | bool PhoneBook::IsFull() 35 | { 36 | if (num_contacts_ == capacity_) 37 | return true; 38 | else 39 | return false; 40 | } 41 | 42 | void PhoneBook::PrintAll() 43 | { 44 | for (int i = 0; i < num_contacts_; i++) 45 | { 46 | PrintContact(i); 47 | } 48 | } 49 | 50 | void PhoneBook::PrintContact(int index) 51 | { 52 | cout << index << " "; 53 | cout << contacts_[index].name; 54 | cout << ", " << contacts_[index].phone << endl; 55 | } 56 | 57 | void PhoneBook::AddContact(const char name[], const char phone[]) 58 | { 59 | assert(!IsFull()); // 디버깅할 때 assert 괄호 안쪽 조건이 false가 되면 오류 처리 60 | 61 | memcpy(contacts_[num_contacts_].name, name, sizeof(contacts_[num_contacts_].name)); 62 | memcpy(contacts_[num_contacts_].phone, phone, sizeof(contacts_[num_contacts_].phone)); 63 | 64 | num_contacts_ += 1; 65 | } 66 | 67 | void PhoneBook::AddContact() 68 | { 69 | // capacity_가 고정된 경우 70 | if (IsFull()) 71 | { 72 | cout << "Full. Cannot add more." << endl; 73 | return; 74 | } 75 | 76 | // 더 해볼 것: 메모리를 재할당 받아서 연락처 개수 제한 없애기 77 | 78 | char new_name[kMaxStr]; 79 | char new_phone[kMaxStr]; 80 | 81 | cout << "Name : "; 82 | cin.getline(new_name, sizeof(new_name)); 83 | 84 | cout << "Phone : "; 85 | cin.getline(new_phone, sizeof(new_phone)); 86 | 87 | AddContact(new_name, new_phone); 88 | } 89 | 90 | int PhoneBook::FindByName() 91 | { 92 | char search_name[kMaxStr]; 93 | 94 | cout << "Name to find : "; 95 | cin.getline(search_name, sizeof(search_name)); 96 | 97 | // TODO: IsEqual(), PrintContact(i), return i 98 | 99 | cout << search_name << " 님을 찾지 못했습니다." << endl; 100 | 101 | return -1; 102 | } 103 | 104 | bool PhoneBook::IsEqual(const char str1[], const char str2[]) 105 | { 106 | for (int i = 0; i < kMaxStr; i++) 107 | { 108 | if (str1[i] != str2[i]) 109 | return false; 110 | 111 | if (str1[i] == '\0') 112 | return true; 113 | } 114 | 115 | return true; 116 | } 117 | 118 | void PhoneBook::DeleteByName() 119 | { 120 | // 삭제할 때 메모리를 줄이지는 않는 것으로 할께요. 121 | 122 | int index = FindByName(); 123 | 124 | if (index >= 0) 125 | { 126 | // TODO: 중간에서 삭제했을 경우 데이터 정리 127 | 128 | // TODO: num_contacts_ 하나 감소 129 | 130 | num_contacts_ -= 1; 131 | } 132 | } -------------------------------------------------------------------------------- /english/Ex17_PhoneBook/PhoneBook.h: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #ifndef PHONE_BOOK_H_ 6 | #define PHONE_BOOK_H_ 7 | 8 | const int kMaxStr = 20; // 문자열의 최대 글자 수 9 | 10 | struct Contact 11 | { 12 | char name[kMaxStr]; 13 | char phone[kMaxStr]; 14 | }; 15 | 16 | class PhoneBook 17 | { 18 | public: 19 | PhoneBook(); 20 | ~PhoneBook(); 21 | 22 | bool IsEmpty(); 23 | bool IsFull(); 24 | void PrintAll(); 25 | void PrintContact(int i); 26 | void AddContact(); 27 | void AddContact(const char name[], const char phone[]); 28 | int FindByName(); 29 | bool IsEqual(const char str1[], const char str2[]); 30 | void DeleteByName(); 31 | 32 | private: 33 | int capacity_ = 3; // 연락처 최대 개수 (변경 가능) 34 | int num_contacts_ = 0; 35 | Contact *contacts_ = nullptr; 36 | }; 37 | 38 | #endif -------------------------------------------------------------------------------- /english/Ex17_PhoneBook/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 홍정모 연구소 https://honglab.co.kr/ 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "PhoneBook.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | PhoneBook my_phonebook; 15 | 16 | // 초기 데이터 17 | my_phonebook.AddContact("Jeong-Mo Hong", "1234-1234"); 18 | my_phonebook.AddContact("Barbie", "8585-2324"); 19 | my_phonebook.AddContact("Ironman", "7432-9897"); 20 | 21 | int menu_number; 22 | 23 | while (true) 24 | { 25 | cout << "1: Print all\n"; 26 | cout << "2: Find by name\n"; 27 | cout << "3: Add\n"; 28 | cout << "4: Delete \n"; 29 | cout << "X: Exit\n"; 30 | cout << "Menu : "; 31 | 32 | cin >> menu_number; 33 | cin.ignore(); 34 | 35 | // if-else 대신에 switch 사용 가능 36 | if (menu_number == 1) // 모두 출력 37 | { 38 | my_phonebook.PrintAll(); 39 | } 40 | else if (menu_number == 2) // 검색 41 | { 42 | my_phonebook.FindByName(); 43 | } 44 | else if (menu_number == 3) // 추가 45 | { 46 | my_phonebook.AddContact(); 47 | } 48 | else if (menu_number == 4) // 삭제 49 | { 50 | my_phonebook.DeleteByName(); 51 | } 52 | else 53 | { 54 | cout << "Exiting." << endl; 55 | break; 56 | } 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | /* 63 | 전화번호부 필요한 기능들 64 | - 모두 출력, 이름으로 찾기(검색) 65 | - 추가, 삭제 66 | 67 | 더 해볼 것들 68 | - 파일 저장 및 읽어들이기 69 | */ 70 | -------------------------------------------------------------------------------- /english/Ex18_Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class MyInt 7 | { 8 | public: 9 | int data_; 10 | }; 11 | 12 | class MyDouble 13 | { 14 | public: 15 | double data_; 16 | }; 17 | 18 | template 19 | class MyClass 20 | { 21 | public: 22 | T data_; 23 | }; 24 | 25 | int main() 26 | { 27 | MyClass my_int; 28 | MyClass my_double; 29 | 30 | my_int.data_; 31 | my_double.data_; 32 | 33 | cout << sizeof(my_int) << " " << sizeof(my_double) << endl; 34 | 35 | return 0; 36 | } 37 | --------------------------------------------------------------------------------