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

View File

@@ -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 = `
<strong>Комплексное число ${
index + 1
}:</strong> ${complex.toString()}<br>
<button onclick="setSelectedComplex(${index})">Выбрать</button>
<strong>Число ${index + 1}:</strong> ${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('Пожалуйста, выберите комплексное число.');
const firstIndex = parseInt(document.getElementById('first-complex').value);
const secondIndex = parseInt(
document.getElementById('second-complex').value
);
if (isNaN(firstIndex) || isNaN(secondIndex)) {
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 otherComplex = new Complex(realPart, imaginaryPart);
if (isNaN(realPart) || isNaN(imaginaryPart)) {
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() {