본문 바로가기
솔루션모음/쉽게 풀어 쓴 C언어 Express 실습문제

[쉽게 풀어 쓴 C언어 Express] 13장 프로그래밍 programming 솔루션 답지

by 이얏호이야호 2020. 4. 11.

쉽게 풀어 쓴 C언어 express 솔루션입니다.

1.

(a)

#include <stdio.h>

struct point {

 int x, y;

}

;

int equal(struct point p1, struct point p2);

int main(void) {

 struct point p1= {

 1, 2

 }

 ;

 struct point p2= {

 3, 5

 }

 ;

 printf("일치 여부 = %d\n", equal(p1, p2));

 return 0;

}

int equal(struct point p1, struct point p2) {

 if( p1.x == p2.x && p1.y == p2.y ) return 1; else return 0;

}

 

 

(b)

#include <stdio.h>

struct point {

 int x, y;

}

;

int quadrant(struct point p);

int main(void) {

 struct point p= {

 -1, 2

 }

 ;

 printf("사분면 = %d\n", 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;

}

 

2.

(a)

#include <stdio.h>

struct point {

 int x, y;

}

;

int equal(struct point *p1, struct point *p2);

int main(void) {

 struct point p1= {

 1, 2

 }

 ;

 struct point p2= {

 3, 5

 }

 ;

 printf("일치 여부 = %d\n", equal(&p1, &p2));

 return 0;

}

int equal(struct point *p1, struct point *p2) {

 if( p1->x == p2->x && p1->y == p2->y ) return 1; else return 0;

}

 

 

(b)

#include <stdio.h>

struct point {

 int x, y;

}

;

int quadrant(struct point *p);

int main(void) {

 struct point p= {

 -1, 2

 }

 ;

 printf("사분면 = %d\n", 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;

}

 

 

3.

#include <stdio.h>

#include <math.h>

struct point {

 int x, y;

}

;

struct rectangle {

 struct point a, b;

 // a는 오른쪽 상단, b는 왼쪽 하단을 나타낸다.

}

;

int area(struct rectangle r);

int perimeter(struct rectangle r);

int is_square(struct rectangle r);

int main(void) {

 struct point p1= {

 1, 1

 }

 ;

 struct point p2= {

 2, 2

 }

 ;

 struct rectangle r;

 r.a = p1;

 r.b = p2;

 printf("%d\n", area(r));

 printf("%d\n", perimeter(r));

 printf("%d\n", is_square(r));

 return 0;

}

int area(struct rectangle r) {

 return abs(r.a.x-r.b.x)*abs(r.a.y-r.b.y);

}

int perimeter(struct rectangle r) {

 return 2*(abs(r.a.x-r.b.x)+abs(r.a.y-r.b.y));

}

int is_square(struct rectangle r) {

 return abs(r.a.x-r.b.x)==abs(r.a.y-r.b.y);

}

 

 

4.

#include <stdio.h>

struct complex {

 double real;

 double imag;

}

;

struct 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;

}

struct complex sub(struct complex c1, struct complex c2) {

 struct complex result;

 result.real = c1.real+c2.real;

 result.imag = c1.imag+c2.imag;

 return result;

}

struct complex mul(struct complex c1, struct complex c2) {

 struct complex result;

 result.real = c1.real*c2.real-c1.imag*c2.imag;

 result.imag = c1.real*c2.imag+c1.imag*c2.real;

 return result;

}

void print(struct complex c) {

 printf("%f+%fi\n", c.real,c.imag);

}

int main(void) {

 struct complex c1= {

 1.0, 2.0

 }

 ;

 struct complex c2= {

 2.0, 3.0

 }

 ;

 struct complex c3;

 c3= add(c1, c2);

 print(c3);

 return 1;

}

 

 

 

5.

#include <stdio.h>

#include <math.h>

struct vector {

 double x;

 double y;

}

;

struct vector vector_add(struct vector v1, struct vector v2) {

 struct vector r;

 r.x = v1.x + v2.x;

 r.y = v1.y + v2.y;

 return r;

}

void vector_print(struct vector v) {

 printf("(%f, %f)\n", v.x, v.y);

}

int main(void) {

 struct vector v1= {

 1.0, 2.0

 }

 ;

 struct vector v2= {

 2.0, 3.0

 }

 ;

 struct vector v3;

 v3 = vector_add(v1, v2);

 vector_print(v3);

 return 0;

}

 

 

6.

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

}

;

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, "2010/9/1");

 e.priority = 1;

 return 0;

}

 

 

7.

#include <stdio.h>

#include <math.h>

struct food {

 char name[100];

 int calories;

}

;

int calc_total_calroies(struct food array[], int size);

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;

}

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;

}

 

 

8.

#include <stdio.h>

#include <string.h>

// 직원

struct employee {

 int number;

 // 사번

 char name[20];

 // 이름

 int age;

 // 나이

 char tel[20];

 // 전화번호

}

;

int main(void) {

 struct employee e[10] = { {

 1, "홍길동1", 20, "111-1111"

 }

 , {

 2, "홍길동2", 25, "111-1112"

 }

 , {

 3, "홍길동3", 60, "111-1113"

 }

 , {

 4, "홍길동4", 40, "111-1114"

 }

 , {

 5, "홍길동5", 50, "111-1115"

 }

 , {

 6, "홍길동6", 45, "111-1116"

 }

 , {

 7, "홍길동7", 32, "111-1117"

 }

 , {

 8, "홍길동8", 23, "111-1118"

 }

 , {

 9, "홍길동9", 29, "111-1119"

 }

 , {

 10, "홍길동10", 62, "111-1120"

 }

 }

 ;

 int i;

 for (i=0;i<10;i++)

 if( e[i].age >= 20 && e[i].age <= 30 )

 printf("%s\n", e[i].name);

 return 0;

}

 

 

 

 

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 이론 솔루션

 https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%9D%B4%EB%A1%A0

더 많은 쉽게 풀어 쓴 C언어EXPRESS 프로그래밍 솔루션 : 

https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%8B%A4%EC%8A%B5%EB%AC%B8%EC%A0%9C

댓글