// [ 문제23 -1 : 구조체 변수의 연산 ]
// 문제1 :
// 구조체 swap 구현
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
int xpos;
int ypos;
} Point;
void swap(Point* p1, Point* p2);
void inputData(Point* ptr);
void outputData(Point* ptr);
int main(void)
{
Point p[2];
for (int i = 0; i < 2; i++)
inputData(&p[i]);
swap(&p[0], &p[1]);
for (int i = 0; i < 2; i++)
outputData(&p[i]);
}
void swap(Point* p1, Point* p2)
{
Point temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void inputData(Point* ptr)
{
printf("좌표 입력 [x,y] :");
scanf_s("%d,%d", &(ptr->xpos),&(ptr->ypos));
}
void outputData(Point* ptr)
{
printf("좌표 : %d,%d\n", ptr->xpos,ptr->ypos);
}
// [ 문제23 -2 : 다양한 형태의 구조체 정의 ]
// 문제1 :
// 구조체 중첩 구현
// 직사각형의 넓이 구하기
// 4점의 좌표 출력
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
typedef struct
{
int xpos;
int ypos;
} Point;
typedef struct
{
Point* p1;
Point* p2;
double area;
} Square;
void inputData(Square* ptr);
void outputData(Square* ptr);
int main(void)
{
Square s;
inputData(&s);
outputData(&s);
}
void inputData(Square* ptr)
{
Point p[2];
printf("[ 좌표 입력 ]\n");
for (int i = 0; i < 2; i++)
{
printf("%d번째 좌표 :", i + 1);
scanf_s("%d,%d", &(p[i].xpos), &(p[i].ypos));
}
ptr->p1 = &p[0];
ptr->p2 = &p[1];
ptr->area = fabs((ptr->p2)->xpos - (ptr->p1)->xpos) * \
fabs((ptr->p2)->ypos - (ptr->p1)->ypos);
}
void outputData(Square* ptr)
{
printf("좌표%d : %d,%d\n", 1, (ptr->p1)->xpos, (ptr->p1)->ypos);
printf("좌표%d : %d,%d\n", 2, (ptr->p1)->xpos, (ptr->p2)->ypos);
printf("좌표%d : %d,%d\n", 3, (ptr->p2)->xpos, (ptr->p1)->ypos);
printf("좌표%d : %d,%d\n", 4, (ptr->p2)->xpos, (ptr->p2)->ypos);
printf("직사각형의 넓이 : %f\n", ptr->area);
}