13장 Exercise & Programming
13장 Exercise
1.
struct customer {
char name[20];
int zip_code;
long mileage;
};
struct customer c1;
2. a)거짓
b)거짓
c)거짓
d)거짓
e)참
3. 2번
4. 1, 3번
5. 여러 개의 변수가 메모리 공간을 공유하는 것 – 공용체
서로 다른 자료형의 변수들을 묶은 것 – 구조체
여러 개의 기호 상수를 정의한 것 – 열거형
사용자 정의 자료형을 정의하는 키워드 –typedef
6. white = 0
red = 3
blue = 4
green = 5
black = 9
7. 4번
8.
a)
struct Car {
int speed;
int gear;
char model[10];
};
b)
struct Car list[3] = {
{ 100, 1, "sclass" },
{ 60, 2, "eclass" },
{ 0, 1, "cclass" }
};
9. a) enum primary_colore {RED, GREEN, BLUE};
b) enum months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
10.
a) book.pages X, 구조체 변수명 .pages가 맞다
b)
struct book {
char title[50];
int pages;
} abook = { "Data Structures in C", 577 };
c)
typedef enum { red, green, blue } color;
color c;
c = red;
d) p가 초기화가 안되어 있다
13장 Exercise
1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
enum genre { COMIC, DOCU, DRAMA };
typedef enum genre GENRE;
struct book {
char title[100];
char author[20];
GENRE type;
};
char* name[] = { "COMIC", "DOCU", "DRAMA" };
int main(void){
struct book b1 = { "바람과 함께 사라지다", "마가렛 미첼", DRAMA };
printf("{%s, %s, %s } \n", b1.title, b1.author, name[b1.type]);
return 0;
}
2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct book {
char title[100];
char author[20];
char genre[100];
};
int equal_author(struct book b1, struct book b2) {
if (strcmp(b1.author, b2.author) == 0) {
return 1;
}
else {
return 0;
}
}
int main() {
struct book b1 = { "노인과 바다", "헤밍웨이", "DRAMA" };
struct book b2 = { "누구를 위하여 종을 울리나", "헤밍웨이", "DRAMA" };
int result = equal_author(b1, b2);
printf("b1 = { %s, %s, %s }\n", b1.title, b1.author, b1.genre);
printf("b2 = { %s, %s, %s }\n", b2.title, b2.author, b2.genre);
printf("equal_author()의 반환값: %d\n", result);
return 0;
}
3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct email {
char title[100];
char receiver[50];
char sender[50];
char content[1000];
char date[100];
int priority;
};
print(struct email e){
printf("제목: %s\n", e.title);
printf("수신자: %s\n", e.receiver);
printf("발신자: %s\n", e.sender);
printf("내용: %s\n", e.content);
printf("날짜: %s\n", e.date);
printf("우선순위: %d\n", e.priority);
}
int main(void){
struct email e;
strcpy(e.title, "안부 메일");
strcpy(e.receiver, "chulsoo@hankuk.ac.kr");
strcpy(e.sender, "hsh@hankuk.ac.kr");
strcpy(e.content, "안녕하십니까? 새해 복 많이 받으세요.");
strcpy(e.date, "2023/9/1");
e.priority = 1;
print(e);
return 0;
}
4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct complex {
double real;
double imag;
};
struct complex complex_add(struct complex c1, struct complex c2) {
struct complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
int main() {
struct complex c1 = { 1.00, 2.00 };
struct complex c2 = { 2.00, 3.00 };
struct complex sum = complex_add(c1, c2);
printf("%.2lf+%.2lfi\n", c1.real, c1.imag);
printf("%.2lf+%.2lfi\n", c2.real, c2.imag);
printf("%.2lf+%.2lfi\n", sum.real, sum.imag);
return 0;
}
5
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
int equal(struct point p1, struct point p2) {
if (p1.x == p2.x && p1.y == p2.y) {
return 1;
}
else {
return 0;
}
}
int main() {
struct point p1 = { 1, 2 };
struct point p2 = { 3, 5 };
int result = equal(p1, p2);
printf("(%d, %d) %s= (%d, %d)\n", p1.x, p1.y, result ? "==" : "!=", p2.x, p2.y);
return 0;
}
6
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
int equal(struct point* p1, struct point* p2) {
if (p1->x == p2->x && p1->y == p2->y) {
return 1;
}
else {
return 0;
}
}
int main() {
struct point p1 = { 1, 2 };
struct point p2 = { 3, 5 };
int result = equal(&p1, &p2);
printf("(%d, %d) %s= (%d, %d)\n", p1.x, p1.y, result ? "==" : "!=", p2.x, p2.y);
return 0;
}
7
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
int quadrant(struct point* p);
int main(void){
struct point p = { -1, 2 };
printf("(%d, %d)의 사분면 = %d\n", p.x, p.y, quadrant(&p));
return 0;
}
int quadrant(struct point* p){
if (p->x > 0 && p->y > 0) return 1;
else if (p->x < 0 && p->y > 0) return 2;
else if (p->x < 0 && p->y < 0) return 3;
else return 4;
}
8-1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
struct circle {
struct point center; // 원의 중심
double radius; // 원의 반지름
};
double area(struct circle c) {
double result = 3.14159 * c.radius * c.radius;
return result;
}
int main() {
struct circle c = { {0, 0}, 10.0 };
double circleArea = area(c);
printf("원의 면적: %.2lf\n", circleArea);
return 0;
}
8-2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
struct circle {
struct point center; // 원의 중심
double radius; // 원의 반지름
};
double perimeter(struct circle c) {
double result = 2 * 3.14159 * c.radius;
return result;
}
int main() {
struct circle c = { {0, 0}, 10.0 };
double circlePerimeter = perimeter(c);
printf("원의 둘레: %.2lf\n", circlePerimeter);
return 0;
}
8-3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct point {
int x, y;
};
typedef struct circle {
struct point center; // 원의 중심
double radius; // 원의 반지름
} CIRCLE;
double area(CIRCLE c) {
double result = 3.14159 * c.radius * c.radius;
return result;
}
double perimeter(CIRCLE c) {
double result = 2 * 3.14159 * c.radius;
return result;
}
int main() {
CIRCLE c = { {0, 0}, 10.0 };
printf("원의 중심점: (%d, %d)\n", c.center.x, c.center.y);
printf("원의 반지름: %.2lf\n", c.radius);
printf("원의 면적: %.2lf\n", area(c));
printf("원의 둘레: %.2lf\n", perimeter(c));
return 0;
}
9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
struct food {
char name[100];
int calories;
};
int calc_total_calroies(struct food array[], int size);
int calc_total_calroies(struct food array[], int size){
int i, total = 0;
for (i = 0; i < size; i++) {
total += array[i].calories;
}
return total;
}
int main(void){
struct food food_array[3] =
{ { "hambuger", 900 },{ "bulgogi", 500 },{ "sushi", 700 } };
int total = calc_total_calroies(food_array, 3);
printf("총 칼로리=%d\n", total);
return 0;
}
10
#include <stdio.h>
typedef struct {
int id;
char name[20];
char phone[15];
int age;
} Employee;
int main() {
Employee employees[10];
employees[0].id = 1;
strcpy(employees[0].name, "홍길동1");
strcpy(employees[0].phone, "010-1111-1111");
employees[0].age = 20;
employees[1].id = 2;
strcpy(employees[1].name, "홍길동2");
strcpy(employees[1].phone, "010-2222-2222");
employees[1].age = 25;
employees[2].id = 3;
strcpy(employees[2].name, "홍길동3");
strcpy(employees[2].phone, "010-3333-3333");
employees[2].age = 18;
employees[3].id = 4;
strcpy(employees[3].name, "홍길동4");
strcpy(employees[3].phone, "010-4444-4444");
employees[3].age = 33;
employees[4].id = 5;
strcpy(employees[4].name, "홍길동5");
strcpy(employees[4].phone, "010-5555-5555");
employees[4].age = 33;
employees[5].id = 6;
strcpy(employees[5].name, "홍길동6");
strcpy(employees[5].phone, "010-6666-6666");
employees[5].age = 31;
employees[6].id = 7;
strcpy(employees[6].name, "홍길동7");
strcpy(employees[6].phone, "010-7777-7777");
employees[6].age = 19;
employees[7].id = 8;
strcpy(employees[7].name, "홍길동8");
strcpy(employees[7].phone, "010-8888-8888");
employees[7].age = 23;
employees[8].id = 9;
strcpy(employees[8].name, "홍길동9");
strcpy(employees[8].phone, "010-9999-9999");
employees[8].age = 29;
employees[9].id = 10;
strcpy(employees[9].name, "홍길동10");
strcpy(employees[9].phone, "010-1010-1010");
employees[9].age = 35;
for (int i = 0; i < 10; i++) {
if (employees[i].age >= 20 && employees[i].age <= 30) {
printf("이름: %s 나이=%d\n", employees[i].name, employees[i].age);
}
}
return 0;
}
11
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
struct contact {
char name[100];
char home_phone[100];
char cell_phone[100];
};
int main(void)
{
struct contact list[5];
int i;
char name[100];
for (i = 0; i < 5; i++) {
printf("이름을 입력하시오: ");
scanf("%s", list[i].name);
printf("집전화번호를 입력하시오: ");
scanf("%s", list[i].home_phone);
printf("휴대폰번호를 입력하시오: ");
scanf("%s", list[i].cell_phone);
}
printf("검색할 이름을 입력하시오: ");
scanf("%s", name);
for (i = 0; i < 5; i++) {
if (strcmp(name, list[i].name) == 0) {
printf("집전화번호: %s\n", list[i].home_phone);
printf("휴대폰번호: %s\n", list[i].cell_phone);
return 0;
}
}
printf("검색이 실패하였습니다\n");
return 0;
}
12
#include <stdio.h>
typedef struct {
int value;
char suit;
} Card;
int main() {
Card cards[52];
char suits[] = { 'c', 'd', 'h', 's' };
int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int cardIndex = 0;
for (int suitIndex = 0; suitIndex < 4; suitIndex++) {
for (int valueIndex = 0; valueIndex < 13; valueIndex++) {
cards[cardIndex].value = values[valueIndex];
cards[cardIndex].suit = suits[suitIndex];
cardIndex++;
}
}
for (int i = 0; i < 52; i++) {
printf("%c %d", cards[i].suit, cards[i].value);
}
return 0;
}
13
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
enum shape_type { TRIANGLE, RECTANGLE, CIRCLE };
struct shape {
int type;
union {
struct {
int base, height;
} tri;
struct {
int width, height;
} rect;
struct {
int radius;
} circ;
} p;
};
int main(void){
struct shape s;
enum shpae_type type;
printf("도형의 타입을 입력하시오(0, 1, 2): ");
scanf("%d", &type);
switch (type) {
case TRIANGLE:
printf("밑변과 반지름을 입력하시오(예를 들어서 100 200): ");
scanf("%d %d", &s.p.tri.base, &s.p.tri.height);
printf("면적은 %d\n", (int)(0.5 * s.p.tri.base * s.p.tri.height));
break;
case RECTANGLE:
printf("가로와 세로의 길이를 입력하시오(예를 들어서 100 200):");
scanf("%d %d", &s.p.rect.width, &s.p.rect.height);
printf("면적은 %d\n", (int)(s.p.rect.width * s.p.rect.height));
break;
case CIRCLE:
printf("반지름을 입력하시오(예를 들어서 100): ");
scanf("%d", &s.p.circ.radius);
printf("면적은 %d\n", (int)(3.14 * s.p.circ.radius * s.p.circ.radius));
break;
}
return 0;
}
14
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SONGS 100
typedef enum {
GAYO,
POP,
CLASSIC,
MOVIE
} Genre;
typedef struct {
char title[100];
char artist[100];
char location[100];
Genre genre;
} Song;
Song songs[MAX_SONGS];
int numSongs = 0;
void addSong() {
if (numSongs >= MAX_SONGS) {
printf("더 이상 곡을 추가할 수 없습니다.\n");
return;
}
printf("제목: ");
scanf(" %[^\n]s", songs[numSongs].title);
printf("가수: ");
scanf(" %[^\n]s", songs[numSongs].artist);
printf("위치: ");
scanf(" %[^\n]s", songs[numSongs].location);
printf("장르 (0: 가요, 1: 팝, 2: 클래식, 3: 영화음악): ");
scanf("%d", &(songs[numSongs].genre));
numSongs++;
}
void printSongs() {
if (numSongs == 0) {
printf("등록된 곡이 없습니다.\n");
return;
}
printf("등록된 곡 목록:\n");
for (int i = 0; i < numSongs; i++) {
printf("제목: %s\n", songs[i].title);
printf("가수: %s\n", songs[i].artist);
printf("위치: %s\n", songs[i].location);
printf("장르: ");
switch (songs[i].genre) {
case GAYO:
printf("가요\n");
break;
case POP:
printf("팝\n");
break;
case CLASSIC:
printf("클래식\n");
break;
case MOVIE:
printf("영화음악\n");
break;
default:
printf("알 수 없음\n");
break;
}
printf("\n");
}
}
void searchSong() {
if (numSongs == 0) {
printf("등록된 곡이 없습니다.\n");
return;
}
char searchTitle[100];
printf("검색할 곡의 제목을 입력하세요: ");
scanf(" %[^\n]s", searchTitle);
int search = 0;
for (int i = 0; i < numSongs; i++) {
if (strcmp(songs[i].title, searchTitle) == 0) {
printf("검색 결과:\n");
printf("제목: %s\n", songs[i].title);
printf("가수: %s\n", songs[i].artist);
printf("위치: %s\n", songs[i].location);
printf("장르: ");
switch (songs[i].genre) {
case GAYO:
printf("가요\n");
break;
case POP:
printf("팝\n");
break;
case CLASSIC:
printf("클래식\n");
break;
case MOVIE:
printf("영화음악\n");
break;
default:
printf("알 수 없음\n");
break;
}
printf("\n");
search = 1;
break;
}
}
if (!search) {
printf("검색 결과가 없습니다.\n");
}
}
int main() {
int choice;
while (1) {
printf("===============\n");
printf("1. 추가\n");
printf("2. 출력\n");
printf("3. 검색\n");
printf("4. 종료\n");
printf("===============\n");
printf("정수값을 입력하시오: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addSong();
break;
case 2:
printSongs();
break;
case 3:
searchSong();
break;
case 4:
printf("프로그램을 종료합니다.\n");
return 0;
default:
printf("잘못된 선택입니다. 다시 입력하세요.\n");
break;
}
}
return 0;
}
15
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int id;
char name[30];
char author[30];
};
struct book library[1000];
int main(void){
char name[30], author[30];
int select = 0;
int nbooks = 0;
while (select != 6){
int select;
printf("===========================================\n");
printf("1. 도서 번호로 책 찾기\n");
printf("2. 저자 이름으로 책 찾기\n");
printf("3. 제목으로 책 찾기\n");
printf("4. 새로운 책 추가\n");
printf("5. 도서관이 소장한 도서의 수 표시\n");
printf("6. 종료\n");
printf("===========================================\n");
printf("메뉴 중에서 하나를 선택하세요: \n");
scanf("%d", &select);
getchar();
switch (select)
{
case 4:
printf("책 이름 = ");
gets_s(library[nbooks].name, 30);
printf("저자 이름 = ");
gets_s(library[nbooks].author, 30);
library[nbooks].id = nbooks;
nbooks++;
break;
case 5:
for (int i = 0; i < nbooks; i++)
{
printf("책이름 = %s", library[i].name);
printf("저자 = %s", library[i].author);
}
printf("\n");
break;
case 2:
printf("저자 이름을 입력하시오 : ");
scanf("%s", author);
for (int i = 0; i < nbooks; i++)
{
if (strcmp(author, library[i].author) == 0)
printf("%s %s \n", library[i].name, library[i].author);
}
break;
case 6:
exit(0);
}
}
return 0;
}
16
#include <stdio.h>
typedef struct {
char name[50];
int studentID;
float GPA;
} Student;
int main() {
Student students[] = {
{"홍길동", 20230001, 4.20},
{"홍길동1", 20230002, 3.90},
{"홍길동2", 20230003, 3.70},
{"홍길동3", 20230004, 4.10},
{"홍길동4", 20230005, 4.00}
};
int numStudents = sizeof(students) / sizeof(students[0]);
float maxGPA = students[0].GPA;
int maxIndex = 0;
for (int i = 1; i < numStudents; i++) {
if (students[i].GPA > maxGPA) {
maxGPA = students[i].GPA;
maxIndex = i;
}
}
printf("평점이 가장 높은 학생은 (이름: %s, 학번: %d, 평점: %.2f)입니다.\n",
students[maxIndex].name, students[maxIndex].studentID, students[maxIndex].GPA);
return 0;
}
14장 Exercise & Programming은 아래 클릭하시면 됩니다.
[쉽게 풀어쓴 C언어 Express 개정4판] 14장 Exercise & Programming
[쉽게 풀어쓴 C언어 Express 개정4판] 14장 Exercise & Programming
14장 Exercise & Programming14장 Exercise 1. 다음은 무엇을 선언하는 문장인가? a) int형 포인터에 대한 포인터 선언b) 10개의 int형 포인터를 저장하는 배열의 선언c) 3개의 int를 가지는 배열에 대한 포인터...
gomszone.tistory.com
'프로그래밍 > C언어' 카테고리의 다른 글
[쉽게 풀어쓴 C언어 Express 개정4판] 14장 Exercise & Programming (1) | 2024.10.17 |
---|---|
[쉽게 풀어쓴 C언어 Express 개정4판] 17장 Exercise & Programming (11) | 2024.02.13 |
[쉽게 풀어쓴 C언어 Express 개정4판] 12장 Exercise & Programming (4) | 2024.02.12 |
[쉽게 풀어쓴 C언어 Express 개정4판] 11장 Exercise & Programming (5) | 2024.02.12 |
[쉽게 풀어쓴 C언어 Express 개정4판] 10장 Exercise & Programming (2) | 2024.02.12 |
댓글