Çʵå Å×½ºÆ®
1. ´ÙÀ½Àº µÎ Á¡ÀÇ °Å¸®¸¦ ±¸ÇÏ´Â ¼Ò½º Äڵ带 2°³ÀÇ ±¸Çö ÆÄÀÏ¿¡ ¹«ÀÛÁ¤ ¿Å°Ü ³õÀº °ÍÀÌ´Ù. ÀÌ ÇÁ·Î±×·¥ÀÌ Àß µ¹¾Æ°¡°Ô ÇÏ·Á¸é ¿©·¯ºÐÀÌ Çì´õ ÆÄÀÏÀ» ¸¸µé°í ÇÊ¿äÇÑ Äڵ带 Ãß°¡ÇØÁÖ¾î¾ß ÇÑ´Ù. ÀÏ´Ü ±¸Çö ÆÄÀϰú ÇÔ¼ö¿Í ±¸Á¶Ã¼ÀÇ °ü°è¸¦ ±×¸²À¸·Î ±×·Áº» ÈÄ¿¡, Çì´õ ÆÄÀϰú ÇÊ¿äÇÑ Äڵ带 Ãß°¡Çغ¸ÀÚ. (ÈùÆ®: ÇÁ·Î±×·¥ÀÌ Á¦´ë·Î µ¹¾Æ°£´Ù¸é, ¿©·¯ºÐÀÌ Á¦´ë·Î ÇÑ °ÍÀÌ´Ù)
Example1.cpp
1.
#include
<iostream>
2.
#include
<cmath>
3.
using
namespace std;
4.
5.
struct
Point
6.
{
7.
int
x, y;
8.
};
9.
10.
int
main()
11.
{
12.
//
µÎ Á¡À» ¸¸µç´Ù.
13.
Point
a = {0, 0};
14.
Point
b = {3, 4};
15.
16.
//
µÎ Á¡ÀÇ °Å¸®¸¦ ±¸ÇÑ´Ù.
17.
double
dist_a_b = Distance(a, b);
18.
19.
//
°á°ú¸¦ Ãâ·ÂÇÑ´Ù.
20.
cout
<< "(" << a.x << ", " << a.y
<< ") ¿Í ";
21.
cout
<< "(" << b.x << ", " << b.y
<< ") ÀÇ °Å¸® = " << dist_a_b
<< "\n";
22.
23.
return
0;
24.
}
Example2.cpp
25.
double
Distance(Point p1, Point p2)
26.
{
27.
//
ÇÇŸ°í¶ó½ºÀÇ Á¤¸®¸¦ »ç¿ëÇÑ´Ù.
28.
double
distance;
29.
distance
= sqrt( pow(p1.x - p2.x, 2) +
pow(p1.y - p2.y, 2) );
30.
31.
//
°á°ú¸¦ ¹ÝȯÇÑ´Ù.
32.
return
distance;
33.
}
Á¤´ä->
--- Point.h
#ifndef POINT_H
#define POINT_H
struct Point
{
int x, y;
};
#endif // POINT_H
--- Example2.h
#ifndef EXAMPLE2_H
#define EXAMPLE2_H
double
Distance(Point p1, Point p2);
#endif EXAMPLE2_H
--- Example1.cpp
#include
"Point.h"
#include
"Example2.h"
#include
<iostream>
using namespace std;
int main()
{
//
µÎ Á¡À» ¸¸µç´Ù.
Point
a = {0, 0};
Point
b = {3, 4};
//
µÎ Á¡ÀÇ °Å¸®¸¦ ±¸ÇÑ´Ù.
double
dist_a_b = Distance(a, b);
//
°á°ú¸¦ Ãâ·ÂÇÑ´Ù.
cout
<< "(" << a.x << ", " << a.y
<< ") ¿Í ";
cout
<< "(" << b.x << ", " << b.y
<< ") ÀÇ °Å¸® = " << dist_a_b <<
"\n";
return
0;
}
--- Example2.cpp
#include
"Point.h"
#include
"Example2.h"
#include <cmath>
using namespace std;
double Distance(Point
p1, Point p2)
{
//
ÇÇŸ°í¶ó½ºÀÇ Á¤¸®¸¦ »ç¿ëÇÑ´Ù.
double
distance;
distance
= sqrt( pow(p1.x - p2.x, 2) +
pow(p1.y - p2.y, 2) );
//
°á°ú¸¦ ¹ÝȯÇÑ´Ù.
return
distance;
}