C++ Programming Ch.3 Exercise 9 Solution

Problem: Oval 클래스는 주어진 사각형에 내접하는 타원을 추상화한 클래스이다. Oval 클래스의 멤버는 모두 다음과 같다. Oval 클래스를 선언부와 구현부로 나누어 작성하라. ● 정수값의 사각형 너비와 높이를 가지는 width, height 변수 멤버 ● 너비와 높이 값을 매개 변수로 받는 생성자 ● 너비와 높이를 1로 초기화하는 매개 변수 없는 생성자 ● width와 height를 출력하는 소멸자 ● 타원이 너비를 리턴하는 getWidth() 함수 멤버 ● 타원의 높이를 리턴하는 getHeight() 함수 멤버 ● 타원의 너비와 높이를 변경하는 set(int w, int h) 함수 멤버 ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 8 Solution

Problem: int 타입의 정수를 객체화한 Integer 클래스를 작성하라. Integer의 모든 멤버 함수를 자동 인라인으로 작성하라. Integer 클래스를 활용하는 코드는 다음과 같다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include using namespace std; int main() { Integer n(30); cout << n.get() << ' '; // 30 출력 n.set(50); cout << n.get() << ' '; // 50 출력 Integer m("300"); cout << m.get() << ' '; // 300 출력 cout << m.isEven(); // true(정수로 1) 출력 } Objective & Hints: ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 7 Solution

Problem: 문제 5번을 참고하여 생성자를 이용하여 짝수 홀수를 선택할 수 있도록 SelectableRandom 클래스를 작성하고 짝수 10개, 홀수 10개를 랜덤하게 발생시키는 프로그램을 작성하라. 2020/03/03 - [C++/명품 C++ programming] - 명품 C++ programming Exercise Problem 3장 5번 [명품 C++ programming Exercise Problem 3장 5번 Problem: 랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용하여 랜덤 한 정수를 10개 출력하는 사례는 다음과 같다. Random 클래스가 생성자, next(), nextInRange()의 3개의 멤버 함수를 가지도.. sobamemil.tistory.com](https://sobamemil.tistory.com/47) ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 6 Solution

Problem: 문제 5번을 참고하여 짝수 정수만 랜덤하게 발생시키는 EvenRandom 클래스를 작성하고 EvenRandom 클래스를 이용하여 10개의 짝수를 랜덤하게 출력하는 프로그램을 완성하라. 0도 짝수로 처리한다. 2020/03/03 - [C++/명품 C++ programming] - 명품 C++ programming Exercise Problem 3장 5번 [명품 C++ programming Exercise Problem 3장 5번 Problem: 랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용하여 랜덤 한 정수를 10개 출력하는 사례는 다음과 같다. Random 클래스가 생성자, next(), nextInRange()의 3개의 멤버 함수를 가지도.. ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 5 Solution

Problem: 랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용하여 랜덤 한 정수를 10개 출력하는 사례는 다음과 같다. Random 클래스가 생성자, next(), nextInRange()의 3개의 멤버 함수를 가지도록 작성하고 main() 함수와 합쳐 하나의 cpp 파일에 구현하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main() { Random r; cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10 개--" << endl; for(int i=0; i<10; i++) { int n = r.next(); // 0에서 RAND_MAX(32767) 사이의 랜덤한 정수 cout << n << ' '; } cout << endl << endl << "-- 2에서 " << "4 까지의 랜덤 정수 10 개 --" << endl; for(int i=0; i<10; i++) { int n = r.nextInRange(2,4); // 2에서 4 사이의 랜덤한 정수 cout << n << ' '; } cout << endl; } Objective & Hints: ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 4 Solution

Problem: CoffeeMachine 클래스를 만들어보자. main() 함수와 Execution Result가 다음과 같도록 CoffeeMachine 클래스를 작성하라. 에스프레소 한 잔에는 커피와 물이 각각 1씩 소비되고, 아메리카노의 경우 커피는 1, 물은 2가 소비되고, 설탕 커피는 커피 1, 물 2, 설탕 1이 소비된다. CoffeeMachine 클래스에는 어떤 멤버 변수와 어떤 멤버 함수가 필요한지 잘 판단하여 작성하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { CoffeeMachine java(5, 10, 3); // 커피량:5, 물량:10, 설탕:6 으로 초기화 java.drinkEspresso(); // 커피 1, 물 1 소비 java.show(); // 현재 커피 머신의 상태 출력 java.drinkAmericano(); // 커피 1, 물 2 소비 java.show(); // 현재 커피 머신의 상태 출력 java.drinkSugarCoffee(); // 커피 1, 물 2, 설탕 1 소비 java.show(); // 현재 커피 머신의 상태 출력 java.fill(); // 커피 10, 물 10, 설탕 10 으로 채우기 java.show(); // 현재 커피 머신의 상태 출력 Execution Result: ...

March 3, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 3 Solution

Problem: 은행에서 사용하는 프로그램을 작성하기 위해, 은행 계좌 하나를 표현하는 클래스 Account가 필요하다. 계좌 정보는 계좌의 주인, 계좌 번호, 잔액을 나타내는 3 멤버 변수로 이루어진다. main() 함수의 실행과 결과가 다음과 같도록 Account 클래스를 작성하라. 1 2 3 4 5 6 7 8 9 10 #include using namespace std; int main() { Account a("Kitae", 1, 5000); // id 1번, 잔액 5000원, 이름이 Kitae인 계좌 성 a.deposit(50000); // 50000원 저금 cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl; int money = a.withdraw(20000); // 20000원 출금. withdraw()는 출금한 실제 금액 리턴 cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl; } Execution Result: ...

March 2, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 2 Solution

Problem: 날짜를 다루는 Date 클래스를 작성하고자 한다. Date를 이용하는 main()과 Execution Result는 다음과 같다. 클래스 Date를 작성하여 아래 프로그램에 추가하라. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { Date virth(2014, 3, 20); Date independenceDay("1945/8/15"); independenceDay.show(); cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl; } Execution Result: Objective & Hints: 2개의 생성자와 여러 멤버를 가진 클래스 만들기 ...

March 2, 2020 · 2 min · Sobamemil

C++ Programming Ch.3 Exercise 1 Solution

Problem: 아래 main()의 Execution Result가 다음과 같도록 Tower 클래스를 작성하라. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { Tower myTower; // 1 미터 Tower seoulTower(100); // 100 미터 cout << "높이는 " << myTower.getHeight() << "미터" << endl; cout << "높이는 " << seoulTower.getHeight() << "미터" << endl; } Objective & Hints: 2개의 생성자와 여러 멤버를 가진 클래스 만들기 ...

March 2, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 16 Solution

Problem: 영문 텍스트를 입력받아 알파벳 히스토그램을 그리는 프로그램을 작성하라. 대문자는 모두 소문자로 집계하며, 텍스트 입력의 끝은 ';' 문자로 한다. Objective & Hints: 문자열 읽기, C++ 프로그램 종합 응용 Input File: 입력할 텍스트 파일을 첨부하였습니다. [2-16.txt 0.00MB](https://blog.kakaocdn.net/dna/EsAHK/btqChFfPBnw/AAAAAAAAAAAAAAAAAAAAAOhcwRebixdRiSrFB2r5c8Z4mBZDeCt4ibUUEp2n5aCw/2-16.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=PEoHUr5ktP9MyFLvskuQj0jS1Q8%3D&attach=1&knm=tfile.txt) 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 #include #include using namespace std; int main() { int tot=0; // 총 알파벳 개수 int alpha[27]={0}; // 각각의 알파벳을 나타냄 char text[10000]; // text를 저장할 배열 cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다.\n텍스트의 끝은 ; 입니다. 10000개까지 가능합니다.\n"; cin.getline(text,10000,';'); // 최대 10,000개의 영문 텍스트를 ';'전까지 입력받는다 for (int i = 0; i<strlen(text); i++) { // text[0]부터 text의 끝까지 if (isalpha(text[i])) { // text[i]가 알파벳이면 참 if(text[i]<91) text[i] = tolower(text[i]); // text[i]가 대문자이면 소문자로 변경 tot++; alpha[text[i] - 97]++; // 각각의 알파벳이 몇개인지 카운트 } } cout << "총 알파벳 수 " << tot << endl << endl; for (int i = 0; i < 26; ++i) { // 알파벳의 소문자 총 개수는 26개 (a~z) cout << (char)(i+'a') << "(" << alpha[i] << ")"; // a부터 z까지 출력 후 각 (알파벳의 개수) 출력 cout << "\t: "; // 포맷을 출력하기 위해 탭과 ":" 출력 for (int j = 1; j <= alpha[i]; j++) // 각 알파벳의 개수만큼 "*" 출력 cout << "*"; cout << endl; } } Explanation: ...

February 28, 2020 · 2 min · Sobamemil