쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 15-3

9.

#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;
}

10.

#include <stdio.h>
#include <math.h>

struct card {

int value;
char suit;
}
;

int main(void) {

struct card cards[52];
int i;

for (i = 0; i < 13; i++) {

cards[i].value = i % 13 + 1;
cards[i].suit = 'c';
}

for (i = 0; i < 13; i++) {

cards[i + 13].value = i % 13 + 1;
cards[i + 13].suit = 'd';
}

for (i = 0; i < 13; i++) {

cards[i + 26].value = i % 13 + 1;
cards[i + 26].suit = 'h';
}

for (i = 0; i < 13; i++) {

cards[i + 39].value = i % 13 + 1;
cards[i + 39].suit = 's';
}

for (i = 0; i < 52; i++) {

printf("%d:%c ", cards[i].value, cards[i].suit);
}

return 0;
}

15.

#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;
}

18.

#include <stdio.h>
#include <string.h>

#define TITLE_SIZE 50
#define NAME_SIZE 50
#define LOCATION_SIZE 50

enum music_type {
KPOP, POP, CLASSIC, SCREEN_MUSIC
}
;

typedef struct music {

char title[TITLE_SIZE];
char singer[NAME_SIZE];
char location[LOCATION_SIZE];

enum music_type genre;
}

MUSIC;

void add_record(MUSIC library[], int count);
void menu();

int get_input();

void search_record(MUSIC library[], int count);
void print_record(MUSIC library[], int count);

int main(void) {

int num, count = 0;

MUSIC library[30] = {
'\0'
}
;

while (1) {

menu();

num = get_input();

switch (num) {

case 1:

add_record(library, count);
count++;
continue;

case 2:

print_record(library, count);
continue;

case 3:

search_record(library, count);
continue;

case 4:

return -1;
}
return 0;
}
}

void add_record(MUSIC library[], int count) {

int type;

fflush(stdin);

printf("제목:");
gets(library[count].title);

printf("가수:");
gets(library[count].singer);

printf("위치:");
gets(library[count].location);

printf("장르(0: 가요, 1: 팝, 2: 클래식, 3: 영화음악)");
scanf("%d", &type);

if (type >= KPOP && type <= SCREEN_MUSIC)

library[count].genre = type; else
library[count].genre = KPOP;
}

void menu() {

printf("====================\n");
printf(" 1. 추가\n");
printf(" 2. 출력\n");
printf(" 3. 검색\n");
printf(" 4. 종료\n");
printf("====================\n");
}

int get_input() {

int num;

printf("정수값을 입력하시오 : ");
scanf("%d", &num);

return num;
}

void search_record(MUSIC library[], int count) {

int i;
char title[TITLE_SIZE];

fflush(stdin);

printf("제목: ");
gets(title);

for (i = 0; i < count; i++) {

if (strcmp(title, library[i].title) == 0) {

printf("저장된 위치는 %s\n", library[i].location);
return;
}
}
printf("찾는 음악이 테이블에 없습니다.\n");
}

void print_record(MUSIC library[], int count) {

int i;

fflush(stdin);

for (i = 0; i < count; i++) {

printf("제목 : %s\n", library[i].title);
printf("가수 : %s\n", library[i].singer);
printf("위치 : %s\n", library[i].location);

if (library[i].genre == 0)

printf("장르 : 가요\n"); else if (library[i].genre == 1)
printf("장르 : 팝\n"); else if (library[i].genre == 2)
printf("장르 : 클래식\n"); else if (library[i].genre == 3)
printf("장르 : 영화음악\n");
}
}

댓글

이 블로그의 인기 게시물

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 10-3

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 16-1

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 13-2