์ƒˆ์†Œ์‹

Languages/C

๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ’ ์„œ๋กœ ๋ฐ”๊พธ๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ์‹ค์Šต ์ฝ”๋“œ๋Š” ๊น€์„ฑ์—ฝ ๋Œ€ํ‘œ๋‹˜์˜ 

"Do it ! C์–ธ์–ด ์ž…๋ฌธ" ์ฑ…์˜ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์‹ค์Šตํ•˜๊ณ  ๋‚ด์šฉ์„ ์š”์•ฝํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

โ€ป ์ƒ์—…์šฉ๋„๊ฐ€ ์•„๋‹ˆ๋ฉฐ, tips๋ฅผ ํ†ตํ•ด ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ณต์Šตํ•˜๊ณ ์ž ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ˆ

๋ถˆ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ธˆ์ง€ํ•ฉ๋‹ˆ๋‹ค.

 


 

1.  ์ง์ ‘ ์ฃผ์†Œ ์ง€์ • ๋ฐฉ์‹์œผ๋กœ ๋ณ€์ˆ˜ ๊ฐ’ ๊ตํ™˜ํ•˜๊ธฐ.

 

๊ฐ’์„ ๊ตํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ main ํ•จ์ˆ˜ ์•ˆ์— ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๋ฐ, 

๋ถˆํŽธํ•  ์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด Swapํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.

 

#include<stdio.h>
// temp ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ a์™€ b์˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜
void Swap(int a, int b)
{
	int temp = a; //a = 96, b = 5
	a = b; //a = 5, b = 5 
	b = temp; // a = 5; b = 96;
}

void main()
{
	int start = 96, end = 5;

	printf("before : start  = %d, end = %d \n", start, end);
	if (start > end) {
		Swap(start, end); // start์™€ end์˜ ๊ฐ’์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ Swap ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•จ.
	}
	printf("after : start =%d, end =%d \n", start, end);
	
	return 0;
}


 

๊ฒฐ๊ณผ

 

 

๊ฒฐ๊ณผ๋Š” ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

 

๋ถ„๋ช… start ๊ฐ’์ด end ๊ฐ’ ๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด๋ฌธ์— ๋งŒ์กฑํ•˜์—ฌ Swapํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ์„ ๊ฒƒ์ด๊ณ 

start, end ๋ณ€์ˆ˜ ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜ a,b๋กœ ์ „๋‹ฌ ๋ฐ›์•„์„œ a,b๊ฐ€ ์„œ๋กœ ๊ตํ™˜๊นŒ์ง€ ํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

ํ•˜์ง€๋งŒ main ํ•จ์ˆ˜์˜ start, end์™€ ์ƒ๊ด€ ์—†์ด ๋ณ€๊ฒฝ๋œ ๋ณ€์ˆ˜๊ฐ€ a,b,๋ผ๋Š” ๊ฒƒ์ด ์ด ์˜ˆ์ œ์˜ ๋ฌธ์ œ ์ ์ด๋‹ค.

 

๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์ƒ (bp์˜ ํ๋ฆ„๋„ ์•Œ์•„์•ผ ํ•จ)

๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ง€์—ญ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ํฌ์ธํ„ฐ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

 

ํฌ์ธํ„ฐ ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด์„œ ๊ฐ’ ๋Œ€์‹  ๊ฐ ๋ณ€์ˆ˜์˜ '์ฃผ์†Œ'๋ฅผ ๋„˜๊ฒจ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

main ํ•จ์ˆ˜์˜ start์™€ end ๋ณ€์ˆ˜ ์ฃผ์†Œ๋ฅผ Swapํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ ค๋ฉด Swapํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๊ฐ€ ์™€์•ผํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ Swapํ•จ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋œ๋‹ค.

 

 

2. ๊ฐ„์ ‘ ์ฃผ์†Œ ์ง€์ • ๋ฐฉ์‹(ํฌ์ธํ„ฐ)์œผ๋กœ ๋ณ€์ˆ˜ ๊ฐ’ ๊ตํ™˜ํ•˜๊ธฐ.

 

#include<stdio.h>
//temp๋ฅผ ์ด์šฉํ•˜์—ฌ a์™€ b์˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ๊ธฐ

void Swap(int* pa, int* pb)
{
	int temp = *pa; // start(*pa) = 96, end(*pb)=5;
	*pa = *pb; //start(*pa) = 5, end(*pb)=5;
	*pb = temp; //start(*pa)= 5, end(*pb) = 96;

}

void main() {

	int start = 96, end = 5;

	printf("before : start = %d, end = %d\n", start, end);
	if (start > end) {
		Swap(&start, &end);//๊ฐ’์ด ์•„๋‹Œ ์ฃผ์†Œ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค.
	}
	printf("after : start = %d, end = %d\n", start, end);
}

 

๊ฒฐ๊ณผ 

 

 

start์™€ end๊ฐ’์ด ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

Swap ํ•จ์ˆ˜์—์„œ pa, pbํฌ์ธํ„ฐ๋ฅผ *pa, *pb์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด pa,pb ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๋Œ€์ƒ ๋ฉ”๋ชจ๋ฆฌ์ธ

start, end์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— Swapํ•จ์ˆ˜ ์ข…๋ฃŒ ํ›„ main์œผ๋กœ ๋ณต๊ท€ํ–ˆ์„ ๋•Œ, ์ฃผ์†Œ์— ํ•ด๋‹นํ•˜๋Š”

๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋” ์ž์„ธํ•œ ๋‚ด์šฉ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ๋ณ€ํ™” ๊ณผ์ •์„ ํ™•์ธ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๊น€์„ฑ์—ฝ ์Œค์˜ "Do it C์–ธ์–ด ์ž…๋ฌธ" ์ฑ…์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.