πŸ’» Dev & CSiOS & Others

Binary Search Tree Array Implementation

Problem: 배열을 μ΄μš©ν•˜μ—¬ 이원 탐색 트리λ₯Ό λ§Œλ“€κ³  νƒμƒ‰ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. μž…λ ₯ : 정렬이 λ˜μ§€ μ•Šμ€ μˆ«μžλ“€ ν”„λ‘œκ·Έλž¨ : 2.1 μž…λ ₯된 μˆ«μžλ“€μ„ ν•˜λ‚˜μ”© μ½μœΌλ©΄μ„œ 이원 탐색 트리 λ°°μ—΄ λ§Œλ“€κΈ° 2.2 숫자 ν•˜λ‚˜λ₯Ό μž…λ ₯ν•˜λ©΄ μ΄μ›νƒμƒ‰νŠΈλ¦¬ μ•Œκ³ λ¦¬μ¦˜μ„ μ μš©ν•˜μ—¬ ν•΄λ‹Ήν•˜λŠ” λ°°μ—΄μ˜ 첨자λ₯Ό 좜λ ₯ν•˜κΈ° (이 λ•Œ 좜λ ₯은 λ°°μ—΄ μ›μ†Œλ“€μ„ μ°¨λ‘€λŒ€λ‘œ 좜λ ₯ν•˜κ³  ν•΄λ‹Ήν•˜λŠ” λ°°μ—΄ 첨자λ₯Ό 좜λ ₯) Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #include using namespace std; void newBinarySearchTree(int *num_arr, int size, int *new_num_arr) { for(int i=0; i<size+20; i++){ // μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄ -1둜 μ΄ˆκΈ°ν™” new_num_arr[i] = -1; } new_num_arr[0] = num_arr[0]; for(int i=1; i<size; i++){ int index=0; // μƒˆλ‘œ λ“€μ–΄μ˜¬ μˆ«μžκ°€ μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄μ˜ λ£¨νŠΈλ³΄λ‹€ μž‘μœΌλ©΄ μ™Όμͺ½μœΌλ‘œ 이동 if(new_num_arr[0] > num_arr[i]) { for(int j=2*index+1;;) { if(new_num_arr[j] != -1){ //μ‚½μž…ν•˜λ €λŠ” μ΄μ›νƒμƒ‰νŠΈλ¦¬ 배열곡간이 NULL이 μ•„λ‹ˆλ©΄ 비ꡐ if(new_num_arr[j] < num_arr[i]) j=2*j+2; // μ‚½μž…ν•˜κ³ μž ν•˜λŠ” μˆ«μžκ°€ 더 크면 2j+2 else if(new_num_arr[j] > num_arr[i]) j=2*j+1; // μ‚½μž…ν•˜κ³ μž ν•˜λŠ” μˆ«μžκ°€ 더 μž‘μœΌλ©΄ 2j+1 z else { //같은 μˆ«μžκ°€ λ‚˜μ˜€λ©΄ 였λ₯˜λ©”μ‹œμ§€ 좜λ ₯ ν›„ ν”„λ‘œκ·Έλž¨ 비정상 μ’…λ£Œ cout << "same data error...\n"; exit(1); } } else if(new_num_arr[j] == -1) { // μ‚½μž…ν•˜λ €λŠ” μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄ 곡간이 NULL이면 λ°”λ‘œ μ‚½μž… new_num_arr[j] = num_arr[i]; break; } } } // μƒˆλ‘œ λ“€μ–΄μ˜¬ μˆ«μžκ°€ μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄μ˜ λ£¨νŠΈλ³΄λ‹€ 크면 였λ₯Έμͺ½μœΌλ‘œ 이동 else if(new_num_arr[0] < num_arr[i]) { for(int j=2*index+2;;) { if(new_num_arr[j] != -1){ // μ‚½μž…ν•˜λ €λŠ” μ΄μ›νƒμƒ‰νŠΈλ¦¬ 배열곡간이 NULL이 μ•„λ‹ˆλ©΄ 비ꡐ if(new_num_arr[j] < num_arr[i]) j=2*j+2; else if(new_num_arr[j] > num_arr[i]) j=2*j+1; else { cout << "same data error...\n"; exit(1); } } else if(new_num_arr[j] == -1) { new_num_arr[j] = num_arr[i]; break; } } } else continue; } } void find(int *new_num_arr, int size) { int x, flag; cout << "찾고자 ν•˜λŠ” 숫자 μž…λ ₯ : "; cin >> x; for(int i=0; i<size; i++) // μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œ 좜λ ₯ cout << "arr[" << i << "] : " << new_num_arr[i] << endl; for(int i=0; i<size; i++){ if(new_num_arr[i] == x) { cout << "index : " << i; flag = true; break; } else flag = false; } if(!flag) // flagκ°€ false이면 찾고자 ν•˜λŠ” μˆ«μžκ°€ μ—†λ‹€κ³  좜λ ₯ cout << "찾고자 ν•˜λŠ” 숫자 μ—†μŒ\n"; } int main() { int num_arr[] = {50, 40, 55, 30, 45, 54, 53, 1, 60, 301, 2}; int num_arr_size = sizeof(num_arr)/sizeof(num_arr[0]); int *new_num_arr = new int [num_arr_size + 20]; // μ΄μ›νƒμƒ‰νŠΈλ¦¬ λ°°μ—΄ 곡간 생성 newBinarySearchTree(num_arr, num_arr_size, new_num_arr); find(new_num_arr, num_arr_size + 20); } Explanation: ...

