251219 TIL - C++ 10일차
2025. 12. 19. 20:58ㆍ언리얼 7기 본캠프
오늘은 따로 문법보단 응집보다 결합도, SOLID원칙에 대해 배웠고, 이를 통해 코드를 좀 더 좋게 쓰는법을 배움
그래서 복습도 할 겸 코테도 잔뜩 하고 알고리즘 강의때 작성한 코드도 완성함
주말동안에 과제 준비해서 월요일에 마무리하면 될 것 같음
//LinkedList 구현
#include <iostream>
#include <string>
using namespace std;
class Node {
private:
string data;
Node* next;
public:
Node(string data) {
this->data = data;
this->next = nullptr;
}
friend class LinkedList;
};
class LinkedList {
private:
Node* head;
int nodeCount;
public:
LinkedList(string value) {
this->head = new Node(value);
this->nodeCount = 1;
}
void append(string value) {
Node* curr = this->head;
while (curr->next != nullptr) {
curr = curr->next;
}
cout << "curr->next:" << curr->next << endl;
curr->next = new Node(value);
this->nodeCount++;
}
Node* getNode(int index) {
if (index < 0 || index >= nodeCount) {
throw std::out_of_range("유효하지 않은 인덱스");
}
Node* node = this->head;
for (int i = 0; i < index; i++) {
node = node->next;
}
return node;
}
void addNode(int index, string value) {
Node* newNode = new Node(value);
if (index == 0) {
newNode->next = this->head;
this->head = newNode;
this->nodeCount++;
return;
}
Node* node = this->getNode(index - 1);
Node* nextNode = node->next;
node->next = newNode;
newNode->next = nextNode;
this->nodeCount++;
}
};
//클래스 분리 실습
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class fileIOSystem { // 파일 저장, 로드 클래스 분리
private:
string saveName; // 임시로 세이브명 저장할 string만 생성
public:
// 파일 저장 - X
void saveToFile(const string& filename)
{
saveName = filename;
cout << "[Manager] 아이템 목록을 " << filename << "(으)로 저장 완료." << endl;
}
// 파일 로드
void loadFromFile(const string& filename)
{
// 실제 파일 입출력 로직 생략
cout << "[Manager] 아이템 목록을 " << filename << "(으)로부터 불러오기 완료." << endl;
}
};
class InventoryManager // 아이템 추가, 제거, 목록 불러오기 클래스
{
private:
vector<string> items;
public:
// 아이템 목록 관리 - O
void addItem(const string& itemName)
{
items.push_back(itemName);
cout << "[Manager] " << itemName << "을/를 인벤토리에 추가했습니다." << endl;
}
// 아이템 목록 관리 - O
void removeItem(const string& itemName)
{
// 실제 삭제 로직 생략
cout << "[Manager] " << itemName << "을/를 인벤토리에서 제거했습니다." << endl;
}
const vector<string>& getItems() const {
return items;
}
};
int main()
{
InventoryManager manager;
fileIOSystem file;
manager.addItem("HP Potion");
manager.addItem("Sword of Light");
cout << "현재 인벤토리 내용물 : ";
auto& item = manager.getItems(); // 벡터 참고를 위해 미리 꺼내옴
for (int i = 0; i < item.size(); i++) { // 인벤토리 내용 출력 테스트
cout << item[i];
if (i != item.size() - 1) {
cout << ", ";
}
}
cout << endl;
file.saveToFile("savegame.dat"); // 저장은 이제 fileIOSystem 클래스가 처리함
return 0;
}
//코테 나선형 문제
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> solution(int n) {
vector<vector<int>> answer(n, vector<int>(n, 0));
int x = 0, y = 0;
int dy[4] = { 0, 1, 0, -1 };
int dx[4] = { 1, 0, -1, 0 };
int dir = 0;
for (int i = 0; i < n * n; i++) {
answer[y][x] = i + 1;
int next_y = y + dy[dir];
int next_x = x + dx[dir];
bool oorCheck = (next_x < 0 || next_x >= n || next_y < 0 || next_y >= n);
if (oorCheck || answer[next_y][next_x] != 0) {
dir = (dir + 1) % 4;
next_y = y + dy[dir];
next_x = x + dx[dir];
}
x = next_x;
y = next_y;
}
return answer;
}
//코테 더 크게 합치기
#include <string>
#include <vector>
using namespace std;
int solution(int a, int b) {
int answer = 0;
string tempa, tempb;
string str_a = to_string(a);
string str_b = to_string(b);
tempa += str_a + str_b;
tempb += str_b + str_a;
if (stoi(tempa) > stoi(tempb)) {
answer = stoi(tempa);
}
else answer = stoi(tempb);
return answer;
}'언리얼 7기 본캠프' 카테고리의 다른 글
| 251223 TIL - C++ 12일차, 5번 과제 발제 (0) | 2025.12.23 |
|---|---|
| 251222 TIL - C++ 11일차 (0) | 2025.12.22 |
| 251218 TIL - C++ 9일차 (0) | 2025.12.18 |
| 251217 TIL - C++ 8일차 (0) | 2025.12.17 |
| 251216 TIL - C++ 7일차, 새로운 과제 하달 (1) | 2025.12.16 |