Unreal5 공부/TIL
2026/03/10 TIL
anna59
2026. 3. 10. 21:02
오늘의 목표
- C++ 문법 1-3 프로그래밍 기초(3) 강의 수강
- C++ 문법 1-4 포인터와 레퍼런스 강의 수강
- C++ 문법 1-5 Class 개념 강의 수강
- C++ 문법 1-6 객체 지향 프로그래밍 강의 수강
- C++ 문법 1주차 숙제
1. C++ 문법 1-3 프로그래밍 기초(3)
조건문
단순 if문
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
// if 문: 조건이 true일 때만 실행됩니다.
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
// 조건이 false인 경우 아무것도 실행되지 않습니다.
cout << "Program finished." << endl;
return 0;
}
<출력 결과>
Enter your age: 30
You are eligible to vote.
Program finished.
if / else
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
// if/else 문: 조건이 true일 때와 false일 때 다른 작업을 수행합니다.
if (number % 2 == 0) { // 입력값이 짝수인지 확인합니다.
cout << "The number is even." << endl;
}
else { // 위 조건이 false라면 (즉, 홀수라면) 실행됩니다.
cout << "The number is odd." << endl;
}
cout << "Program finished." << endl;
return 0;
}
입력값: 3
<출력 결과>
Enter a number: 3
The number is odd.
Program finished.
입력값: 4
<출력 결과>
Enter a number: 4
The number is even.
Program finished.
if / else if / else문
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter your score (0-100): ";
cin >> score;
// if/else if/else 문: 여러 조건을 순차적으로 검사합니다.
if (score >= 90) { // 90 이상인 경우
cout << "Grade: A" << endl;
}
else if (score >= 80) { // 80 이상 90 미만인 경우
cout << "Grade: B" << endl;
}
else if (score >= 70) { // 70 이상 80 미만인 경우
cout << "Grade: C" << endl;
}
else if (score >= 60) { // 60 이상 70 미만인 경우
cout << "Grade: D" << endl;
}
else { // 60 미만인 경우
cout << "Grade: F" << endl;
}
cout << "Program finished." << endl;
return 0;
}
<출력 결과>
Enter your score (0-100): 86
Grade: B
Program finished.
else문을 적합하게 사용하지 않아서 과도하게 복잡한 조건문
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
if (op == '+') {
cout << "Result: " << num1 + num2 << endl;
} else {
if (op == '-') {
cout << "Result: " << num1 - num2 << endl;
} else {
if (op == '*') {
cout << "Result: " << num1 * num2 << endl;
} else {
if (op == '/') {
if (num2 == 0) {
cout << "Division by zero is not allowed." << endl;
} else {
cout << "Result: " << num1 / num2 << endl;
}
} else {
cout << "Invalid operator." << endl;
}
}
}
}
return 0;
}
<출력 결과>
Enter first number: 5
Enter an operator (+, -, *, /): /
Enter second number: 0
Division by zero is not allowed.
성적 등급 나누기
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score;
// 조건: 0 <= score <= 100
if (score >= 0 && score <= 100) {
if (score >= 90) {
cout << "Grade: A\n";
}
else if (score >= 80) {
cout << "Grade: B\n";
}
else if (score >= 70) {
cout << "Grade: C\n";
}
else if (score >= 60) {
cout << "Grade: D\n";
}
else {
cout << "Grade: F\n";
}
}
else {
cout << "Invalid score. Please enter a value between 0 and 100.\n";
}
return 0;
}
<출력 결과>
Enter your score: 89
Grade: B
Invalid 값 입력시
Enter your score: 512
Invalid score. Please enter a value between 0 and 100.
날씨의 온도에 따른 외출 여부
#include <iostream>
using namespace std;
int main() {
string weather;
int temperature;
cout << "Enter weather (sunny/rainy): ";
cin >> weather;
cout << "Enter temperature: ";
cin >> temperature;
// 조건: 맑은 날씨(sunny)이고 온도가 20도 이상일 때 외출
if (weather == "sunny" && temperature >= 20) {
cout << "It's a nice day to go out!\n";
}
// 조건: 비가 오거나 너무 추운 날씨
else if (weather == "rainy" || temperature < 10) {
cout << "Better stay indoors.\n";
}
// 나머지 경우
else {
cout << "You can go out, but dress appropriately.\n";
}
return 0;
}
<출력 결과>
Enter weather (sunny/rainy): sunny
Enter temperature: 11
You can go out, but dress appropriately.
동일한 작업을 해주는 반복문
모든 입출력을 하드코딩 할 필요가 없다.
1부터 10까지 합 계산
#include <iostream>
using namespace std;
int main() {
int sum = 0; // 합을 저장할 변수 초기화
for (int i = 1; i <= 10; i++) { // 초기화: i = 1
// 종료 조건: i <= 10
// 사후 동작: i++
sum += i; // 실제 동작: sum에 i를 더함
}
cout << "Sum: " << sum << endl; // 출력: 합계 출력
// 출력값: Sum: 55
return 0;
}
<출력 결과>
Sum: 55
5부터 1까지 출력
#include <iostream>
using namespace std;
int main() {
for (int i = 5; i >= 1; i--) { // 초기화: i = 5
// 종료 조건: i >= 1
// 사후 동작: i--
cout << i << " "; // 실제 동작: i를 출력
}
cout << endl; // 줄 바꿈
// 출력값: 5 4 3 2 1
return 0;
}
<출력결과>
5 4 3 2 1
1부터 20까지 3의 배수 출력
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 20; i++) { // 초기화: i = 1
// 종료 조건: i <= 20
// 사후 동작: i++
if (i % 3 == 0) { // 조건: i가 3의 배수인지 확인
cout << i << " "; // 3의 배수일 때 i를 출력
}
}
cout << endl; // 줄 바꿈
// 출력값: 3 6 9 12 15 18
return 0;
}
<출력 결과>
3 6 9 12 15 18
오른쪽 정렬된 삼각별 출력
#include <iostream>
using namespace std;
int main() {
int n = 5; // 삼각형의 높이
for (int i = 1; i <= n; i++) { // 초기화: i = 1
// 종료 조건: i <= n
// 사후 조건: i++
for (int j = 1; j <= n - i; j++) { // 초기화: j = 1
// 종료 조건: j <= n - i
// 사후 동작: j++
cout << " "; // 실제 동작: 공백 출력
}
for (int j = 1; j <= i; j++) { // 초기화: j = 1
// 종료 조건: j <= i
// 사후 조건: j++
cout << "*"; // 실제 동작: 별 출력
}
cout << endl; // 줄 바꿈
}
// 출력값:
// *
// **
// ***
// ****
// *****
return 0;
}
<출력 결과>
*
**
***
****
*****
사용자 입력 종료
#include <iostream>
using namespace std;
int main() {
int number; // 사용자 입력값을 저장할 변수
cout << "Enter numbers (negative number to stop): ";
cin >> number; // 초기 동작: 첫 번째 입력 받기
while (number >= 0) { // 종료 조건: 입력값이 음수가 아니면 반복
cout << "You entered: " << number << endl; // 실제 동작: 입력값 출력
cin >> number; // 사후 동작: 다음 입력 받기
}
cout << "Program terminated." << endl;
// 출력 예시:
// Enter numbers (negative number to stop): 5
// You entered: 5
// 10
// You entered: 10
// -1
// Program terminated.
return 0;
}
<출력 결과>
Enter numbers (negative number to stop): 1
You entered: 1
2
You entered: 2
-1
Program terminated.
게임 루프
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // 랜덤 시드 초기화
int secretNumber = rand() % 100 + 1; // 1부터 100 사이의 랜덤 숫자
int guess;
cout << "Guess the number (1 to 100): ";
cin >> guess; // 초기 동작: 첫 번째 추측 입력 받기
while (guess != secretNumber) { // 종료 조건: 추측이 정답과 다를 경우 반복
if (guess < secretNumber) {
cout << "Too low! Try again: ";
} else {
cout << "Too high! Try again: ";
}
cin >> guess; // 사후 동작: 새로운 추측 입력 받기
}
cout << "Congratulations! You guessed the number!" << endl;
// 출력 예시:
// Guess the number (1 to 100): 50
// Too low! Try again: 75
// Too high! Try again: 60
// Congratulations! You guessed the number!
return 0;
}
<출력 결과>
Guess the number (1 to 100): 10
Too low! Try again: 50
Too low! Try again: 90
Too high! Try again: 70
Too low! Try again: 80
Too low! Try again: 85
Too high! Try again: 82
Too high! Try again: 81
Congratulations! You guessed the number!
2. C++ 문법 1-4 포인터와 레퍼런스
필기