March 19, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSSystem & Embedded

μ‹œμŠ€ν…œ ν”„λ‘œκ·Έλž˜λ° ν”„λ‘œμ νŠΈ #7 μ΅œμ’… (Assembler in C)

Problem: μ§€κΈˆκΉŒμ§€μ˜ ν”„λ‘œμ νŠΈλ₯Ό μ°Έκ³ ν•˜μ—¬ 2 pass assemblerλ₯Ό λ§Œλ“€λ©΄ λ©λ‹ˆλ‹€. λ¨Όμ € μ–΄μ…ˆλΈ”λŸ¬(Assembler)λž€? ν•˜λ“œμ›¨μ–΄κ°€ 직접 μ΄ν•΄ν•˜μ—¬ μ‹€ν–‰ν•˜λŠ” κΈ°κ³„μ–΄λŠ” 일반적으둜 λΉ„νŠΈ μ—΄ λ˜λŠ” 16μ§„μˆ˜λ‘œ ν‘œν˜„λ˜κΈ° λ•Œλ¬Έμ— 인간이 μ΄ν•΄ν•˜κΈ° μ–΄λ ΅λ‹€. κ·Έλž˜μ„œ 인간이 μ΄ν•΄ν•˜κΈ° 쉽도둝 기계어와 거의 μΌλŒ€μΌλ‘œ λŒ€μ‘ν•˜λŠ” 기호둜 ν‘œν˜„λœ μ–Έμ–΄λ‘œ μ–΄μ…ˆλΈ”λŸ¬ μ–Έμ–΄κ°€ 있으며, μ–΄μ…ˆλΈ”λŸ¬ μ–Έμ–΄λ₯Ό κΈ°κ³„μ–΄λ‘œ λ²ˆμ—­ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μ–΄μ…ˆλΈ”λŸ¬, λ²ˆμ—­ν•˜λŠ” 것을 μ–΄μ…ˆλΈ”μ΄λΌκ³  ν•©λ‹ˆλ‹€. μ–΄μ…ˆλΈ”λŸ¬μ˜ 역할을 그림으둜 κ°„λ‹¨ν•˜κ²Œ λ‚˜νƒ€λ‚΄ 보면 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. 이 κΈ€μ—μ„œ κ΅¬ν˜„ ν•  2 패슀 μ–΄μ…ˆλΈ”λŸ¬μ˜ μ•Œκ³ λ¦¬μ¦˜μ„ λ³΄κ² μŠ΅λ‹ˆλ‹€. pass 1 : ...

March 19, 2020 Β· 6 min Β· Sobamemil
πŸ’» Dev & CS

Merge Sort Algorithm

