" async="async"> ', { cookie_domain: 'auto', cookie_flags: 'max-age=0;domain=.tistory.com', cookie_expires: 7 * 24 * 60 * 60 // 7 days, in seconds }); 책 알려주는 남자 :: 쉽게 풀어쓴 C언어 Express 13장 Programming

13장 Programming


1.

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

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 if(p.x>0 && p.y<0)

return 4;

}



int main(){

struct point p1, p2;

printf("점의 좌표를 입력하시오: ");

scanf("%d,%d", &p1.x, &p1.y);

printf("점의 좌표를 입력하시오: ");

scanf("%d,%d", &p2.x, &p2.y);

if(equal(p1, p2)==1) 

printf("두 점은 일치합니다\n");

else 

printf("두 점은 일치하지 않습니다\n");

printf("첫번째 점은 %d사분면에 있습니다\n", quadrant(p1)); 

printf("두번째 점은 %d사분면에 있습니다\n", quadrant(p2));


return 0;

}


2.

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

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 if(p->x>0 && p->y<0)

return 4;

}



int main(){

struct point p1, p2;

printf("점의 좌표를 입력하시오: ");

scanf("%d,%d", &p1.x, &p1.y);

printf("점의 좌표를 입력하시오: ");

scanf("%d,%d", &p2.x, &p2.y);

if(equal(&p1, &p2)==1) 

printf("두 점은 일치합니다\n");

else 

printf("두 점은 일치하지 않습니다\n");

printf("첫번째 점은 %d사분면에 있습니다\n", quadrant(&p1)); 

printf("두번째 점은 %d사분면에 있습니다\n", quadrant(&p2));


return 0;

}


3.

#include <stdio.h>


typedef struct {

int x, y;

} Point;


typedef struct {

Point a, b

} Rectangle;


int area(Rectangle r);

int perimeter(Rectangle r);

int is_square(Rectangle r);


void main(){

Rectangle r;

printf("오른쪽 상단 점의 좌표: ");

scanf("%d,%d", &r.a.x, &r.a.y);

printf("왼쪽 하단 점의 좌표: ");

scanf("%d,%d", &r.b.x, &r.b.y);

printf("사각형의 넓이: %d\n", area(r));

printf("사각형의 둘레: %d\n", perimeter(r));

if(is_square(r)==1)

printf("정사각형입니다\n");

else

printf("정사각형이 아닙니다\n"); 

}


int area(Rectangle r){

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

}


int perimeter(Rectangle r){

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

}


int is_square(Rectangle r){

if((r.a.x-r.b.x)==(r.a.y-r.b.y))

return 1;

return 0;

}


4.

#include <stdio.h>


typedef struct {

double real;

double imag;

} Complex;


void add_complex(Complex x, Complex y);


void main(){

Complex x, y;

printf("첫 번째 복소수의 실수부: "); scanf("%lf", &x.real);

printf("첫 번째 복소수의 허수부: "); scanf("%lf", &x.imag);

printf("두 번째 복소수의 실수부: "); scanf("%lf", &y.real);

printf("두 번째 복소수의 허수부: "); scanf("%lf", &y.imag);

add_complex(x, y); 

}


void add_complex(Complex x, Complex y){

printf("합의 실수부: %lf\n", x.real+y.real);

printf("합의 허수부: %lf\n", x.imag+y.imag);

}


5.

#include <stdio.h>


typedef struct{

double x, y;

} Vector;


void add_vector(Vector v1, Vector v2);


void main(){

Vector v1, v2;

printf("벡터 a의 좌표: ");

scanf("%lf,%lf", &v1.x, &v1.y);

printf("벡터 b의 좌표: ");

scanf("%lf,%lf", &v2.x, &v2.y);

add_vector(v1, v2);

}


void add_vector(Vector v1, Vector v2){

printf("벡터 a+b = (%0.2lf,%0.2lf)", v1.x+v2.x, v1.y+v2.y);

}


6.

#include <stdio.h>


typedef struct {

char title[30];

char toname[20];

char byname[20];

char text[100];

char date[20];

int primary;

} Email;


void main(){

Email x;

printf("제목: ");

gets(x.title);

printf("수신자: ");

gets(x.toname);

printf("발신자: ");

gets(x.byname);

printf("본문: ");

gets(x.text);

printf("보낸날짜(yymmdd): ");

gets(x.date);

printf("우선순위: ");

scanf("%d", &x.primary); 

}


