C++ Programming Ch.4 Exercise 13 Solution

Problem: 영문자로 구성된 텍스트에 대해 각 알파벳에 해당하는 문자가 몇 개인지 출력하는 히스토그램 클래스 Histogram을 만들어보자. 대문자는 모두 소문자로 변환하여 처리한다. Histogram 클래스를 활용하는 사례와 Execution Result는 다음과 같다. 1 2 3 4 5 Histogram elvisHisto("Wise men say, only fools rush in But I can't help, "); elvisHisto.put("falling in love with you"); elvisHisto.putc('-'); elvisHisto.put("Elvis Presley"); elvisHisto.print(); Execution Result: Objective & Hints: 클래스 만들기 종합 응용 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 #include #include using namespace std; class Histogram { string sent; public: Histogram(string sent); void put(string sent); void putc(char c); void print(); }; Histogram::Histogram(string text){ sent = text; cout << sent << endl; } void Histogram::put(string text){ sent += text; cout << text; } void Histogram::putc(char c){ sent += c; cout << c; } void Histogram::print(){ int alpha[26] = { 0 }; int num = 0; for (int i = 0; i < sent.length(); i++) { if(isalpha(sent[i])){ char c = tolower(sent[i]); alpha[c - 'a']++; num++; } } cout << endl << endl; cout << "총 알파벳 수 " << num; cout << endl << endl; for (int i = 0; i < 26; ++i) { char c = 'a' + i; cout << c << " (" << alpha[i] << ")\t: "; for (int j = 0; j < alpha[i]; ++j) { cout << "*"; } cout << endl; } } int main() { Histogram elvisHisto("Wise men say, only fools rush in But I can't help, "); elvisHisto.put("falling in love with you"); elvisHisto.putc('-'); elvisHisto.put("Elvis Presley"); elvisHisto.print(); } Explanation: ...

March 4, 2020 · 2 min · Sobamemil

C++ Programming Ch.2 Exercise 15 Solution

Problem: 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/), 나머지(%)의 정수 5척 연산을 할 수 있는 프로그램을 작성하라. 식은 다음과 같은 형식으로 입력된다. 정수와 연산자는 하나의 빈칸으로 분리된다. Objective & Hints: 공백을 포함하는 문자열 읽기, C++ 프로그램 종합 응용 한 줄을 문자열로 읽고, 공백 문자를 찾아 연산자와 두 개의 피연산자를 구분한 후, 계산하면 됩니다. 문자열을 정수로 바꿀 때 atoi() 함수를 이용하면 됩니다. 예를 들면 atoi("34") = 34 입니다. 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 #include #include #include using namespace std; int main(){ char sic[100]; char *symbol; int front,rear; while(true){ cout << "? "; cin.getline(sic,100); front = atoi(strtok(sic, " ")); symbol = strtok(NULL, " "); rear = atoi(strtok(NULL, " ")); if(*symbol == '+'){ cout << front << " + " << rear << " = " << front+rear << endl; } else if(*symbol == '-'){ cout << front << " - " << rear << " = " << front-rear << endl; } else if(*symbol == '*'){ cout << front << " * " << rear << " = " << front*rear << endl; } else if(*symbol == '/'){ cout << front << " / " << rear << " = " << front/rear << endl; } else if(*symbol == '%'){ cout << front << " % " << rear << " = " << front%rear << endl; } } return 0; } Explanation: ...

February 28, 2020 · 2 min · Sobamemil

C++ Programming Ch.2 Exercise 14 Solution

Problem: 커피를 주문하는 간단한 C++ 프로그램을 작성해보자. 커피 종류는 "에스프레소", "아메리카노", "카푸치노"의 3가지이며 가격은 각각 2000원, 2300원, 2500원이다. 하루에 20000원 이상 벌게 되면 카페를 닫는다. Execution Result와 같이 작동하는 프로그램을 작성하라. Objective & Hints: C++ 프로그램 구성. cin, strcmp() 활용 종합 연습 char coffee[100]; int num; cin >> coffee >> num; 으로 커피 이름과 잔 수를 입력받으면 됩니다. 또한 커피는 if(strcmp(coffee, "에스프레소") == 0 )과 같이 비교하면 됩니다. Execution Result: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 8 Solution

Problem: 한 라인에 ';' 으로 5개의 이름을 구분하여 입력받아, 각 이름을 끊어내어 화면에 출력하고 가장 긴 이름을 판별하라. Objective & Hints: cin.getline()으로 문자열 읽기 ';'까지 문자열을 읽고자 하면 다음 코드를 사용하고 1 2 char name[100]; cin.getline(name, 100, ';'); 5개 읽어야 하니 5번 루프를 돈다. 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 #include #include using namespace std; int main() { int A=0; int i; char name[100]; char longName[100]; cout << "5 명의 이름을 ';' 으로 구분하여 입력하세요 \n>>"; for(i=1;i<6;i++){ cin.getline(name,100,';'); cout << i << " : " << name << endl; if(A < strlen(name)) { A = strlen(name); strcpy(longName,name); } } cout << "가장 긴 이름은 " << longName; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 7 Solution

Problem: 다음과 같이 "yes"가 입력될 때까지 종료하지 않는 프로그램을 작성하라. 사용자로부터의 입력은 cin.getline() 함수를 사용하라. Objective & Hints: 공백을 포함하는 문자열 읽기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include using namespace std; int main() { char A[] = "yes"; // 배열 선언시 "yes"로 초기화 char B[100]; while(true){ cout << "종료하고 싶으면 yes를 입력하세요>>"; cin.getline(B,100); if(strcmp(A,B) == 0) break; } cout << "종료합니다..."; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 6 Solution

Problem: 문자열을 두 개 입력받고 두 개의 문자열이 같은지 검사하는 프로그램을 작성하라. 만일 같으면 "같습니다", 아니면 "같지 않습니다"를 출력하라. Objective & Hints: 공백 없이 입력된 문자열 읽기 (빈칸 없이 입력) Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include using namespace std; int main() { char pw1[100]; char pw2[100]; cout << "새 암호를 입력하세요>>"; cin.getline(pw1,100); cout << "새 암호를 다시 한 번 입력하세요>>"; cin.getline(pw2,100); if(strcmp(pw1,pw2)==0) cout << "같습니다"; else cout << "같지 않습니다"; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil