99 lines
3.0 KiB
JavaScript
99 lines
3.0 KiB
JavaScript
let complexNumbers = [];
|
||
|
||
function createComplexNumber() {
|
||
const realPart = parseFloat(document.getElementById('real-part').value);
|
||
const imaginaryPart = parseFloat(
|
||
document.getElementById('imaginary-part').value
|
||
);
|
||
|
||
if (isNaN(realPart) || isNaN(imaginaryPart)) {
|
||
alert(
|
||
'Пожалуйста, введите корректные значения для действительной и мнимой частей.'
|
||
);
|
||
return;
|
||
}
|
||
|
||
const complex = new Complex(realPart, imaginaryPart);
|
||
complexNumbers.push(complex);
|
||
|
||
document.getElementById('real-part').value = '';
|
||
document.getElementById('imaginary-part').value = '';
|
||
|
||
renderComplexNumbers();
|
||
clearActionsLog();
|
||
}
|
||
|
||
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()}
|
||
`;
|
||
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 performOperation(operation) {
|
||
const firstIndex = parseInt(document.getElementById('first-complex').value);
|
||
const secondIndex = parseInt(
|
||
document.getElementById('second-complex').value
|
||
);
|
||
|
||
if (isNaN(firstIndex) || isNaN(secondIndex)) {
|
||
alert('Пожалуйста, выберите оба комплексных числа.');
|
||
return;
|
||
}
|
||
|
||
const complex1 = complexNumbers[firstIndex];
|
||
const complex2 = complexNumbers[secondIndex];
|
||
|
||
if (operation === 'add') {
|
||
complex1.add(complex2);
|
||
} else if (operation === 'subtract') {
|
||
complex1.subtract(complex2);
|
||
} else if (operation === 'multiply') {
|
||
complex1.multiply(complex2);
|
||
} else if (operation === 'divide') {
|
||
complex1.divide(complex2);
|
||
}
|
||
|
||
renderComplexNumbers();
|
||
clearActionsLog();
|
||
logActions(complex1);
|
||
}
|
||
|
||
function clearActionsLog() {
|
||
const log = document.getElementById('actions-log');
|
||
log.textContent = '';
|
||
}
|
||
|
||
function logActions(complex) {
|
||
const log = document.getElementById('actions-log');
|
||
complex.actions.forEach((action) => {
|
||
log.textContent += complex.formatAction(action) + '\n';
|
||
});
|
||
}
|
||
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
renderComplexNumbers();
|
||
});
|