일반 변수의 대입
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = a;
cout << "초기값 - a: " << a << ", b: " << b << endl;
b = 20;
cout << "변경 후 -a: " << a << ", b: " << b << endl;
return 0;
}
<출력 결과>
초기값 - a: 10, b: 10
변경 후 - a: 10, b: 20
배열의 대입
#include <iostream>
using namespace std;
int main()
{
int arr1[3] = { 1, 2, 3 };
int arr2[3];
// 배열 전체를 한 번에 대입하는 것은 불가능
// arr2 = arr1; //컴파일 오류 발생
//개별 요소 복사
for (int i = 0; i < 3; i++)
{
arr2[i] = arr1[i];
}
// arr2 변경 후 확인
arr2[0] = 100;
cout << "arr1[0]: " << arr1[0] << ", arr2[0]: " << arr2[0] << endl;
return 0;
}
<출력 결과>
arr1[0]: 1, arr2[0]: 100
값 복사 비용
#include <iostream>
using namespace std;
int main(){
int a = 10; // 4바이트 크기의 변수
int b = a; // 변수 a의 값을 b에 복사 (4바이트 비용 발생)
cout << "a: " << a << ", b: " << b << endl;
b = 20; // b의 값만 변경
cout << "변경 후 a: " << a << ", b: " << b << endl;
return 0;
}
<출력 결과>
a: 10, b: 10
변경 후 a: 10, b: 20
값 복사 비용(배열)
#include <iostream>
using namespace std;
int main() {
const int SIZE = 10000000; // 1,000,000개의 정수 (약 4MB)
int arr1[SIZE];
int arr2[SIZE];
// 배열 복사 (매우 높은 복사 비용)
for (int i = 0; i < SIZE; i++) {
arr2[i] = arr1[i];
}
cout << "배열 복사 완료" << endl;
return 0;
}
<출력 결과>
stack overflow 오류 발생.
변수의 주소값 저장
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* p = &a;
cout << "변수 a의 값: " << a << endl;
cout << "변수 a의 주소: " << &a << endl;
cout << "포인터 p의 값(저장된 주소): " << p << endl;
return 0;
}
<출력 결과>
변수 a의 값: 10
변수 a의 주소: 0000008DB28FF734
포인터 p의 값(저장된 주소): 0000008DB28FF734
포인터를 이용한 배열 접근
#include <iostream>
using namespace std;
int main(){
int arr[3] = {10, 20, 30};
int* p = arr;
cout << "p가 가리키는 값: " << *p << endl;
cout << "p+1이 가리키는 값: " << *(p + 1) << endl;
cout << "p+2이 가리키는 값: " << *(p + 2) << endl;
return 0;
}
<예측 결과>
p가 가리키는 값: 10
p+1이 가리키는 값: 20
p+2이 가리키는 값: 30
<출력 결과>
p가 가리키는 값: 10
p+1이 가리키는 값: 20
p+2이 가리키는 값: 30
포인터 변수의 타입과 크기
#include <iostream>
using namespace std;
int main(){
int x = 3;
char y = 'A';
int* ptr1 = &x; // 정수형 변수 x의 주소 저장
char* ptr2 = &y; // 문자형 변수 y의 주소 저장
cout << "ptr1이 가리키는 값: " << *ptr1 << endl;
cout << "ptr2가 가리키는 값: " << *ptr2 << endl;
return 0;
}
<예측 결과>
ptr1이 가리키는 값: 3
ptr2가 가리키는 값: A
<출력 결과>
ptr1이 가리키는 값: 3
ptr2가 가리키는 값: A
포인터를 이용한 주소 확인
#include <iostream>
using namespace std;
int main(){
int x = 3;
char y = 'A';
int* ptr1 = &x;
char* ptr2 = &y;
cout << "x의 주소: " << &x << ", ptr1: " << ptr1 << endl;
cout << "y의 주소: " << (void*)&y << ", ptr2: " << (void*)ptr2 << endl;
return 0;
}
<예측 결과>
x의 주소: (x의 주소값), ptr1: (x의 주소값)
y의 주소: (y의 주소값), ptr2: (y의 주소값)
// 주소값은 실행 할 때마다 바뀔 수 있음
<출력 결과>
x의 주소: 00000081250FF8D4, ptr1: 00000081250FF8D4
y의 주소: 00000081250FF8F4, ptr200000081250FF938
<?> y주소가 같지 않다. 왜일까
오늘의 배운점: 레퍼런스는 포인터와 다르게 특정한 변수의 별명을 지정하는 것일 뿐, 주소를 다른 위치에 저장하는 것이 아니다.
오늘의 아쉬운 점: 포인터를 끝내지 못 했다. 내일은 포인터 진도를 다 나갈 것.