From e0ad9b54e96d5cda857b8413991aee4c23165a4c Mon Sep 17 00:00:00 2001 From: lionarius Date: Thu, 23 May 2024 22:43:00 +0300 Subject: [PATCH] lab 8 2 --- Лаб8/Сайт/index.html | 14 +++++----- Лаб8/Сайт/main.js | 63 ++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/Лаб8/Сайт/index.html b/Лаб8/Сайт/index.html index a98e513..ed43aa9 100644 --- a/Лаб8/Сайт/index.html +++ b/Лаб8/Сайт/index.html @@ -35,15 +35,15 @@

Список комплексных чисел

-

Выбранное комплексное число

-
Не выбрано
-

Произвести действие над комплексным числом

- - - - +
+ + + + + +
diff --git a/Лаб8/Сайт/main.js b/Лаб8/Сайт/main.js index 4f55a50..b290712 100644 --- a/Лаб8/Сайт/main.js +++ b/Лаб8/Сайт/main.js @@ -1,5 +1,4 @@ let complexNumbers = []; -let selectedComplexIndex = null; function createComplexNumber() { const realPart = parseFloat(document.getElementById('real-part').value); @@ -17,6 +16,9 @@ function createComplexNumber() { const complex = new Complex(realPart, imaginaryPart); complexNumbers.push(complex); + document.getElementById('real-part').value = ''; + document.getElementById('imaginary-part').value = ''; + renderComplexNumbers(); clearActionsLog(); } @@ -25,59 +27,58 @@ function renderComplexNumbers() { const container = document.getElementById('complex-numbers'); container.innerHTML = ''; + const firstSelect = document.getElementById('first-complex'); + const secondSelect = document.getElementById('second-complex'); + firstSelect.innerHTML = ''; + secondSelect.innerHTML = ''; + complexNumbers.forEach((complex, index) => { const complexDiv = document.createElement('div'); complexDiv.className = 'complex-number'; complexDiv.innerHTML = ` - Комплексное число ${ - index + 1 - }: ${complex.toString()}
- + Число ${index + 1}: ${complex.toString()} `; container.appendChild(complexDiv); + + const option1 = document.createElement('option'); + option1.value = index; + option1.text = `Число ${index + 1}`; + firstSelect.appendChild(option1); + + const option2 = document.createElement('option'); + option2.value = index; + option2.text = `Число ${index + 1}`; + secondSelect.appendChild(option2); }); } -function setSelectedComplex(index) { - selectedComplexIndex = index; - document.getElementById('selected-complex').textContent = `Выбрано число ${ - index + 1 - }: ${complexNumbers[index].toString()}`; -} - function performOperation(operation) { - if (selectedComplexIndex === null) { - alert('Пожалуйста, выберите комплексное число.'); - return; - } - - const complex = complexNumbers[selectedComplexIndex]; - const realPart = parseFloat(document.getElementById('real-part-op').value); - const imaginaryPart = parseFloat( - document.getElementById('imaginary-part-op').value + const firstIndex = parseInt(document.getElementById('first-complex').value); + const secondIndex = parseInt( + document.getElementById('second-complex').value ); - const otherComplex = new Complex(realPart, imaginaryPart); - if (isNaN(realPart) || isNaN(imaginaryPart)) { - alert( - 'Пожалуйста, введите корректные значения для действительной и мнимной частей.' - ); + if (isNaN(firstIndex) || isNaN(secondIndex)) { + alert('Пожалуйста, выберите оба комплексных числа.'); return; } + const complex1 = complexNumbers[firstIndex]; + const complex2 = complexNumbers[secondIndex]; + if (operation === 'add') { - complex.add(otherComplex); + complex1.add(complex2); } else if (operation === 'subtract') { - complex.subtract(otherComplex); + complex1.subtract(complex2); } else if (operation === 'multiply') { - complex.multiply(otherComplex); + complex1.multiply(complex2); } else if (operation === 'divide') { - complex.divide(otherComplex); + complex1.divide(complex2); } renderComplexNumbers(); clearActionsLog(); - logActions(complex); + logActions(complex1); } function clearActionsLog() {