1
0
This commit is contained in:
2024-05-23 22:43:00 +03:00
parent fbcdbf4e12
commit e0ad9b54e9
2 changed files with 39 additions and 38 deletions

View File

@@ -35,15 +35,15 @@
<h2>Список комплексных чисел</h2> <h2>Список комплексных чисел</h2>
<div id="complex-numbers"></div> <div id="complex-numbers"></div>
<h3>Выбранное комплексное число</h3>
<div id="selected-complex">Не выбрано</div>
<div> <div>
<h2>Произвести действие над комплексным числом</h2> <h2>Произвести действие над комплексным числом</h2>
<label for="real-part-op">Действительная часть:</label> <div>
<input type="number" id="real-part-op" step="any"> <label for="first-complex">Первое комплексное число:</label>
<label for="imaginary-part-op">Мнимая часть:</label> <select id="first-complex"></select>
<input type="number" id="imaginary-part-op" step="any">
<label for="second-complex">Второе комплексное число:</label>
<select id="second-complex"></select>
</div>
<button onclick="performOperation('add')">Сложить</button> <button onclick="performOperation('add')">Сложить</button>
<button onclick="performOperation('subtract')">Вычесть</button> <button onclick="performOperation('subtract')">Вычесть</button>
<button onclick="performOperation('multiply')">Умножить</button> <button onclick="performOperation('multiply')">Умножить</button>

View File

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