명품 C++ Programming 교재 2장 실습문제 풀이입니다. 개인 풀이이므로 더 효율적인 풀이가 있을 수는 있으나 문제에서 요구하는 출력 조건은 모두 맞춘 해답 코드입니다.
1. cout과 << 연산자를 이용하여, 1에서 100까지의 정수를 다음과 같이 한 줄에 10개씩 출력하라. 각 정수는 탭으로 분리하여 출력하라.
// 실습 1번
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 11; j++)
cout << 10*i + j << '\t';
cout << endl;
}
}
2. cout과 << 연산자를 이용하여 다음과 같이 구구단을 출력하는 프로그램을 작성하라.
// 실습 2번
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
cout << i << 'x' << j << '=' << i * j << '\t';
}
cout << endl;
}
}
3. 키보드로부터 두 개의 정수를 읽어 큰 수를 화면에 출력하라.
// 실습 3번
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "두 수를 입력하라>>";
cin >> a >> b;
if (a > b) cout << "큰 수 = " << a;
else cout << "큰 수 = " << b;
}
4. 소수점을 가지는 5개의 실수를 입력 받아 제일 큰 수를 화면에 출력하라.
// 실습 4번
#include <iostream>
using namespace std;
int main() {
cout << "5 개의 실수를 입력하라>>";
double num[5];
//cin >> num[0] >> num[1] >> num[2] >> num[3] >> num[4];
for (int i = 0; i < 5; i++) cin >> num[i];
double max = num[0];
for (int i = 1; i < 5; i++) {
if (max <= num[i]) max = num[i];
}
cout << "제일 큰 수 = " << max << endl;
}
5. <Enter> 키가 입력될 때까지 문자들을 읽고, 입력된 문자 'x'의 개수를 화면에 출력하라.
// 실습 5번
#include <iostream>
#include <cstring>
using namespace std;
int main() {
cout << "문자들을 입력하라(100개 미만)." << endl;
char words[100];
cin.getline(words, 100, '\n');
int count = 0;
for (int i = 0; i < strlen(words); i++) if (words[i] == 'x') count += 1;
cout << "x의 개수는 " << count << endl;
}
6. 문자열을 두 개 입력받고 두 개의 문자열이 같은지 검사하는 프로그램을 작성하라. 만일 같으면 "같습니다", 아니면 "같지 않습니다"를 출력하라.
// 실습 6번
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
cout << "새 암호를 입력하세요>>";
cin >> password;
string password_check;
cout << "새 암호를 다시 한 번 입력하세요>>";
cin >> password_check;
if (password == password_check) cout << "같습니다." << endl;
else cout << "같지 않습니다." << endl;
}
7. 다음과 같이 "yes"가 입력될 때까지 종료하지 않는 프로그램을 작성하라. 사용자로부터 입력은 cin.getline() 함수를 사용하라.
// 실습 7번
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string, getline() 을 사용하는 방법
/*
string check;
while (1)
{
cout << "종료하고 싶으면 yes를 입력하세요>>";
getline(cin, check);
if (check == "yes") { cout << "종료합니다..."; break; }
}
*/
char check[100];
while (1)
{
cout << "종료하고 싶으면 yes를 입력하세요>>";
cin.getline(check, 100, '\n');
if (!strcmp(check, "yes")) { cout << "종료합니다..."; break; }
}
}
8. 한 라인에 ';'으로 5개의 이름을 구분하여 입력받아, 각 이름을 끊어내어 화면에 출력하고 가장 긴 이름을 판별하라.
// 실습 8번
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[100];
string max_length_name;
int max = 0;
cout << "5 명의 이름을 ';'으로 구분하여 입력하세요" << endl;
cout << ">>";
for (int i = 0; i < 5; i++)
{
cin.getline(name, 100, ';');
cout << i+1 << " : " << name << endl;
if (max < strlen(name))
{
max = strlen(name);
max_length_name = name;
}
}
cout << "가장 긴 이름은 " << max_length_name << endl;
}
9. 이름, 주소, 나이를 입력받아 다시 출력하는 프로그램을 작성하라.
// 실습 9번
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string address;
int age;
cout << "이름은?";
getline(cin, name);
cout << "주소는?";
getline(cin, address);
cout << "나이는?";
cin >> age;
cout << name << ", " << address + ", " << age << "세" << endl;
}
10. 문자열을 하나 입력받고 문자열의 부분 문자열을 다음과 같이 출력하는 프로그램을 작성하라.
// 실습 10번
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
cout << "문자열 입력>>";
getline(cin, word);
for (int i = 0; i < size(word); i++) {
for (int j = 0; j < i + 1; j++) {
cout << word[j];
}
cout << endl;
}
}
11. 다음 C 프로그램을 C++ 프로그램으로 수정하여 실행하라.
// 실습 11번
#include <iostream>
using namespace std;
int main()
{
int k, n = 0;
int sum = 0;
cout << "끝 수를 입력하세요>>";
cin >> n;
for (k = 1; k <= n; k++) {
sum += k;
}
cout << sum;
cout << "1에서 " << k << "까지의 합은 " << sum << "입니다." << endl;
}
12. 다음 C 프로그램을 C++ 프로그램으로 수정하여 실행하라. 실행 결과는 11번과 같다.
// 실습 12번
#include <iostream>
using namespace std;
int sum(int a, int b);
int main()
{
int n = 0;
cout << "끝 수를 입력하세요>>";
cin >> n;
cout << "1에서 " << n << "까지의 합은 " << sum(1, n) << "입니다." << endl;
}
int sum(int a, int b) {
int k = 0, res = 0;
for (k = a; k <= b; k++) {
res += k;
}
return res;
}
C 에서는 함수의 원형을 선언할 때 매개변수를 생략해도 괜찮았지만 C++ 에서는 함수 중복이 있기 때문에 매개변수도 맞춰서 원형을 사용해야만 한다.
13. 중식당의 주문 과정을 C++ 프로그램으로 작성
// 실습 13번
#include <iostream>
using namespace std;
int main()
{
int menu;
int servings;
cout << "***** 승리장에 오신 것을 환영합니다. *****" << endl;
while (1) {
cout << "짬뽕:1, 짜장:2, 군만두:3, 종료:4>>";
cin >> menu;
if (menu > 4) {
cout << "다시 주문하세요!!" << endl;
continue;
}
else if (menu == 4) {
cout << "오늘 영업은 끝났습니다." << endl;
break;
}
cout << "몇인분?";
cin >> servings;
if (menu == 3) cout << "군만두 " << servings << "인분 나왔습니다" << endl;
else if (menu == 2) cout << "짜장 " << servings << "인분 나왔습니다" << endl;
else cout << "짬뽕 " << servings << "인분 나왔습니다" << endl;
}
}
14. 커피를 주문하는 간단한 C++ 프로그램 작성. 메뉴는 "에스프레소", "아메리카노", "카푸치노"의 3가지이며 가격은 각각 2000원, 2300원, 2500원이다. 하루에 20000원 이상 벌게 되면 카페를 닫는다.
// 실습 14번
#include <iostream>
#include <string>
using namespace std;
int main()
{
char coffee[100];
int num = 0;
int sales = 0;
cout << "에스프레소 2000원, 아메리카노 2300원, 카푸치노 2500원입니다." << endl;
while (1) {
if (sales >= 20000) {
cout << "오늘 " << sales << "원을 판매하여 카페를 닫습니다. 내일 봐요~~~" << endl;
break;
}
cout << "주문>> ";
cin >> coffee >> num;
if (!strcmp(coffee, "에스프레소"))
{ sales += 2000 * num;
cout << 2000 * num << "원입니다. 맛있게 드세요" << endl;
}
else if (!strcmp(coffee, "아메리카노")) {
sales += 2300 * num;
cout << 2300 * num << "원입니다. 맛있게 드세요" << endl;
}
else if (!strcmp(coffee, "카푸치노")) {
sales += 2500 * num;
cout << 2500 * num << "원입니다. 맛있게 드세요" << endl;
}
else cout << "다시 주문해주세요" << endl;
}
}
15. 정수 5칙 연산을 할 수 있는 프로그램을 작성하라. 정수와 연산자는 하나의 빈칸으로 분리된다.
// 실습 15번
#include <iostream>
#include <string>
using namespace std;
int main()
{
while (1)
{
char op[2];
int n1, n2;
cout << "? ";
cin >> n1 >> op >> n2;
if (!strcmp(op, "+")) cout << n1 << " + " << n2 << " = " << n1 + n2 << endl;
else if (!strcmp(op, "-")) cout << n1 << " - " << n2 << " = " << n1 - n2 << endl;
else if (!strcmp(op, "*")) cout << n1 << " * " << n2 << " = " << n1 * n2 << endl;
else if (!strcmp(op, "/")) cout << n1 << " / " << n2 << " = " << n1 / n2 << endl;
else if (!strcmp(op, "%")) cout << n1 << " % " << n2 << " = " << n1 % n2 << endl;
else cout << "다시 입력해주세요" << endl;
}
}
16. 영문 텍스트를 입력받아 알파벳 히스토그램을 그리는 프로그램을 작성하라. 대문자는 모두 소문자로 집계하며, 텍스트 입력의 끝은 ';' 문자로 한다.
// 실습 16번
#include <iostream>
#include <string>
using namespace std;
int main()
{
char text[10000];
int alpha[26] = { 0 };
int num = 0;
cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다." << endl;
cout << "텍스트의 끝은 ; 입니다. 10000개까지 가능합니다." << endl;
cin.getline(text, 10000, ';');
for (int i = 0; i < strlen(text); i++) {
if (isalpha(text[i]))
{
num += 1;
text[i] = tolower(text[i]);
alpha[text[i] - 97] += 1;
}
}
cout << "총 알파벳 수" << num << endl << endl;
for (int i = 0; i < 26; i++) {
cout << (char)(97 + i) << " (" << alpha[i] << ")" << "\t: ";
for (int j = 0; j < alpha[i]; j++)
cout << '*';
cout << endl;
}
}
'C++ > PS' 카테고리의 다른 글
[C++ PS] 명품 C++ Programming 실습 문제 5장 풀이 (0) | 2023.07.10 |
---|---|
[C++ PS] 명품 C++ Programming 실습 문제 4장 풀이 (0) | 2023.07.07 |
[C++ PS] 명품 C++ Programming 실습 문제 3장 풀이 (0) | 2023.07.04 |