본문 바로가기

c언어

15. 함수에 대한 고급 이론

1. 재귀 호출을 이용하여 값이 60 이하인 피보나치(Fibonacci) 수열을 출력하는 프로그램을 작성하세요. 참고로 피보나치 수열은 다음과 같이 앞 두 숫자의 합이 이어지는 모습을 하고 있습니다.

1  1  2  3  5  8  13  21  34  55

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void Fibonacci(int nLimit, int nBefore, int nAfter)
{
	int nTmp = 0;
	nTmp = nBefore + nAfter;
	nBefore = nAfter;
	nAfter = nTmp;
	if (nBefore  <= nLimit)
	{
		printf("%d ", nBefore);
		Fibonacci(nLimit, nBefore, nAfter);
	}

	return;
}

void main(void)
{
	Fibonacci(60, 0, 1);
	return;
}

[실행결과]

 

 

2. 인자로 char* 형을 받아서 문자열을 거꾸로 출력하는 재귀 호출 함수를 작성하세요. 함수의 원형은

void PutString(char* pszData)와 같습니다.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>

void PutString(char* pszData);

void main(void)
{
	char* pszData = "Hello, World!";
	puts("Before)");
	puts(pszData);
	puts("After)");
	PutString(pszData);
	return;
}

void PutString(char* pszData)
{
	if (*pszData != '\0')
	{ 
		PutString(pszData + 1);
		putc(*pszData, stdout);
	}
	return;
}

[실행결과]

 

 

3. char* 형을 반환하고 int형 매개 변수가 둘인 함수에 대하여 포인터 변수를 선언하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>

char* TestFunc(int nData1, int nData2)
{
	if (nData1 > nData2)
	{
		return "Left";
	}
	else if (nData1 < nData2)
	{
		return "Right";
	}
	else
	{
		return "Same";
	}
}

void main()
{
	char* (*pfTestFunc)(int, int) = TestFunc;
	printf("%d <-> %d : %s \n", 10, 20, pfTestFunc(10, 20));
	return;
}

[실행결과]

 

 

4. 3번 문제에 해당하는 함수 포인터를 매개 변수로 받고 int형을 반환하는 함수에 대하여 포인터 변수를 선언하고, 실제로 제대로 선언되었음을 확인할 수 있는 간단한 예제를 작성하세요. 즉, 이 포인터 변수에 저장할 수 있는 함수를 선언하고 포인터 변수로 호출하여 동작할 수 있게 코드를 작성해야 합니다.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>

int g_nData1 = 10;
int g_nData2 = 20;

char* TestFunc(int nData1, int nData2);
int MainTestFunc(char* (*pfTestFunc)(int, int));

void main()
{
	char* (*pfTestFunc)(int, int) = TestFunc;
	int (*pfMainTestFunc)(char* (*TestFunc(int, int))) = MainTestFunc;
	printf("결과(%d, %d) : %d \n", g_nData1, g_nData2, pfMainTestFunc(pfTestFunc));
	return;
}


char* TestFunc(int nData1, int nData2)
{
	if (nData1 > nData2)
	{
		return "Left";
	}
	else if (nData1 < nData2)
	{
		return "Right";
	}
	else
	{
		return "Same";
	}
}


int MainTestFunc(char* (*pfTestFunc)(int, int))
{
	char* pszResult = pfTestFunc(g_nData1, g_nData2);
	if (strcmp(pszResult, "Left") == 0)
	{
		return 1;
	}
	else if (strcmp(pszResult, "Right") == 0)
	{
		return -1;
	}
	else if (strcmp(pszResult, "Same") == 0)
	{
		return 0;
	}
	else
	{
		return -9999;
	}
}

[실행결과]

 

 

5. 인자로 int형을 받고 반환값이 없는 함수에 대하여 포인터 변수를 선언하세요. 단, 함수 호출 규약을 __stdcall로 설정해야 합니다. 또한, 4번 문제처럼 변수를 실제 사용하는 간단한 예제를 작성하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>

void __stdcall TestFunc(int);

void main()
{
	void (__stdcall *pfTestFunc)(int) = TestFunc;
	pfTestFunc(10);
	pfTestFunc(20);
	pfTestFunc(30);
	pfTestFunc(40);
	return;
}

void __stdcall TestFunc(int nData)
{
	printf("TestFunc() - %d \n", nData);
	return;
}

[실행결과]

 

'c언어' 카테고리의 다른 글

17. 기본 자료구조  (0) 2023.06.28
16. 구조체와 공용체  (0) 2023.06.28
14. 변수에 대한 고급 이론  (0) 2023.06.26
13. 유틸리티 함수  (0) 2023.06.23
12. 문자, 문자열 처리 함수  (0) 2023.06.21