U E D R , A S I H C RSS

호너의법칙/박영창

개요

자료구조 숙제로 나온거라, 스펙이랑 완존히 100 파센트 다름;;

code

~cpp
/**
Chapter 1.2 (2)Problem Solving
programmed by eternalbleu (youngchang park, 2001 1612)

Date: 2005 09 10
**/
#include <iostream>
// 에러처리를 위한 매크로
#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1

#define PARAM_X		2.0
#define COEFFICIENTS	{1, 2, 3, 4, 5, 6, 7}

double horner_func(double, double*, int);

int max_exponentials = 0;

int main ()
{
	double x_param = PARAM_X;

	// index 차수의 계수를 배열로 지정한다.
	double coefficient [] = COEFFICIENTS;

	max_exponentials = sizeof(coefficient) / sizeof(double);

	std::cout<<horner_func(x_param, coefficient, 0)<<std::endl;
	
	return EXIT_SUCCESS;
}

/**
horner_func 호너의 법칙을 재귀함수로 구현한다.
@param x_param 인자로 전달될 미지수의 값.
@param coefficient 인자로 전달될 계수의 배열.
@param exponential 미지수 차수.

@return 호너함수의 재귀호출 값 + 호출 차수의 계수값
**/
double horner_func(double x_param, double* coefficient, int exponential)
{
	// Recursion Termination Condition
	if (exponential == max_exponentials-1) return coefficient[exponential];
	// General Recursion Call
	return horner_func(x_param, coefficient, exponential+1)*x_param + coefficient[exponential];
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:33
Processing time 0.0115 sec