티스토리 뷰
1. cout과 << 연산자를 이용하여, 1에서 100까지 정수를 다음과 같이 한 줄에 10개씩 출력하라. 각 정수는 탭으로 분리하여 출력하라.
1
2
3
4
5
6
7
8
9
10
11
12 |
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 100; i++) {
cout << i << "\t";
if (i % 10 == 0)
cout << endl;
}
return 0;
} |
cs |
2. cout과 << 연산자를 이용하여 다음과 같이 구구단을 출력하는 프로그램을 작성하라.
1
2
3
4
5
6
7
8
9
10
11
12
13 |
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
cout << j << "x" << i << "=" << i*j <<"\t";
}
cout << endl;
}
return 0;
} |
cs |
3. 키보드로부터 두 개의 정수를 읽어 큰 수를 화면에 출력하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
#include<iostream>
using namespace std;
int main() {
int a, b;
cout << "두 수를 입력하라>>";
cin >> a >> b;
cout << "큰 수 = ";
if (a > b)
cout << a << endl;
else
cout << b << endl;
return 0;
} |
cs |
4. 소수점을 가지는 5개의 실수를 입력 받아 제일 큰 수를 화면에 출력하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
#include<iostream>
using namespace std;
int main() {
double a[5];
double max = 0;
cout << "5 개의 실수를 입력하라>>";
for (int i = 0; i < 5; i++) {
cin >> a[i];
if (max < a[i])
max = a[i];
}
cout << "제일 큰 수 = " << max << endl;
return 0;
} |
cs |
5. <enter>키가 입력될 때까지 문자들을 읽고, 입력된 문자 'x'의 개수를 화면에 출력하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
#include<iostream>
using namespace std;
int main() {
int i = 0;
int cnt = 0;
char strings[100];
cout << "문자들을 입력하라(100개 미만)." << endl;
cin.getline(strings, 100);
for (int i = 0; i < sizeof(strings); i++) {
if (strings[i] == 'x')
cnt++;
}
cout << "x의 개수는 " << cnt << endl;
return 0;
} |
cs |
6. 문자열을 두 개 입력받고 두 개의 문자열이 같은지 검사하는 프로그램을 작성하라.만일 같으면 "같습니다." 아니면 "같지 않습니다."를 출력하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main() {
string password1, password2;
cout << "새 암호를 입력하세요>>";
getline(cin, password1);
cout << "새 암호를 다시 한 번 입력하세요>>";
getline(cin, password2);
if (password1 == password2)
cout << "같습니다." << endl;
else
cout << "다릅니다." << endl;
return 0;
} |
cs |
7. 다음과 같이 "yes"가 입력될 때 까지 종료하지 않는 프로그램을 작성하라. 사용자로부터의 입력은 cin.getline() 함수를 사용하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main() {
char word[10];
while (1) {
cout << "종료하고싶으면 yes를 입력하세요>>";
cin.getline(word, 10);
if (strcmp(word,"yes") == 0)
break;
}
return 0;
} |
cs |
8.한 라인에 ';' 으로 5개의 이름을 구분하여 입력받아, 각 이름을 끊어내어 화면에 출력하고 가장 긴 이름을 판별하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
#include<iostream>
#include<string>
using namespace std;
int main() {
char word[100] = {};
char maximum[100] = {};
cout << "5명의 이름을 ';'으로 구분하여 입력하세요" << endl << ">>";
for (int i = 0; i < 5; i++) {
cin.getline(word, 100, ';');
cout << i << " : " << word << endl;
if (strlen(maximum) < strlen(word)) {
strcpy(maximum, word);
}
}
cout << "r가장 긴 이름은 " << maximum << endl;
return 0;
} |
cs |
9. 이름, 주소, 나이를 입력받아 다시 출력하는 프로그램을 작성하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
#include<iostream>
#include<string>
using namespace std;
int main() {
char name[20];
char address[100];
int age;
cout << "이름은 ? ";
cin.getline(name, 20);
cout << "주소는 ? ";
cin.getline(address, 100);
cout << "나이는 ? ";
cin >> age;
cout << name << ", " << address << ", " << age <<"세"<< endl;
return 0;
} |
cs |
10. 문자열을 하나 입력받고 문자열의 부분 문자열을 다음과 같이 출력하는 프로그램을 작성하라. 예시는 다음과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
#include<iostream>
#include<string>
using namespace std;
int main() {
char word[20];
cout << "문자열 입력 >>";
cin.getline(word, 20);
for (int i = 0; i < strlen(word); i++) {
for (int j = 0; j <= i; j++) {
cout << word[j];
}
cout << endl;
}
return 0;
} |
cs |
11. 다음 C 프로그램을 C++ 프로그램으로 수정하여 실행하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
#include<iostream>
#include<string>
using namespace std;
int main() {
int k, n = 0;
int sum = 0;
cout << "끝 수를 입력하세요 >>";
cin >> n;
for (k = 1; k <= n; k++)
sum += k;
cout << "1에서 %d까지의 합은 " << sum << "입니다. \n" << endl;
return 0;
} |
cs |
12. 다음 C 프로그램을 C++ 프로그램으로 수정하여 실행하라. 이 프로그램의 실행 결과는 연습문제 11과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
#include<iostream>
#include<string>
using namespace std;
int sum(int a, int b);
int main() {
int n = 0;
cout << "끝 수를 입력하세요>>";
cin >> n;
cout << "1에서 " << n << "까지의 합은 " << sum(1, n) << " 입니다.\n" << endl;
return 0;
}
int sum(int a, int b) {
int k, res = 0;
for (k = a; k <= b; k++)
res += k;
return res;
} |
cs |
13. 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/), 나머지(%)의 정수 5칙 연산을 할 수 있는 프로그램을 작성하라. 식은 다음과 같은 형식으로 입력된다. 정수와 연산자는 하나의 빈칸으로 분리된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
#include<iostream>
#include<string>
using namespace std;
int main() {
int a, b;
char oper;
while(1){
cout << "? ";
cin >> a >> oper >> b;
cout << a << " " << oper << " " << b << " = ";
switch (oper) {
case '+':cout << a + b; break;
case '-':cout << a - b; break;
case '*':cout << a * b; break;
case '/':cout << a / b; break;
case '%':cout << a % b; break;
default:cout << "잘못된 연산자"; break;
}
cout << "\n";
}
return 0;
} |
cs |
14. 영문 텍스트를 입력받아 알파벳 히스토그램을 그리는 프로그램을 작성하라. 대문자는 모두 소문자로 집계하며, 텍스트 입력의 끝은 ';' 문자로 한다.
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 |
#include<iostream>
#include<string>
using namespace std;
int main() {
int amount = 0;
int alphabetCnt[26] = { 0 };
int i = 0;
char buf[10000];
cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다." << endl;
cout << "텍스트의 끝은 ; 입니다. 10000개까지 가능합니다." << endl;
cin.getline(buf, 10000, ';');
while (i < strlen(buf)) {
if (isalpha(buf[i])) {
buf[i] = tolower(buf[i]);
alphabetCnt[buf[i] - 97]++;;
amount++;
}
i++;
}
cout << "총 알파벳 수 " << amount << endl << endl;
for (i = 0; i < 26; i++) {
cout << (char)97 + 0 << "(" << alphabetCnt[i] << ")" << " : ";
for (int j = 0; j < alphabetCnt[i]; j++)
cout << '*';
cout << endl;
}
return 0;
} |
cs |
참고 문헌 : 명품 C++ Programming / 황기태
※
본 게시물은 개인적인 용도로 작성된 게시물입니다. 이후 포트폴리오로 사용될 정리 자료이니 불펌과 무단도용은 하지 말아주시고 개인 공부 목적으로만 이용해주시기 바랍니다.
※
'객체 지향 프로그래밍 > 예제' 카테고리의 다른 글
[C++]명품 C++ 프로그래밍 4장 실습문제 (0) | 2017.11.27 |
---|
- Total
- Today
- Yesterday
- 파이썬 for
- 파이썬 단계적 개선
- 파이썬 if문
- 자바스크립트 자료구조
- 파이썬
- 파이썬 선택문
- css 그리드
- 자바 에센셜 실습문제
- 백준 1874
- 파이썬 문자열
- 파이썬 진수 변환
- 자바
- 백준 11501
- 백준
- 버츄어박스
- 백준 10451
- 자료구조
- 파이썬 함수
- 웹
- 명품 c++ 실습
- 파이썬 클래스
- 파이썬 객체
- 파이썬 연산자
- 파이썬 터틀
- 자바스크립트 그래프
- 파이썬 예제
- 파이썬 리스트
- 파이썬 while
- css 박스
- css
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |