문제 01) 덧셈, 뺄셈, 나눗셈을 지원하는 계산기 프로그램을 작성하여 보자. 이번에는 각 연산들이 몇 번씩 계산되었는지를 기억하게 하자. 각 연산을 지원하는 함수들은 자신이 호출된 획수를 화면에 출력한다.
1. 정적 지역 변수를 사용하여 프로그램을 작성하라.
2. 전역 변수를 사용하여 프로그램을 작성하라.
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
|
#include<stdio.h>
int a, b; char sum;
void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void division(int a, int b);
int main() {
while (1) {
printf("연산을 입력하시오: ");
scanf("%d %c %d", &a, &sum, &b);
switch (sum) {
case '+':
add(a, b); break;
case'-':
subtract(a, b); break;
case'*':
multiply(a, b); break;
case '/':
division(a, b); break;
}
}
return 0;
}
void add(int a, int b) {
static int count = 1;
printf("덧셈은 총 %d번 실행되었습니다.\n", count);
printf("연산결과: %d\n", a + b); return;
}
void subtract(int a, int b) {
static int count = 1;
printf("뺄셈은 총 %d번 실행되었습니다.\n", count);
printf("연산결과: %d\n", a - b); return;
}
void multiply(int a, int b) {
static int count = 1;
printf("곱셈은 총 %d번 실행되었습니다.\n", count);
printf("연산결과: %d\n", a * b); return;
}
void division(int a, int b) {
static int count = 1;
printf("나눗셈은 총 %d번 실행되었습니다.\n", count);
printf("연산결과: %d\n", a / b); return;
}
|
cs |
문제 02) 주사위를 던져서 각각의 면이 몇 번 나왔는지를 출력하는 프로그램을 작성하라. 주사위의 면은 난수를 이용하여 생성한다. 주사위를 던지는 함수 get_dice_face()를 만들고 이 함수 안에서 각각의 면이 나올 때마다 그 횟수를 정적 지역 변수를 이용하여 기억하게 하라. get_dice_face() 호출 횟수가 100이 되면 각 면의 횟수를 출력한다.
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
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int get_dice_face();
int main() {
static int count = 0;
int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0;
srand((unsigned)time(NULL));
while(count<100){
int a = get_dice_face();
switch (a) {
case 1:
c1++; break;
case 2:
c2++; break;
case 3:
c3++; break;
case 4:
c4++; break;
case 5:
c5++; break;
case 6:
c6++; break;
}
count++;
}
printf("1-> %d\n", c1);
printf("2-> %d\n", c2);
printf("3-> %d\n", c3);
printf("4-> %d\n", c4);
printf("5-> %d\n", c5);
printf("6-> %d\n", c6);
return 0;
}
int get_dice_face() {
return rand() % 6 + 1;
}
|
cs |
문제 03) 로그인시에 아이지를 검사하는 함수 int check()를 작성해서 테스트하라. check()가 한번 호출 될 때마다 비밀번호를 질문하고 일치 여부를 0과 1으로 반환한다. 비밀번호는 숫자 1234로 고정되어 있다고 가정한다. check()가 3번 이상 호출되고 아이디가 일치하지 않으면 check()는 "로그인 시도 횟수 초과" 메시지를 출력한다. check()한수 안에서 정적 변수를 선언하여 사용해보자.
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
|
#include<stdio.h>
#define PASSWORD 1234
int check();
int main() {
for (int i = 0; i < 3; i++) {
if (check() == 1)
break;
}
return 0;
}
int check() {
int input;
static int count = 0;
printf("비밀번호: ");
scanf("%d", &input);
count++;
if (input == PASSWORD) {
printf("로그인 성공\n");
return 1;
}
if (count == 3)
printf("로그인 시도 횟수 초과");
return 0;
}
|
cs |
문제 04) 본문에서 설명한 바와 같이 정적 변수는 초기화를 딱 한번만 수행하는 경우에도 사용된다. 난수를 생성하여 반환하는 함수 get_random()을 작성하여 테스트하라. get_random()이 처음으로 호출되는 경우에는 srand()를 호출하여서 난수의 시드를 초기화하고 그렇지 않으면 단순히 rand()를 호출하여 난수를 반환한다.
(정적 지역 변수 inited를 작성하여 사용해본다. 본문의 LAB 문제를 참조한다.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include<stdio.h>
#include<stdlib.h>
void get_random();
int main() {
for (int i = 0; i < 3; i++) {
get_random();
}
return 0;
}
void get_random() {
static int inited = 0;
if (inited == 0) {
srand((unsigned)time(NULL));
printf("초기화 실행\n");
inited = 1;
}
printf("%d\n", rand());
return;
}
|
cs |
문제 05) 1부터 n까지의 합 (1+2+3+...+n)을 계산하는 문제를 순환기법을 이용하여 작성해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<stdio.h>
int sum(int n);
int main() {
int input;
printf("정수를 입력하시오: ");
scanf("%d", &input);
printf("1부터 %d까지의 합= %d", input, sum(input));
return 0;
}
int sum(int n) {
if (n == 1) return 1;
return n + sum(n - 1);
}
|
cs |
문제 06) 순환기법을 이용하여 지수값을 계산하는 함수 power(int base, int power_raised)를 작성하고 테스트해보자. power(2, 3)가 호출되면 2^3을 계산하여 반환한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include<stdio.h>
int power(int base, int power_raised);
int main() {
int a, b;
printf("밑수: ");
scanf("%d", &a);
printf("지수: ");
scanf("%d", &b);
printf("%d^%d = %d", a, b, power(a, b));
return 0;
}
int power(int base, int power_raised) {
if (power_raised == 0)
return 1;
return base * power(base, power_raised - 1);
}
|
cs |
문제 07) 순환 호출을 이용하여 정수의 각 자리수를 출력하는 함수 show_digit(int x)를 작성하고 테스트하라. 즉 정수가 1234이면 화면에 1 2 3 4와 같이 출력한다. 함수는 일의 자리를 출력하고 나머지 부분을 대상으로 다시 같은 함수를 순환 호출한다. 예를 들어서 1234의 4를 출력하고 나머지 123을 가지고 다시 같은 함수를 순환 호출한다. 1234를 10으로 나누면 123이 되고 4는 1234를 10으로 나눈 나머지이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<stdio.h>
int show_digit(int x);
int main() {
int n;
printf("정수를 입력하시오: ");
scanf("%d", &n);
show_digit(n);
return 0;
}
int show_digit(int x) {
static int a = 10000;
a /= 10;
if (x == 0)
return 0;
printf("%d ", x / a);
return show_digit(x % a);
}
|
c |
문제 08) 주어진 정수가 몇 개의 자리수를 가지고 있는지를 계산하는 프로그램의 순환을 이용하여 작성해보자. 예를 들어서 12345의 경우에는 5가 출력된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include<stdio.h>
int count(int x);
int main() {
int n;
printf("정수를 입력하시오: ");
scanf("%d", &n);
printf("자리수의 개수: %d", count(n));
return 0;
}
int count(int x) {
static int num = 0;
if (x == 0)
return num;
x /= 10; num++;
return count(x);
}
|
cs |
문제 09) 앞의 문제와 유사한 문제이다. 자리수의 합계를 계산하는 프로그램의 순환을 이용하여 작성해보자. 예를 들어서 123의 경우에는 6이 출력된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<stdio.h>
int get_sum(int x);
int main() {
int n;
printf("정수를 입력하시오: ");
scanf("%d", &n);
printf("자리수의 합: %d", get_sum(n));
return 0;
}
int get_sum(int x) {
if (x / 10 == 0) return x;
return x % 10 + get_sum(x / 10);
}
|
cs |
문제 10) 다음과 같은 수식의 값을 계산하는 순환적인 프로그램을 작성하라.
1/1+1/2+1/3+...+1/n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include<stdio.h>
double recursive(int x);
int main() {
int n;
scanf("%d", &n);
printf("%lf", recursive(n));
return 0;
}
double recursive(int x) {
if (x == 0) return 0;
return 1.0 / x + recursive(x - 1);
}
|
cs |
문제 11) 이항 계수(binomial coefficient)를 계산하는 순환 함수를 작성하라. 이항 계수는 다음과 같이 순환적으로 정의 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include<stdio.h>
int recursive(int n, int k);
int main() {
int n, k;
printf("n=");
scanf("%d", &n);
printf("k=");
scanf("%d", &k);
printf("%d", recursive(n, k));
return 0;
}
int recursive(int n,int k) {
if (n == k || k == 0) return 1;
return recursive(n - 1, k - 1) + recursive(n - 1, k);
}
|
cs |
문제 12) 순환 호출을 이용하여 피보나치 수열을 계산해 보자. 피보나치 수열이란 다음과 같이 정의되는 수열이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include<stdio.h>
int fib(int n);
int main() {
for (int i = 0; i < 10; i++) {
printf("fib(%d) = %d\n", i, fib(i));
}
return 0;
}
int fib(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
return fib(n - 2) + fib(n - 1);
}
|
cs |
:)
'C > 쉽게 풀어쓴 C언어 Express' 카테고리의 다른 글
[쉽게 풀어쓴 C언어 Express] 11장 programming (0) | 2022.08.17 |
---|---|
[쉽게 풀어쓴 C언어 Express] 10장 programming (0) | 2022.08.14 |
[쉽게 풀어쓴 C언어 Express] 8장 programming (0) | 2022.08.08 |
[쉽게 풀어쓴 C언어 Express] 7장 programming (0) | 2022.08.06 |
[쉽게 풀어쓴 C언어 Express] 6장 programming (0) | 2022.08.05 |
댓글