C 언어 중첩구조체 활용과 구조체 포인터를 사용한 직사각형 좌표 출력

s_rectangle 구조체는 중첩구조체로 left_top이라는 s_point 구조체 멤버변수를 가진다.
직사각형의 너비와 너비를 입력받아 나머지 right_top, left_bottom, right_bottom 좌표를
출력해주는 코드이다.

#include <stdio.h>

struct s_point
{
        int x;
        int y;
};

struct s_rectangle
{
        struct s_point left_top;
        int width; //너비
        int height; //높이
};

void print_rectangle_info(struct s_rectangle rectangle);
void input_rectangle_info(struct s_rectangle *pRectangle);

int main()
{
        struct s_rectangle rectangle = {0};

        input_rectangle_info(&rectangle);
        print_rectangle_info(rectangle);

        return 0;
}

void input_rectangle_info(struct s_rectangle *pRectangle)
{
        if(NULL != pRectangle)
        {
                printf("기준점 (left_top)의 위치 x는 ?");
                scanf("%d", &(pRectangle -> left_top.x));

                printf("기준점 (left_top)의 위치 y는 ?");
                scanf("%d", &(pRectangle -> left_top.y));

                printf("너비는 ? ");
                scanf("%d",&(pRectangle -> width));

                printf("높이는 ? ");
scanf("%d",&(pRectangle -> height));

        }
        else
        {
                printf("NULL 오류 !!!: input_rectangle_info() \n");
        }
}

void print_rectangle_info(struct s_rectangle rectangle)
{
        printf("Left-top: (%d , %d) \n", rectangle.left_top.x, rectangle.left_top.y);
        printf("Right-top: (%d , %d) \n", rectangle.left_top.x + rectangle.width, rectangle.left_top.y);
        printf("Left-bottom: (%d , %d) \n", rectangle.left_top.x, rectangle.left_top.y + rectangle.height);
        printf("Right-bottom: (%d , %d) \n", rectangle.left_top.x + rectangle.width, rectangle.left_top.y + rectangle.height);

}


실행 시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./print_points
기준점 (left_top)의 위치 x는 ?1
기준점 (left_top)의 위치 y는 ?5
너비는 ? 20
높이는 ? 30
Left-top: (1 , 5)
Right-top: (21 , 5)
Left-bottom: (1 , 35)
Right-bottom: (21 , 35)

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About idempotent

About G1 GC

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

sneak peek jitpack

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

DDD(Domain Driven Design) - Aggregate (어그리게잇)

Strategy Pattern In Spring (feat. JPA)