합병 μ •λ ¬(Merge Sort)μ΄λž€? λΆ„ν•  정볡 μ•Œκ³ λ¦¬μ¦˜(=Divide and conquer algorithm 즉, κ·ΈλŒ€λ‘œ ν•΄κ²°ν•  수 μ—†λŠ” 문제λ₯Ό μž‘μ€ 문제둜 λΆ„ν• ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” λ°©λ²•μ΄λ‚˜ μ•Œκ³ λ¦¬μ¦˜μž…λ‹ˆλ‹€.)의 ν•˜λ‚˜λ‘œ O(n log n)의 μ‹œκ°„ λ³΅μž‘λ„λ₯Ό κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€. 합병 μ •λ ¬μ˜ μž‘λ™ μ•Œκ³ λ¦¬μ¦˜μ€ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 리슀트의 길이가 1 μ΄ν•˜μ΄λ©΄ 이미 μ •λ ¬λœ κ²ƒμœΌλ‘œ λ³Έλ‹€. κ·Έλ ‡μ§€ μ•Šμ€ κ²½μš°μ—λŠ” λΆ„ν• (divide) : μ •λ ¬λ˜μ§€ μ•Šμ€ 리슀트λ₯Ό 절반으둜 잘라 λΉ„μŠ·ν•œ 크기의 두 λΆ€λΆ„ 리슀트둜 λ‚˜λˆˆλ‹€. 정볡(conquer) : 각 λΆ€λΆ„ 리슀트λ₯Ό μž¬κ·€μ μœΌλ‘œ 합병 정렬을 μ΄μš©ν•΄ μ •λ ¬ν•œλ‹€. κ²°ν•©(combine) : 두 λΆ€λΆ„ 리슀트λ₯Ό λ‹€μ‹œ ν•˜λ‚˜μ˜ μ •λ ¬λœ 리슀트둜 ν•©λ³‘ν•œλ‹€. μ΄λ•Œ μ •λ ¬ κ²°κ³Όκ°€ μž„μ‹œλ°°μ—΄μ— μ €μž₯λœλ‹€. 볡사(copy) : μž„μ‹œ 배열에 μ €μž₯된 κ²°κ³Όλ₯Ό μ›λž˜ 배열에 λ³΅μ‚¬ν•œλ‹€. 이해가 잘 μ•ˆκ°„λ‹€λ©΄ μ•„λž˜ μ• λ‹ˆλ©”μ΄μ…˜μ„ 톡해 μž‘λ™ 원리λ₯Ό μ‰½κ²Œ 이해할 수 μžˆμŠ΅λ‹ˆλ‹€. ...

March 18, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CS

Insertion Sort Algorithm

μ‚½μž… μ •λ ¬(Insertion Sort)μ΄λž€? 자료 λ°°μ—΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό μ•žμ—μ„œλΆ€ν„° μ°¨λ‘€λŒ€λ‘œ 이미 μ •λ ¬λœ λ°°μ—΄ λΆ€λΆ„κ³Ό λΉ„κ΅ν•˜μ—¬, μžμ‹ μ˜ μœ„μΉ˜λ₯Ό μ°Ύμ•„ μ‚½μž…ν•¨μœΌλ‘œμ¨ 정렬을 μ™„μ„±ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μž…λ‹ˆλ‹€. μ‚½μž… μ •λ ¬μ˜ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n2)이며 μ•ˆμ • μ •λ ¬μž…λ‹ˆλ‹€. λ˜ν•œ 배열이 κΈΈμ–΄μ§ˆμˆ˜λ‘ 효율이 맀우 λ–¨μ–΄μ§€μ§€λ§Œ κ΅¬ν˜„μ΄ κ°„λ‹¨ν•˜λ‹€λŠ” μž₯점이 μžˆμŠ΅λ‹ˆλ‹€. μ‚½μž… μ •λ ¬μ˜ 예 μ‚½μž… μ •λ ¬μ˜ μ• λ‹ˆλ©”μ΄μ…˜ -Simpsons contributor- μ‚½μž… μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ„ κ΅¬ν˜„ν•˜κΈ° 전에 μ˜μ‚¬μ½”λ“œ(Pseudocode)둜 λ¨Όμ € 이해λ₯Ό ν•˜κ³  μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” 것이 더 μ‰½κ²Œ μž‘μ„±ν•  수 μžˆμ„ κ²ƒμž…λ‹ˆλ‹€. Pseudocode : 1 2 3 4 5 6 7 8 9 //InsertionSort pseudo code InsertionSort(A,n) // sort A[1...n] for j <- 2 to n do key <- A[j] i <- j-1 while i>0 and A[i]>key do A[i+1] <- A[i] i <- i-1 A[i+1] <- key CCode: ...

March 17, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 16 Solution

