다라다라V
article thumbnail
728x90
반응형
본 글은 "황기태"님의 [명품 C++ Programming]의 연습 문제 답을 공유하고자 작성되었으며, 필자가 직접 문제를 풀며 작성한 것이기에 오류가 있을 수 있습니다. 댓글로 알려주시면 반영하도록 하겠습니다.

시리즈 보기
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 3장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 5장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 6장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 7장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 13장 연습 문제 풀이 (이론 문제)

 

1. 💡 

<cpp />
#include <iostream> using namespace std; int main() { int ch, cnt = 0; cout << "문자를 입력하세요 >>"; while ((ch = cin.get()) != EOF) { if (ch == '\n') break; if (ch == 'a') cnt++; } cout << "a의 개수는 " << cnt << "개 입니다."; }

 

2. 💡 

<cpp />
#include <iostream> using namespace std; int main() { char ch; int cnt = 0; cout << "문자를 입력하세요 >>"; while (true) { cin.get(ch); if (cin.eof() || ch == '\n') break; if (ch == ' ') cnt++; } cout << "공백의 개수는 " << cnt << "개 입니다."; }

 

3. 💡 

<cpp />
#include <iostream> using namespace std; int main() { int ch; cin.ignore(100, ';'); while ((ch = cin.get()) != EOF) { cout << (char)ch; if (ch == '\n') cin.ignore(100, ';'); } cout << endl; }

 

4. 💡 

<cpp />
#include <iostream> using namespace std; int main() { int ch; while ((ch = cin.get()) != EOF) { if (ch == ';') { cout.put('\n'); cin.ignore(100, '\n'); } else cout.put(ch); } cout << endl; }

 

5. 💡 

<cpp />
#include <iostream> #include <string> using namespace std; int main() { string cmd; cout << "cin.get(char*, int)로 문자열을 읽습니다." << endl; while (true) { cout << "종료하려면 exit를 입력하세요 >> "; getline(cin, cmd); if (cmd == "exit") { cout << "프로그램을 종료합니다..."; return 0; } } }

 

6. 💡 

 

<cpp />
#include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { cout.width(15); cout << setw(15) << left << "Number"; cout << setw(15) << left << "Square"; cout << setw(15) << left << "Squre Root" << endl; int i; for (i = 0; i < 50; i += 5) { cout << setw(15) << left << setfill('_') << i; cout << setw(15) << left << setfill('_') << i * i; cout << setw(15) << left << setfill('_') << setprecision(3) << sqrt(i) << endl; } }

 

7. 💡 

<cpp />
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { for (int i = 0; i < 4; i++) { cout << setw(8) << left << "dec"; cout << setw(8) << left << "hexa"; cout << setw(8) << left << "char"; } cout << endl; for (int i = 0; i < 4; i++) { cout << setw(8) << left << "---"; cout << setw(8) << left << "----"; cout << setw(8) << left << "----"; } cout << endl; for (int i = 0; i < 128; i++) { cout << setw(8) << left << i; cout << setw(8) << left << hex << i; if (isprint(i)) cout << setw(8) << left << (char)i; else cout << setw(8) << left << "."; if (i % 4 == 3) cout << endl; } }

 

8. 💡 