7.

#include <stdio.h>


typedef struct{

char name[100];

int calories;

} Food;


void add_calories(Food info[], int count);


void main(){

Food info[10];

int i=0, count=0;

char an;

while(1){

printf("음식정보를 저장하시겠습니까?(y/n)");

scanf("%c", &an);

fflush(stdin);

if(an=='n')

break; 

printf("음식이름: ");

gets(info[i].name);

printf("칼로리: ");

scanf("%d", &info[i].calories);

fflush(stdin);

i++;

count++;

}

add_calories(info, count);

}


void add_calories(Food info[], int count){

int i, sum=0;

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

sum+=info[i].calories;

printf("총 칼로리는 %d입니다", sum);

}


8.

#include <stdio.h>


typedef struct{

int id_number;

char name[20];

int ph_number;

int age;

} Employee;


void main(){

Employee people[10];

int i, count=0;

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

printf("%d번째 직원정보\n", i+1);

printf("사원번호: "); scanf("%d", &people[i].id_number);

fflush(stdin);

printf("이름: "); gets(people[i].name);

printf("전화번호: "); scanf("%d", &people[i].ph_number);

printf("나이: "); scanf("%d", &people[i].age);

}

printf("\n");

printf("나이가 20이상 30이하인 직원\n");

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

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

puts(people[i].name);

count++;

}

printf("해당 총인원: %d명\n", count);

}


9.

#include <stdio.h>

#include <string.h>

 

typedef struct {

    char name[20];

    char homeNum[20];

    char phoneNum[20];

} Book;


void main() {

    Book number[5];

    char search[20];

    int i;

    char an;

 

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

        printf("%d번째 전화번호부\n", i+1);

        printf("이    름 : ");        gets(number[i].name);

        printf("자택번호 : ");        gets(number[i].homeNum);

        printf("휴대전화 : ");        gets(number[i].phoneNum);

    }

    printf("\n");

 

    do {

        printf("이름을 검색하시오 : ");

        gets(search);

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

            if(strcmp(search, number[i].name) == 0) {

                printf("자택번호 : %s \n", number[i].homeNum);

                printf("휴대전화 : %s \n", number[i].phoneNum);

                printf("\n");

            }

        }

        printf("계속 찾으시겠습니까?(y/n) : ");

        scanf("%c", &an);

        fflush(stdin);

    } while(an != 'n');

}



10.

#include <stdio.h>

#include <stdlib.h>

#include <time.h> 

#define SIZE 52


typedef struct {

int value;

char suit;

} Card;


void init_card(Card pack[], char shape[]);

void print_card(Card pack[], int size);

void shuf_card(Card pack[], int size);


void main(){

Card pack[SIZE];

char shape[]={'c', 'd', 'h', 's'};

init_card(pack, shape);

printf("카드가 초기화 되었습니다.\n");

print_card(pack, SIZE); 

printf("\n");

shuf_card(pack, SIZE);

printf("카드를 섞었습니다.\n");

print_card(pack, SIZE);

printf("\n");

}


void init_card(Card pack[], char shape[]){

int i, j, count=0;

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

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

pack[count].value=j+1;

pack[count].suit=shape[i];

count++;

}

}

}


void print_card(Card pack[], int size){

int i, j;

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

printf("%3d%c", pack[i].value, pack[i].suit);

if((i+1)%13==0)

printf("\n");

}

}


void shuf_card(Card pack[], int size){

int i, j;

srand((unsigned)time(NULL));

Card temp;

for(i=0; i<size-1; i++){

j=rand()%(size-i)+i;

temp = pack[i];

pack[i] = pack[j];

pack[j] = temp;

}

}


11.

#include <stdio.h>

#define PI 3.14

#define SIZE 10


typedef struct {

int type;

union {

struct { double base, height; } tri;

struct { double width, height; } rect;

struct { double radius; } circ;

} shape;

} Figure;


