본문 바로가기

C++

(19)
[C++]헤더파일 사용 예제 example1.cpp 1234567891011121314151617 #include "point.h"#include "example2.h" int main(){ // 두 점을 만든다. Point a = {100, 100}; Point b = {200, 200}; // 함수를 호출한다. double dist; dist = Distance(a, b); return 0;} cs example2.cpp 12345678910 #include "example2.h"#include "point.h" double Distance(const Point& pt1, const Point& pt2) { // 이 함수의 내용은 생략한다. return 0.0f;} Colored by Color Scriptercs example..
[C++]헤더파일 추가하기 예제 (두 점 사이의 거리 구하기) 일반적인 소스코드 12345678910111213141516171819202122232425262728293031323334353637383940#include #include using namespace std; struct Point{ int x, y;}; // 두 점의 거리를 구하는 함수의 원형double Distance(Point p1, Point p2); int main(){ // 두 점을 만든다. Point a = { 0, 0 }; Point b = { 3, 4 }; // 두 점의 거리를 구한다. double dist_a_b = Distance(a, b); // 결과를 출력한다. cout
[C++]재귀호출을 이용한 2진수 변환 1234567891011121314151617181920#include using namespace std; void Convert2Bin(int dec){ if (dec
[C++]레퍼런스를 이용한 함수 예제 12345678910111213141516171819202122232425262728293031#include using namespace std; void GCD_LCM(int a, int b, int* pgcd, int* plcm) { int z; int x = a; int y = b; while (true) { z = x % y if (0 == z) break; x = y; y = z; } *pgcd = y; *plcm = a * b / *pgcd;} int main(){ int gcd = 0; int lcm = 0; GCD_LCM(28, 35, &gcd, &lcm); cout
[C++]삼항연산자를 이용한 함수 인자 전달 예제 1234567891011121314151617#include using namespace std; int max(int a, int b){ return a > b ? a:b} int main(){ //int ret = max(3,5); int arg1 =3; int arg2 =5; int ret= max(arg1,arg2); return 0;}cs
[C++]기본적인 인자전달 예제 , 팩토리얼을 구하는 함수 1234567891011121314151617181920212223#include using namespace std; int Factorial (int n); int main(){ int result; result = Factorial(5); cout
[C++]함수 기초 예제 12345678910111213141516#include using namespace std; int three(){ return 3;} int main(){ int ret; ret = three(); cout
[C++]서로를 가리키는 구조체 예제 1234567891011121314151617181920212223242526272829#include using namespace std; struct Dizzy{ int id; // 구조체 변수마다 갖는 고유한 값 Dizzy* p; // Dizzy 구조체를 가리키는 포인터}; int main(){ // Dizzy 객체를 3개 만들고, // 서로를 가리키도록 만든다. Dizzy a, b, c; a.id = 1; a.p = &b; b.id = 2; b.p = &c; c.id = 3; c.p = &a; // a 만 사용해서 a, b, c 모두에 접근한다. cout