Problem: vector<Shape*> v;λ₯Ό μ΄μš©ν•˜μ—¬ κ°„λ‹¨ν•œ κ·Έλž˜ν”½ νŽΈμ§‘κΈ°λ₯Ό μ½˜μ†” λ°”νƒ•μœΌλ‘œ λ§Œλ“€μ–΄λ³΄μž. μƒμ„±λœ λ„ν˜• 객체λ₯Ό v에 μ‚½μž…ν•˜κ³  κ΄€λ¦¬ν•˜λΌ. 9μž₯ Exercise Problem 10번의 힌트λ₯Ό μ°Έκ³ ν•˜λΌ. Shapeκ³Ό Circle, Line, Rect ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: vectorλ₯Ό ν™œμš©ν•˜λŠ” μ’…ν•© μ‘μš© Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 #include #include using namespace std; class Shape { protected: virtual void draw() = 0; public: void paint() { draw(); } }; class Circle : public Shape { protected: virtual void draw(){ cout << "Circle" << endl; } }; class Rect : public Shape { protected: virtual void draw() { cout << "Rectangle" << endl; } }; class Line : public Shape { protected: virtual void draw() { cout << "Line" << endl; } }; class UI { public: static int seleteMenu() { int n; cout << "μ‚½μž…:1, μ‚­μ œ:2, λͺ¨λ‘λ³΄κΈ°:3, μ’…λ£Œ:4 >> "; cin >> n; return n; } static int seleteShape() { int n; cout << "μ„ :1, 원:2, μ‚¬κ°ν˜•:3 >> "; cin >> n; return n; } static int seleteDelIndex() { int n; cout << "μ‚­μ œν•˜κ³ μž ν•˜λŠ” λ„ν˜•μ˜ 인덱슀 >> "; cin >> n; return n; } static void showAll(vector<Shape*> &v, vector<Shape*>::iterator &it) { int i=0; for(it = v.begin();it!=v.end(); it++, i++){ // vector v의 첫 μ›μ†ŒλΆ€ν„° 끝 μ›μ†ŒκΉŒμ§€ 탐색 및 좜λ ₯ cout << i << ": "; v.at(i)->paint(); } } }; class GraphicEditor { vector<Shape*> v; vector<Shape*>::iterator it; public: GraphicEditor() { cout << "κ·Έλž˜ν”½ μ—λ””ν„°μž…λ‹ˆλ‹€.\n"; start(); } void start() { while(true){ int n; n = UI::seleteMenu(); switch(n){ case 1: //μ‚½μž…μ„ μ„ νƒν•œ 경우 n = UI::seleteShape(); switch(n){ case 1: //선을 μ„ νƒν•œ 경우 v.push_back(new Line()); break; case 2: //원을 μ„ νƒν•œ 경우 v.push_back(new Circle()); break; case 3: //μ‚¬κ°ν˜•μ„ μ„ νƒν•œ 경우 v.push_back(new Rect()); break; default: cout << "잘λͺ» μ„ νƒν•˜μ…¨μŠ΅λ‹ˆλ‹€.\n"; break; } break; case 2:{ //μ‚­μ œλ₯Ό μ„ νƒν•œ 경우 n = UI::seleteDelIndex(); if(n >= v.size()

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 15 Solution

