C++ Programming Ch.11 Exercise 2 Solution

Problem: istream& get(char& ch) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ผ์ธ์„ ์ฝ๊ณ  ๋นˆ์นธ(' ')์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Execution Result: Objective & Hints: cin์œผ๋กœ ํ‚ค ์ž…๋ ฅ ์—ฐ์Šต Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include using namespace std; int main() { char ch; int cnt; while(true){ cin.get(ch); // ํ‚ค๋ฅผ ch์— ์ฝ์–ด์˜ด if(cin.eof()) // EOF ๋ฌธ์ž ์ฆ‰ ctrl-z ํ‚ค๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, ์ฝ๊ธฐ ์ข…๋ฃŒ break; if(ch == '\n') // ํ‚ค๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ ์ฝ๊ธฐ ์ค‘๋‹จ break; else if(ch == ' ') cnt++; } cout << cnt; }

March 27, 2020 ยท 1 min ยท Sobamemil

C++ Programming Ch.11 Exercise 1 Solution

Problem: int cin.get() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ํ•œ ๋ผ์ธ์„ ์ฝ๊ณ  'a'๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Execution Result: Objective & Hints: cin์œผ๋กœ ํ‚ค ์ž…๋ ฅ ์—ฐ์Šต Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { int a; int cnt=0; while( (a = cin.get() ) != EOF ){ if(a == 'a') cnt++; else if(a == '\n') break; } cout << cnt; } Explanation: ...

March 27, 2020 ยท 1 min ยท Sobamemil

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

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

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

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

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

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

C++ Programming Ch.10 Exercise 10 Solution

Problem: ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์— vector๋ฅผ ํ™œ์šฉํ•ด๋ณด์ž. ๋‚˜๋ผ ์ด๋ฆ„(nation)๊ณผ ์ˆ˜๋„(capital) ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋œ Nation ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , vector v;๋กœ ์ƒ์„ฑํ•œ ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‚˜๋ผ ์ด๋ฆ„๊ณผ ์ˆ˜๋„ ์ด๋ฆ„์„ ์‚ฝ์ž…ํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๋žœ๋คํ•˜๊ฒŒ ํ€ด์ฆˆ๋ฅผ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ๋ฒกํ„ฐ์— Nation ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋ฏธ๋ฆฌ ์‚ฝ์ž…ํ•˜์—ฌ ํ€ด์ฆˆ๋ฅผ ๋ณด๋„๋ก ํ•˜๋ผ. ์‹คํ–‰ ํ™”๋ฉด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ์ €์ž๋Š” 9๊ฐœ ๋‚˜๋ผ์˜ ์ด๋ฆ„๊ณผ ์ˆ˜๋„๋ฅผ ๋ฏธ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฝ์ž…ํ•˜์˜€๋‹ค. ๋ฌธ์ž์—ด์€ string ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋ผ. Execution Result: Objective & Hints: vector์— ๊ฐ์ฒด์˜ ์‚ฝ์ž…, ๊ฒ€์ƒ‰ ์‘์šฉ ์—ฐ์Šต ...

March 10, 2020 ยท 2 min ยท Sobamemil

C++ Programming Ch.10 Exercise 9 Solution

Problem: STL์˜ vector ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์ž. vector ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์ •์ˆ˜๋ฅผ ๋ฒกํ„ฐ์— ์‚ฝ์ž…ํ•˜๊ณ  ์ง€๊ธˆ๊นŒ์ง€ ์ž…๋ ฅ๋œ ์ˆ˜์™€ ํ‰๊ท ์„ ์ถœ๋ ฅํ•œ๋А ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. 0์„ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค. Execution Result: Objective & Hints: vector ์ปจํ…Œ์ด๋„ˆ ํ™œ์šฉ ์—ฐ์Šต ์ •์ˆ˜๋งŒ ๋‹ค๋ฃจ๋Š” ๋ฒกํ„ฐ์ด๋ฏ€๋กœ vector v;๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. iterator๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†๋‹ค. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include using namespace std; int main() { vector v; double sum=0; while(true){ int tmp; cout << "์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(0์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ)>>"; cin >> tmp; if(!tmp) break; //์ž…๋ ฅํ•œ ์ •์ˆ˜๊ฐ€ 0์ด๋ฉด ์ข…๋ฃŒ v.push_back(tmp); // ์ž…๋ ฅํ•œ ์ •์ˆ˜๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด v์— ์‚ฝ์ž… for(int i=0; i<v.size(); i++) // vector v์˜ ๋ชจ๋“  ์›์†Œ ์ถœ๋ ฅ cout << v.at(i) << ' '; cout << endl; sum += tmp; cout << "ํ‰๊ท  = " << sum/v.size() << endl; } } Explanation: ...

March 10, 2020 ยท 1 min ยท Sobamemil