<cpp />
class Circle { string name; int radius; public: Circle(int raidus = 1, string name = "") { this->radius = radius; this->name = name; } }; int main() { Circle d, w; cin >> d >> w;// 키보드 입력을 받아 객체 d와 w를 완성 cout << d << w << endl; // 객체 d, w 출력 }
<cpp />
#include <iostream> #include <string> using namespace std; class Circle { string name; int radius; public: Circle(int raidus = 1, string name = "") { this->radius = radius; this->name = name; } friend istream& operator >> (istream& stream, Circle& c); friend ostream& operator << (ostream& stream, Circle c); }; istream& operator >> (istream& stream, Circle& c) { cout << "반지름 >> "; stream >> c.radius; cout << "이름 >> "; stream >> c.name; return stream; }; ostream& operator << (ostream& stream, Circle c) { stream << "(" << c.radius << "인 " << c.name << ")"; return stream; }; int main() { Circle d, w; cin >> d >> w;// 키보드 입력을 받아 객체 d와 w를 완성 cout << d << w << endl; // 객체 d, w 출력 }

 

9. 💡 

<cpp />
class Phone { string name; string telnum; string address; public: Phone(string name="", string telnum = "", string address="") { this->name = name; this->telnum = telnum; this->address = address; } }; int main() { Phone girl, boy; cin >> girl >> boy; cout << girl << endl << boy << endl; }
<cpp />
#include <iostream> #include <string> using namespace std; class Phone { string name, telnum, address; public: Phone(string name="", string telnum = "", string address="") { this->name = name; this->telnum = telnum; this->address = address; } friend istream& operator >> (istream& stream, Phone& p); friend ostream& operator << (ostream& stream, Phone p); }; istream& operator >> (istream& stream, Phone& p) { cout << "이름:"; getline(stream, p.name); cout << "전화번호:"; getline(stream, p.telnum); cout << "주소:"; getline(stream, p.address); return stream; } ostream& operator << (ostream& stream, Phone p) { stream << "(" << p.name << "," << p.telnum << "," << p.address << ")"; return stream; } int main() { Phone girl, boy; cin >> girl >> boy; cout << girl << endl << boy << endl; }

 

10. 💡 

<cpp />
#include <iostream> #include <string> using namespace std; int main() { string password; while (true) { cin >> prompt >> password; if (password == "C++") { cout << "login success!!" << endl; break; } else cout << "login fil. try again!!" << endl; } }
<cpp />
#include <iostream> #include <string> using namespace std; istream& prompt(istream& stream) { cout << "암호?"; return stream; } int main() { string password; while (true) { cin >> prompt >> password; if (password == "C++") { cout << "login success!!" << endl; break; } else cout << "login fil. try again!!" << endl; } }

 

11. 💡 

<cpp />
#include <iostream> using namespace std; int main() { int x, y; cin >> pos >> x; cin >> pos >> y; cout << x << "," << y << endl; }
<cpp />
#include <iostream> using namespace std; istream& pos(istream& stream) { cout << "위치는? "; return stream; } int main() { int x, y; cin >> pos >> x; cin >> pos >> y; cout << x << "," << y << endl; }

 

12. 💡 

// 객체 지향적 처리를 위한 고민만해서 재료가 없을 때 만들지 못하는 처리를 하지 않았습니다. 추후 만들도록 하겠습니다.

<cpp />
#include <iostream> #include <string> using namespace std; class Basic { protected: string name; int coffee, sugar, cream, water, cup; public: Basic(string name) { this->name = name; } string getName() { return name; } virtual void useCoffee(int& coffee, int use) { coffee -= use; } virtual void useSugar(int& sugar, int use) { sugar -= use; } virtual void useCream(int& cream, int use) { cream -= use; } virtual void useWater(int& water, int use) { water -= use; } virtual void useCup(int& cup, int use) { cup -= use; } virtual void show(int& coffee, int& sugar, int& cream, int& water, int& cup) { cout << "Coffee" << "\t"; printStar(coffee); cout << "Sugar" << "\t"; printStar(sugar); cout << "Cream" << "\t"; printStar(cream); cout << "Water" << "\t"; printStar(water); cout << "Cup" << "\t"; printStar(cup); } void printStar(int n) { for (int i = 0; i < n; i++) { cout << "*"; } cout << endl; } void fill(int& coffee, int& sugar, int& cream, int& water, int& cup) { coffee = 3; sugar = 3; cream = 3; water = 3; cup = 3; show(coffee, sugar, cream, water, cup); } }; class Coffee : public Basic { public: Coffee() : Basic("맛있는 보통 커피") {} void makeCoffe(int& coffee, int& water, int& cup) { cout << getName() << " 나왔습니다~~" << endl; useCoffee(coffee, 1); useWater(water, 1); useCup(cup, 1); } }; class SugarCoffee : public Basic { public: SugarCoffee() : Basic("맛있는 설탕 커피") {} void makeCoffe(int& coffee, int& sugar, int& water, int& cup) { cout << getName() << " 나왔습니다~~" << endl; useCoffee(coffee, 1); useSugar(sugar, 1); useWater(water, 1); useCup(cup, 1); } }; class BlackCoffe : public Basic { public: BlackCoffe() : Basic("맛있는 보통 커피") {} void makeCoffe(int& coffee, int& cup) { cout << getName() << " 나왔습니다~~" << endl; useCoffee(coffee, 1); useCup(cup, 1); } }; int main() { int coffee = 3, sugar = 3, cream = 3, water = 3, cup = 3; cout << "----- 명품 커피 자판키 켭니다. -----" << endl; Basic tmp(""); tmp.show(coffee, sugar, cream, water, cup); Coffee c; SugarCoffee sc; BlackCoffe bc; int select; while (true) { cout << endl << endl; cout << "보통 커피:0, 설탕 커피:1, 블랙 커피:2, 채우기:3, 종료:4>> "; cin >> select; switch (select) { case 0: c.makeCoffe(coffee, water, cup); c.show(coffee, sugar, cream, water, cup); break; case 1: sc.makeCoffe(coffee, sugar, water, cup); sc.show(coffee, sugar, cream, water, cup); break; case 2: bc.makeCoffe(coffee, cup); bc.show(coffee, sugar, cream, water, cup); break; case 3: tmp.fill(coffee, sugar, cream, water, cup); break; case 4: return 0; } } }
반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!