본문 바로가기
문제풀이/명품 C++ Programming

[명품 C++ Programming] 2장 실습 문제

by 민됴리 2020. 12. 11.
반응형

2018년에 3월 5일에 발행된

황기태 저자의

명품 C++ Programming 개정판

2장 실습 문제입니다.

 

저작권을 준수하기 위해서

책에 나와있는 문제를 요약해서 적었습니다.

 

궁금한 점은 댓글로 남겨주세요.

 


 

2-1. 1에서 100까지의 정수를 한 줄에 10개씩 출력하기

#include <stdio.h>

int main(void) {

    int a = 20;
    int b = -5;

    printf("%d + %d = %d", a, b, a + b);

    return 0;
}

 

2-2. 구구단 출력 프로그램 작성

#include <iostream>
using namespace std;

int main(void) {

	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			cout << i << "x" << j << "=" << i * j << "\t";
		}
		cout << endl;
	}

	return 0;
}

 

2-3. 입력한 두 개의 정수 중 큰 수를 화면에 출력

#include <iostream>
using namespace std;

int main(void) {

	int num1, num2;
	int bigNum;

	cout << "두 수를 입력하라>>";
	cin >> num1 >> num2;
	if (num1 > num2)
		bigNum = num1;
	else
		bigNum = num2;
	cout << "큰 수 = " << bigNum;

	return 0;
}

 

2-4. 입력받은 5개의 실수 중 제일 큰 수 출력

#include <iostream>
using namespace std;

int main(void) {

	double num[5];
	double bigNum;

	cout << "5개의 실수를 입력하라>>";
	for (int i = 0; i < 5; i++) {
		cin >> num[i];
	}

	bigNum = num[0];
	for (int i = 1; i < 5; i++) {
		if (bigNum < num[i])
			bigNum = num[i];
	}

	cout << "제일 큰 수 = " << bigNum;

	return 0;
}

 

2.5. 입력받은 문자 중 'x'의 개수 출력

#include <iostream>
using namespace std;

int main(void) {

	char str1[100];
	int count = 0;

	cout << "문자들을 입력하라(100개 미만)." << endl;
	cin.getline(str1, 100);
	for (int i = 0; str1[i]; i++) {
		if (str1[i] == 'x')
			count++;
	}

	cout << "x의 개수는 " << count;

	return 0;
}

 

2-6. 입력받은 두 개의 문자열이 같은지 검사

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char str1[50];
	char str2[50];

	cout << "새 암호를 입력하세요>>";
	cin >> str1;
	cout << "새 암호를 다시 한 번 입력하세요>>";
	cin >> str2;

	if (strcmp(str1, str2) == 0) {
		cout << "같습니다";
	}
	else {
		cout << "같지 않습니다";
	}

	return 0;
}

 

2-7. "yes"가 입력될 때 종료하는 프로그램

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char varificationStr[100] = "yes";
	char inputStr[100];

	while (true) {
		cout << "종료하고싶으면 yes를 입력하세요>>";
		cin >> inputStr;
		if (strcmp(varificationStr, inputStr) == 0) {
			break;
		}
		else {
			continue;
		}
	}
	
	cout << "종료합니다...";

	return 0;
}

 

2-8. ';'로 5개의 이름을 구분하여 입력받아 출력

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char name[5][100];

	cout << "5명의 이름을 ';'으로 구분하여 입력하세요" << endl;
	
	for (int i = 0; i < 5; i++) {
		cin.getline(name[i], 100, ';');
	}

	for (int i = 0; i < 5; i++) {
		cout << i + 1 << ": " << name[i] << endl;
	}

	int longestIndex = 0;

	for (int i = 1; i < 5; i++) {
		if (strlen(name[longestIndex]) < strlen(name[i]))
			longestIndex = i;
	}

	cout << "가장 긴 이름은 " << name[longestIndex];

	return 0;
}

 

2-9. 이름, 주소, 나이 입력받아 출력

#include <iostream>
using namespace std;

int main(void) {

	char name[100];
	char address[100];
	int age;

	cout << "이름은? ";
	cin.getline(name, 100);
	cout << "주소는? ";
	cin.getline(address, 100);
	cout << "나이는? ";
	cin >> age;

	cout << name << ", " << address << " " << age << "세";

	return 0;
}

 

2-10. 입력받은 문자열의 부분 문자열 출력

#include <iostream>
using namespace std;

int main(void) {

	char str[100];

	cin >> str;

	for (int i = 0; str[i]; i++) {
		for (int j = 0; j <= i; j++) {
			cout << str[j];
		}
		cout << endl;
	}

	return 0;
}

 

2-11. 주어진 C 프로그램을 C++ 프로그램으로 수정

#include <iostream>
using namespace std;

int main(void) {

	int k, n = 0;
	int sum = 0;
	cout << "끝 수를 입력하세요>>";
	cin >> n;
	for (int k = 1; k <= n; k++) {
		sum += k;
	}
	cout << 1 << "에서 " << n << "까지의 합은  " << sum << " 입니다." << endl;

	return 0;
}

 

