728x90
from fractions import Fraction
def calculate_operations(values):
fractions = [Fraction(value) for value in values]
# 곱셈
product = 1
product_explanation = ''
for i, frac in enumerate(fractions):
product *= frac
product_explanation += f'{frac}'
if i != len(fractions) - 1:
product_explanation += ' * '
print(f'입력한 값들의 곱: {product_explanation} = {product}')
# 교환법칙
reversed_fractions = fractions[::-1]
commutative_product = 1
commutative_explanation = ''
for i, frac in enumerate(reversed_fractions):
commutative_product *= frac
commutative_explanation += f'{frac}'
if i != len(reversed_fractions) - 1:
commutative_explanation += ' * '
print(f'\n교환법칙 적용:')
print(f'원래 값: {product_explanation} = {product}')
print(f'교환된 값: {commutative_explanation} = {commutative_product}')
print(f'교환법칙 적용 결과: {product == commutative_product}')
# 결합법칙
split_index = len(fractions) // 2
first_half = fractions[:split_index]
second_half = fractions[split_index:]
first_half_product = 1
second_half_product = 1
first_half_explanation = ''
second_half_explanation = ''
for i, frac in enumerate(first_half):
first_half_product *= frac
first_half_explanation += f'{frac}'
if i != len(first_half) - 1:
first_half_explanation += ' * '
for i, frac in enumerate(second_half):
second_half_product *= frac
second_half_explanation += f'{frac}'
if i != len(second_half) - 1:
second_half_explanation += ' * '
combined_product = first_half_product * second_half_product
print(f'\n결합법칙 적용:')
print(f'원래 값: ({first_half_explanation}) * ({second_half_explanation}) = {combined_product}')
print(f'분해된 값: {product_explanation} = {product}')
print(f'결합법칙 적용 결과: {combined_product == product}')
# 사용자로부터 값 입력 받기
inputs = []
for i in range(8):
value = input(f"{i + 1}번째 유리수를 입력하세요: ")
inputs.append(value)
# 계산 및 결과 출력
calculate_operations(inputs)
728x90
'PY(Python Image Processing) > 중학수학기초' 카테고리의 다른 글
| 17제곱근의곱셈과나눗셈 (0) | 2024.08.19 |
|---|---|
| 16무리수계산의기초 (0) | 2024.08.15 |
| 14정수와유리수의나눗셈 (0) | 2024.08.15 |
| 13정수와유리수의곱셈 (0) | 2024.08.15 |
| 11정수와유리수의덧셈과뺄셈 (0) | 2024.08.15 |