Problem: vectorλ₯Ό μ΄μš©ν•˜μ—¬ μ•„λž˜ Circle 클래슀의 객체λ₯Ό μ‚½μž…ν•˜κ³  μ‚­μ œν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. μ‚­μ œ μ‹œμ—λŠ” 이름이 같은 λͺ¨λ“  원을 μ‚­μ œν•œλ‹€. 1 2 3 4 5 6 7 8 9 10 class Circle { string name; // 이름 int radius; // λ°˜μ§€λ¦„ public: Circle(int radius, string name) { this->radius = radius; this->name = name; } double getArea() { return 3.14*radius*radius; } string getName() { return name; } }; Execution Result: ...

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 14 Solution

Problem: μ•”ν˜Έ 관리 μ‘μš©ν”„λ‘œκ·Έλž¨μ„ map을 μ΄μš©ν•˜μ—¬ μž‘μ„±ν•˜λΌ. μ‹€ν–‰ 과정은 λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: map μ»¨ν…Œμ΄λ„ˆμ— μ‚½μž… 및 쑰회 μ‘μš© 이름과 점수λ₯Ό 쌍으둜 μ €μž₯ν•  λ§΅ μ»¨ν…Œμ΄λ„ˆλ‘œ map<string, string>을 μ΄μš©ν•˜λ©΄ λ©λ‹ˆλ‹€. μ•„λž˜ 링크에 μžˆλŠ” Exercise Problem 10μž₯ 13λ²ˆμ„ μ°Έκ³ ν•˜μ„Έμš”. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include #include using namespace std; void insert(map<string, string> &pwManager){ string name, pw; cout << "이름 μ•”ν˜Έ>> "; cin >> name >> pw; pwManager.insert(make_pair(name, pw)); } void checkNamePw(map<string, string> &pwManager){ string name, pw; cout << "이름? "; cin >> name; while(true){ cout << "μ•”ν˜Έ? "; cin >> pw; if(pwManager[name] == pw){ cout << "톡과!!\n"; break; } else cout << "μ‹€νŒ¨~~\n"; // 틀리면 좜λ ₯ ν›„ λ‹€μ‹œ μ•”ν˜Έ 질문 } } int main() { map<string, string> pwManager; cout << "***** μ•”ν˜Έ 관리 ν”„λ‘œκ·Έλž¨ WHOλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€ *****\n"; while(true){ cout << "μ‚½μž…:1, 검사:2, μ’…λ£Œ3>> "; int n; cin >> n; switch(n){ case 1: insert(pwManager); break; case 2: checkNamePw(pwManager); break; case 3: cout << "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€..."; return 0; } } } Explanation: ...

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 13 Solution

Problem: map μ»¨ν…Œμ΄λ„ˆλ₯Ό μ΄μš©ν•˜μ—¬ (이름, 성적)을 μ €μž₯ν•˜κ³  μ΄λ¦„μœΌλ‘œ 성적을 μ‘°νšŒν•˜λŠ” 점수 관리 ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€μ–΄λΌ. 이름은 빈칸 없이 μž…λ ₯ν•˜λŠ” 것을 μ›μΉ™μœΌλ‘œ ν•œλ‹€. Execution Result: Objective & Hints: map μ»¨ν…Œμ΄λ„ˆ ν™œμš© 이름과 점수λ₯Ό 쌍으둜 μ €μž₯ν•  λ§΅ μ»¨ν…Œμ΄λ„ˆλ‘œ map<string, int>λ₯Ό μ΄μš©ν•˜λ©΄ λœλ‹€. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include #include #include using namespace std; int main() { map<string, int> Score; // map μ»¨ν…Œμ΄λ„ˆ 생성. ν‚€λŠ” ν•œκΈ€ 이름, 값은 μ •μˆ˜ 점수 cout << "***** μ μˆ˜κ΄€λ¦¬ ν”„λ‘œκ·Έλž¨ HIGH SCORE을 μ‹œμž‘ν•©λ‹ˆλ‹€ *****\n"; while(true){ int num; int score; string name; cout << "μž…λ ₯:1, 쑰회:2, μ’…λ£Œ:3 >> "; cin >> num; switch (num){ case 1: cout << "이름과 점수>> "; cin >> name >> score; Score.insert(make_pair(name, score)); // map에 μ €μž₯ break; case 2: cout << "이름 >> "; cin >> name; if(Score.find(name) == Score.end()) // name 'ν‚€'λ₯Ό λκΉŒμ§€ μ°Ύμ•˜λŠ”λ° μ—†μŒ cout << "μ—†μŒ" << endl; else cout << name << "의 μ μˆ˜λŠ” " <<Score[name] << endl; // Scoreμ—μ„œ name의 값을 μ°Ύμ•„ 좜λ ₯ break; case 3: cout << "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€...\n"; return 0; default : cout << "μ œλŒ€λ‘œ μž…λ ₯ν•΄\n"; break; } } } Explanation: ...

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 12 Solution

Problem: Open Challengeλ₯Ό μˆ˜μ •ν•˜μ—¬ μ‚¬μš©μžκ°€ μ–΄νœ˜λ₯Ό μ‚½μž…ν•  수 μžˆλ„λ‘ κΈ°λŠ₯을 μΆ”κ°€ν•˜λΌ. Execution ResultλŠ” λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: vector μ»¨ν…Œμ΄λ„ˆμ˜ μ’…ν•© μ‘μš© μ—°μŠ΅ 랜덀 μ •μˆ˜λ₯Ό λ°©μƒμ‹œν‚€κΈ° μœ„ν•΄ λ‹€μŒ 두 라인의 μ½”λ“œκ°€ ν•„μš”ν•˜λ©°, κ³Ό λ₯Ό include ν•΄μ•Ό ν•©λ‹ˆλ‹€. 1 2 srand((unsigned)time(0)); // μ‹œμž‘ν•  λ•Œλ§ˆλ‹€, λ‹€λ₯Έ 랜덀수λ₯Ό λ°œμƒμ‹œν‚€κΈ° μœ„ν•œ seed μ„€μ • int n = rand(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜κ°€ n에 λ°œμƒ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 #include #include #include #include using namespace std; class Word { string engWord, korWord; public: Word(string engWord=0, string korWord=0){ this->engWord = engWord; this->korWord = korWord; } void inputWord(string engWord, string korWord){ this->engWord = engWord; this->korWord = korWord; } string getEngWord(){ return engWord; } string getKorWord(){ return korWord; } }; void inputWord(vector &v) { cout << "μ˜μ–΄ 단어에 exit을 μž…λ ₯ν•˜λ©΄ μž…λ ₯ 끝\n"; string engWord, korWord; while(true){ cout << "μ˜μ–΄ >>"; cin >> engWord; if(engWord == "exit") break; cout << "ν•œκΈ€ >>"; cin >> korWord; v.push_back(Word(engWord, korWord)); } } void gameStart(vector &v) { srand((unsigned)time(0)); int num; cout << "μ˜μ–΄ μ–΄νœ˜ ν…ŒμŠ€νŠΈλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€. 1~4 μ™Έ λ‹€λ₯Έ μž…λ ₯μ‹œ μ’…λ£Œ.\n"; while(true){ int randNum = rand()%v.size(); cout << v.at(randNum).getEngWord() << "?\n"; string ex[4] = " "; int tmp = rand()%4; ex[tmp] = v.at(randNum).getKorWord(); for(int i=0; i<4; i++){ if(tmp == i) continue; while(true){ // μ€‘λ³΅λ˜λŠ” μˆ«μžκ°€ μ—†κ²Œ 처리 int numChosen = rand()%v.size(); if(ex[0]!=v.at(numChosen).getKorWord() && ex[1]!=v.at(numChosen).getKorWord() && ex[2]!=v.at(numChosen).getKorWord() && ex[3]!=v.at(numChosen).getKorWord()){ ex[i] = v.at(numChosen).getKorWord(); break; } } } for(int i=0; i<4; i++) cout << "(" << i+1 << ") " << ex[i] << ' '; cout << ":>"; cin >> num; // 숫자만 μž…λ ₯ if(num == -1

March 10, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 11 Solution

Problem: μ±…μ˜ 년도, 책이름, μ €μž 이름을 담은 Book 클래슀λ₯Ό λ§Œλ“€κ³ , vector v;둜 μƒμ„±ν•œ 벑터λ₯Ό μ΄μš©ν•˜μ—¬ 책을 μž…κ³ ν•˜κ³ , μ €μžμ™€ λ…„λ„λ‘œ κ²€μƒ‰ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. Execution Result: Objective & Hints: vector에 객체의 μ‚½μž…, 검색 μ‘μš© μ—°μŠ΅ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include #include #include using namespace std; class Book{ int year; string b_name; string p_name; public: void set(int year, string b_name, string p_name){ this->year = year; this->b_name = b_name; this->p_name = p_name; } string getP(){ return p_name; } int getY(){ return year; } void show(){ cout << year << "년도, " << b_name << ", " << p_name << endl; } }; int main() { vector v; Book b; int year; string b_name; string p_name; cout << "μž…κ³ ν•  책을 μž…λ ₯ν•˜μ„Έμš”. 년도에 -1을 μž…λ ₯ν•˜λ©΄ μž…κ³ λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€.\n"; while(true){ cout << "년도>>"; cin >> year; if(year==-1) break; fflush(stdin); cout << "책이름>>"; getline(cin, b_name); cout << "μ €μž>>"; getline(cin, p_name); b.set(year, b_name, p_name); v.push_back(b); } cout << "총 μž…κ³ λœ 책은 " << v.size() << "ꢌ μž…λ‹ˆλ‹€.\n"; cout << "κ²€μƒ‰ν•˜κ³ μž ν•˜λŠ” μ €μž 이름을 μž…λ ₯ν•˜μ„Έμš”>>"; fflush(stdin); getline(cin, p_name); for(int i=0; i<v.size(); i++){ if(v[i].getP() == p_name) v[i].show(); } cout << "κ²€μƒ‰ν•˜κ³ μž ν•˜λŠ” 년도λ₯Ό μž…λ ₯ν•˜μ„Έμš”>>"; cin >> year; for(int i=0; i<v.size(); i++){ if(v[i].getY() == year) v[i].show(); } } Explanation: ...

March 10, 2020 Β· 2 min Β· Sobamemil