2-12. C 프로그램은 C++ 프로그램으로 수정

#include <iostream>
using namespace std;

int sum(int a, int b);

int main(void) {

	int n = 0;
	cout << "끝 수를 입력하세요>>";
	cin >> n;
	cout << 1 << "에서 " << n << "까지의 합은  " << sum(1, n) << " 입니다." << endl;

	return 0;
}

int sum(int a, int b) {
	int res = 0;
	for (int i = a; i <= b; i++) {
		res += i;
	}
	return res;
}

 

2-13. 중식당의 주문 과정을 프로그램으로 구현

#include <iostream>
using namespace std;

int main(void) {

	int choice;	//선택지 저장
	int dishes; //몇 인분인지 저장

	cout << "***** 장짜장에 오신 것을 환영합니다. *****" << endl;
	while (true) {
		cout << "짬뽕: 1, 짜장: 2, 군만두: 3, 종료: 4 >> ";
		cin >> choice;

		if (choice > 4 || choice < 1) {
			cout << "다시 주문하세요!" << endl;
			continue;
		}
		if (choice == 4) {
			cout << "오늘 영업은 끝났습니다." << endl;
			return 0;
		}

		cout << "몇인분? ";
		cin >> dishes;

		switch(choice) {
		case 1:
			cout << "짬뽕 " << dishes << "인분 나왔습니다.";
			break;
		case 2:
			cout << "짜장 " << dishes << "인분 나왔습니다.";
			break;
		case 3:
			cout << "군만두 " << dishes << "인분 나왔습니다.";
			break;
		default:
			break;
		}
	}

	return 0;
}

 

2-14. 커피를 주문하는 간단한 C++ 프로그램

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char choice[100];
	int amount;	//개수
	int earnings; //소득
	int totalEarnings = 0; //총소득

	cout << "에스프레소 2000원, 아메리카노 2300원, 카푸치노 2500원입니다." << endl;
	while (true) {
		cout << "주문>> ";
		cin >> choice >> amount;
		if (!strcmp(choice, "에스프레소")) {
			earnings = 2000 * amount;
		}
		else if (!strcmp(choice, "아메리카노")) {
			earnings = 2300 * amount;
		}
		else if (!strcmp(choice, "카푸치노")) {
			earnings = 2500 * amount;
		}
		cout << earnings << "원입니다. 맛있게 드세요" << endl;

		totalEarnings += earnings;
		if (totalEarnings > 20000) {
			cout << "오늘 " << totalEarnings << "원을 판매하여 카페를 닫습니다. 내일 봐요~~~";
			break;
		}
	}

	return 0;
}

 

2-15. 정수 5칙 연산을 할 수 있는 프로그램

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char num1[10];
	char num2[10];
	char oper[10];

	while (true) {
		cout << "? ";
		cin.getline(num1, 10, ' ');
		cin.getline(oper, 10, ' ');
		cin.getline(num2, 10);
		

		if (!strcmp(oper, "+")) {
			cout << num1 << " + " << num2 << " = " << atoi(num1) + atoi(num2) << endl;
		}
		else if (!strcmp(oper, "-")) {
			cout << num1 << " - " << num2 << " = " << atoi(num1) - atoi(num2) << endl;
		}
		else if (!strcmp(oper, "*")) {
			cout << num1 << " * " << num2 << " = " << atoi(num1) * atoi(num2) << endl;
		}
		else if (!strcmp(oper, "/")) {
			cout << num1 << " / " << num2 << " = " << atoi(num1) / atoi(num2) << endl;
		}
		else if (!strcmp(oper, "%")) {
			cout << num1 << " % " << num2 << " = " << atoi(num1) % atoi(num2) << endl;
		}
	}

	return 0;
}

 

2-16. 영문 텍스트를 입력받아 알파벳 히스토그램을 그리는 프로그램

#include <iostream>
#include <cstring>
using namespace std;

int main(void) {

	char text[10000];
	int totalAlpha = 0;
	int numAlpha[26] = { 0, };

	cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다." << endl;
	cout << "텍스트의 끝은 ; 입니다. 10000개까지 가능합니다." << endl;
	cin.getline(text, 10000, ';');

	for (int i = 0; text[i]; i++) {
		if (isalpha(text[i])) {
			totalAlpha++;
			(numAlpha[tolower(text[i]) - 'a'])++;
		}
	}

	cout << "총 알파벳 수 " << totalAlpha << endl << endl;

	char alphabet;
	for (int i = 0; i < 26; i++) {
		alphabet = i + 'a';
		cout << alphabet << " (" << numAlpha[i] << ") \t : ";
		for (int j = 0; j < numAlpha[i]; j++) {
			cout << "*";
		}
		cout << endl;
	}

	return 0;
}

 

반응형