파일 목록
# week04_02_chap03_05_self
def print_poly(f_x):
"""
다항식을 문자열로 출력하는 함수
:param f_x: 정수형 계수를 원소를 가지는 리스트
:return: 완성된 다항식 문자열
"""
term = len(f_x) - 1
poly_str = "f(x) = "
for i in range(len(fx)): # 0 ~ 4, i
coefficient = f_x[i]
if coefficient == 0:
term = term - 1
continue
elif coefficient > 0 and i != 0:
poly_str = poly_str + "+"
poly_str = poly_str + f"{coefficient}x^{term} "
term = term - 1
return poly_str
def calc_poly(x_value, f_x):
"""
다항식 산술 연산 함수
:param x_value: 독립 변수, 정수형
:param f_x: 정수형 계수를 원소를 가지는 리스트
:return: 산술 계산된 다항식 결과 값
"""
return_value = 0
term = len(f_x) - 1
for i in range(len(fx)):
coefficient = f_x[i]
return_value = return_value + (coefficient * (x_value ** term))
term = term - 1
return return_value
fx = [1, 2, 0, -3, 7] # f(x) = 1x^4 +2x^3 -3x^1 +7x^0
if __name__ == "__main__":
print(print_poly(fx))
x = int(input("X 값 : "))
print(calc_poly(x, fx))