C++ Programming Ch.4 Exercise 5 Solution

Problem: string 클래스를 이용하여 사용자가 입력한 영문 한 줄을 입력받고 글자 하나만 랜덤하게 수정하여 출력하는 프로그램을 작성하라. Objective & Hints: string 클래스로 문자열 다루기 랜덤 정수를 발생시키기 위해 다음 두 라인의 코드가 필요하며, 와 헤더 파일을 include 해야 한다. 1 2 srand((unsinged)time(0)); // 시작할 때마다, 다른 랜덤수를 발생시키기 위한 seed 설정 int n = rand(); // 0에서 RAND_MAX(32767) 사이의 랜덤한 정수 발생 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 #include #include #include #include using namespace std; int main() { string sent; int n; cout << "아래 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" ; while(true){ srand((unsigned)time(0)); cout << "\n>>"; getline(cin,sent); if(sent == "exit") break; int length = sent.length(); while(true){ n = rand()%length; if(sent[n]!=' ') break; } int a = rand()%25+95; // 임의의 문자 하나 선택 sent[n] = (char)a; cout << sent; } } Explanation: ...

March 4, 2020 · 1 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