문제 01) 책을 나타내는 구조체 book을 선언해서 사용해보자. 책은 제목(title), 저자(author), 분야(subject)를 각지고 있다고 가정한다. {1, "바람과 함께 사라지다", "마가렛 미첼"}의 값을 가지는 구조체 변수를 생성했다가 다시 화면에 출력해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <stdio.h>
struct book {
int id;
char title[100];
char author[20];
}b;
int main() {
scanf("%d", &b.id);
gets_s(b.title, 100);
gets_s(b.author, 20);
printf("{ %d, %s, %s}", b.id, b.title, b.author);
return 0;
}
|
cs |
문제 02) 은행계좌를 나타내는 구조체 account를 선언해서 사용해보자. 계좌는 계좌번호(number), 이름(name), 잔액(balance) 등의 정보를 가지고 있다고 하자. {1, "홍길동", 100000}의 값을 가지는 구조체 변수를 생성했다가 다시 화면에 출력해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <stdio.h>
struct account {
int number;
char name[20];
int balance;
};
int main() {
struct account a;
scanf("%d %s %d", &a.number, a.name, &a.balance);
printf("{ %d, %s, %d }", a.number, a.name, a.balance);
return 0;
}
|
cs |
문제 03) 구조체를 이용하여 이메일을 표현할 수 있는 구조체를 정의하고 적당한 초기값을 부여하고 출력하는 프로그램을 작성하라. 구조체의 멤버는 제목(title), 수신자(sender), 발신자(receiver), 내용(content), 날짜(date), 우선순위(pri) 등으로 구성된다.
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
|
#include <stdio.h>
struct email {
char title[20];
char sender[30];
char receiver[30];
char content[100];
char date[10];
int pri;
}e;
int main() {
gets_s(e.title, 20);
gets_s(e.sender, 30);
gets_s(e.receiver, 30);
gets_s(e.content, 100);
scanf("%s", e.date);
scanf("%d", &e.pri);
printf("제목: %s\n", e.title);
printf("수신자: %s\n", e.sender);
printf("발신자: %s\n", e.receiver);
printf("내용: %s\n", e.content);
printf("날짜: %s\n", e.date);
printf("우선순위: %d", e.pri);
return 0;
}
|
cs |
문제 04) 구조체를 이용하여 복소수를 다음과 같이 정의하고 복소수의 덧셈을 수행하는 함수를 작성하고 테스트하라.
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 <stdio.h>
struct complex {
double real;
double imag;
}c1, c2, sum;
struct complex complex_add(struct complex c1, struct complex c2);
int main() {
scanf("%lf %lf", &c1.real, &c1.imag);
scanf("%lf %lf", &c2.real, &c2.imag);
complex_add(c1, c2);
printf("%lf+%lfi", sum.real, sum.imag);
return 0;
}
struct complex complex_add(struct complex c1, struct complex c2) {
sum.real = c1.real + c2.real;
sum.imag = c1.imag + c2.imag;
return sum;
}
|
cs |
문제 05) 2차원 평면에서 점은 (x, y) 좌표로 나타낼 수 있다. 따라서 하나의 점은 다음과 같은 point라는 구조체로 정의할 수 있다. 이 point 구조체를 받아서 두 점의 좌표가 일치하면 1을 반환하고 그렇지 않으면 0을 반환하는 함수 int equal(struct point p 1, struct point p2)를 작성하고 테스트하라.
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
|
#include <stdio.h>
struct point {
int x, y;
};
int equal(struct point p1, struct point p2);
int main() {
struct point c1, c2;
scanf("%d %d", &c1.x, &c1.y);
scanf("%d %d", &c2.x, &c2.y);
if (equal(c1, c2) == 1) // 일치
printf("(%d ,%d) = (%d ,%d)", c1.x, c1.y, c2.x, c2.y);
else
printf("(%d ,%d) != (%d ,%d)", c1.x, c1.y, c2.x, c2.y);
return 0;
}
int equal(struct point p1, struct point p2) {
if (p1.x == p2.x && p1.y == p2.y) {
return 1;
}
else return 0;
}
|
cs |
문제 06) 앞의 문제에서 equal() 함수를 다음과 같이 구조체의 포인터를 받도록 변경하여서 작성하고 테스트하라.
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
|
#include <stdio.h>
struct point {
int x, y;
};
int equal(struct point *p1, struct point *p2);
int main() {
struct point c1, c2;
scanf("%d %d", &c1.x, &c1.y);
scanf("%d %d", &c2.x, &c2.y);
if (equal(&c1, &c2) == 1) // 일치
printf("(%d ,%d) = (%d ,%d)", c1.x, c1.y, c2.x, c2.y);
else
printf("(%d ,%d) != (%d ,%d)", c1.x, c1.y, c2.x, c2.y);
return 0;
}
int equal(struct point *c1, struct point *c2) {
if (c1 -> x == c2 -> x && c1 -> y == c2 -> y) {
return 1;
}
else return 0;
}
|
cs |
문제 07) 2차원 공간에 있는 점의 좌표를 받아서 이 점이 속하는 사분면의 번호를 반환하는 함수 int quadrant(struct point p)를 작성하고 테스트하라. 앞의 point 구조체를 사용한다.
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 <stdio.h>
struct point {
int x;
int y;
}p;
int quadrant(struct point p);
int main() {
scanf("%d %d", &p.x, &p.y);
printf("(%d, %d)의 사분면 = %d", p.x, p.y, quadrant(p));
return 0;
}
int quadrant(struct point p) {
if (p.x > 0) {
if (p.y > 0) return 1;
return 4;
}
else {
if (p.y > 0) return 2;
return 3;
}
}
|
cs |
문제 08) 원의 중심을 나타내는데 point 구조체를 사용할 수 있다. 원을 나타내는 circle 구조체를 정의하라. 이 circle 구조체를 받아서 다음과 같은 기능을 하는 함수를 작성하고 테스트하라.
(a) 원의 변적을 계산하는 함수 double area(struct circle c)
(b) 원의 둘레를 계산하는 함수 double perimeter(struct circle c)
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
|
#include <stdio.h>
#define PI 3.14
struct point {
int x, y;
}p;
struct circle {
struct point center;
double radius;
}c;
double area(struct circle c);
double perimeter(struct circle c);
int main() {
printf("원의 중심의 좌표를 입력하시오: ");
scanf("%d %d", &c.center.x, &c.center.y);
printf("원의 반지름을 입력하시오: ");
scanf("%lf", &c.radius);
printf("원의 면적: %lf", area(c));
printf(", 원의 둘레: %lf", perimeter(c));
return 0;
}
double area(struct circle c) { //원의 면적
double sum = 0;
sum = c.radius * c.radius * PI;
return sum;
}
double perimeter(struct circle c) { //원의 둘레
double sum = 0;
sum = c.radius * PI * 2;
return sum;
}
|
cs |
(c) typedef을 사용하여 struct circle을 CIRCLE로 정의한 후에 (a)와 (b)를 다시 작성해보자.
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
|
#include <stdio.h>
#define PI 3.14
struct point {
int x, y;
}p;
typedef struct circle {
struct point center;
double radius;
}CIRCLE;
CIRCLE c;
double area(c);
double perimeter(c);
int main() {
printf("원의 중심의 좌표를 입력하시오: ");
scanf("%d %d", &c.center.x, &c.center.y);
printf("원의 반지름을 입력하시오: ");
scanf("%lf", &c.radius);
printf("원의 면적: %lf", area(c));
printf(", 원의 둘레: %lf", perimeter(c));
return 0;
}
double area(CIRCLE c) { //원의 면적
double sum = 0;
sum = c.radius * c.radius * PI;
return sum;
}
double perimeter(CIRCLE c) { //원의 둘레
double sum = 0;
sum = c.radius * PI * 2;
return sum;
}
|
cs |
문제 09) 각각의 음식에 대하여 음식의 이름, 칼로리 정보를 구조체로 표현한다. 사용자가 하루 동안 먹은 음식들을 입력받아 구조체의 배열에 저장하고 하루 동안 먹은 음식의 칼로리를 계산하는 프로그램을 작성하라.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <stdio.h>
struct food {
char name[100];
int calories;
};
struct food food_array[3];
int main() {
int sum = 0;
for (int i = 0; i < 3; i++) {
printf("음식의 이름: ");
scanf("%s", food_array[i].name);
printf("칼로리: ");
scanf("%d", &food_array[i].calories);
sum += food_array[i].calories;
}
printf("총 칼로리= %d", sum);
return 0;
}
|
cs |
:)
'C > 쉽게 풀어쓴 C언어 Express' 카테고리의 다른 글
[쉽게 풀어쓴 C언어 Express] 12장 programming (0) | 2022.08.21 |
---|---|
[쉽게 풀어쓴 C언어 Express] 11장 programming (0) | 2022.08.17 |
[쉽게 풀어쓴 C언어 Express] 10장 programming (0) | 2022.08.14 |
[쉽게 풀어쓴 C언어 Express] 9장 programming (0) | 2022.08.11 |
[쉽게 풀어쓴 C언어 Express] 8장 programming (0) | 2022.08.08 |
댓글