void main(){

Figure data[SIZE];

int i=0;

char an;

do{

printf("저장하려는 도형의 종류를 입력하시오(0=삼각형, 1=사각형, 2=원): ");

scanf("%d", &data[i].type);

fflush(stdin);

switch(data[i].type){

case 0 : 

printf("삼각형의 밑변: "); scanf("%lf", &data[i].shape.tri.base);

printf("삼각형의 높이: "); scanf("%lf", &data[i].shape.tri.height);

i++;

break;

case 1 : 

printf("사각형의 밑변: "); scanf("%lf", &data[i].shape.rect.width);

printf("사각형의 높이: "); scanf("%lf", &data[i].shape.rect.height);

i++;

break;

case 2 : 

printf("원의 반지름: "); scanf("%lf", &data[i].shape.circ.radius);

i++;

break;

default:

printf("숫자를 0~2사이 값을 입력하시오\n");

fflush(stdin);

printf("더 저장하시겠습니까?(y/n)\n");

scanf("%c", &an);

} while(an!='n');

}



12.

#include <stdio.h>

#include <string.h>

#define SIZE 20


typedef struct {

char title[20];

char singer[20];

int class;

} Music;


int i;

int num[SIZE]={0};


void add_music (Music* list);

void print_music (Music* list);

void search_music (Music* list, int size);

void delete_music (Music* list);


void main(){

Music list[SIZE];

int n, result=1;

printf("======================\n");

printf(" 1. 추가(ADD)\n");

printf(" 2. 출력(PRINT)\n");

printf(" 3. 검색(SEARCH)\n");

printf(" 4. 삭제(DELETE)\n");

printf(" 5. 종료(EXIT)\n");

printf("======================\n");

while(result==1){

printf("메뉴를 선택하시오: ");

scanf("%d", &n);

fflush(stdin);

switch(n) { 

case 1: 

add_music(list);

break;

case 2:

print_music(list);

break;

case 3: 

search_music(list, SIZE);

break;

case 4:

delete_music(list);

break;

case 5:

result=0;

}

}


void add_music(Music* list){

do{

printf("1번부터 %d번까지 트랙이 있습니다. 몇 번 트랙에 저장하시겠습니까?", SIZE);

scanf("%d", &i);

fflush(stdin);

if(num[i-1]==1)

printf("이미 저장되어있는 트랙입니다. 다른 트랙번호를 입력하시오\n");

} while(num[i-1]==1);

printf("제목: "); gets(list[i-1].title);

printf("가수: "); gets(list[i-1].singer);

printf("종류(팝=0, 재즈=1, 클래식=2, 락=3): "); scanf("%d",&list[i-1].class);

fflush(stdin);

num[i-1]=1;

printf("\n");

}


void print_music(Music* list){

do{

printf("몇 번 트랙의 음악정보를 출력하시겠습니까? ");

scanf("%d", &i);

if(num[i-1]==0)

printf("해당 트랙엔 저장된 음악정보가 없습니다. 다른 트랙번호를 입력하시오\n");

} while(num[i-1]==0);

printf("제목: %s\n", list[i-1].title);

printf("가수: %s\n", list[i-1].singer);

switch(list[i-1].class){

case 0: printf("종류: 팝\n"); break;

case 1: printf("종류: 재즈\n"); break;

case 2: printf("종류: 클래식\n"); break;

case 3: printf("종류: 락\n"); break;

}

printf("\n"); 

}


void search_music(Music* list, int size){

char searchName[20];

printf("검색하고자 하는 노래 제목을 입력하시오: ");

gets(searchName); 

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

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

printf("제목: %s\n", list[i].title);

printf("가수: %s\n", list[i].singer);

switch(list[i].class){

case 0: printf("종류: 팝\n"); break;

case 1: printf("종류: 재즈\n"); break;

case 2: printf("종류: 클래식\n"); break;

case 3: printf("종류: 락\n"); break;

}

}

else {

printf("찾으시는 노래가 없습니다\n");

break; 

}

}

printf("\n");

}


void delete_music(Music* list){

do{

printf("몇 번 트랙의 음악정보를 삭제하시겠습니까? ");

scanf("%d", &i);

if(num[i-1]==0)

printf("해당 트랙엔 저장된 음악정보가 없습니다. 다른 트랙번호를 입력하시오\n");

} while(num[i-1]==0);

num[i-1]=0;

printf("%d번 트랙의 음악정보가 삭제되었습니다\n", i-1);

printf("\n");

}

블로그 이미지

얼음꿀차

책을 한 번 읽긴 읽어야겠는데 막상 읽자니 뭘 읽을지 고민되는 당신을 위해 읽을만한 책들을 알려드립니다!

,