티스토리 뷰

문제 1. 2차원 평면상에서 좌표를 표현할 수 있는 구조체를 다음과 같이 정의하였다.

typedef struct __Point
{
	int xPos;
	int yPos;
} Point;

위의 구조체를 기반으로 두 점의 합을 계산하는 함수를 다음의 형태로 정의하고 있을 때, 임의의 두 점을 선언하여 함수를 이용한 덧셈연산을 진행하는 main함수를 정의해보자.

Point& PntAdder(const Point &p1, const Point &p2);


#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

typedef struct __Point
{
	int xPos;
	int yPos;
} Point;

Point& PntAdder(const Point &p1, const Point &p2)
{
	Point *pPtr = new Point;

	pPtr->xPos = p1.xPos + p2.xPos;
	pPtr->yPos = p1.yPos + p2.yPos;

	return *pPtr;
}

int main()
{
	Point *pptr1 = new Point;
	pptr1->xPos = 10;
	pptr1->yPos = 10;

	Point *pptr2 = new Point;
	pptr2->xPos = 20;
	pptr2->yPos = 30;

	Point &pRef = PntAdder(*pptr1, *pptr2);

	cout << "두 점의 합 : (" << pRef.xPos << ", " << pRef.yPos << ")" << endl;

	return 0;
}


댓글