1
0
This commit is contained in:
2024-05-23 19:40:42 +03:00
parent f6806d8a5a
commit fbcdbf4e12
5 changed files with 248 additions and 0 deletions

Binary file not shown.

27
Лаб8/Сайт/base.js Normal file
View File

@@ -0,0 +1,27 @@
function BaseObject() {
this.actions = [];
}
BaseObject.prototype.registerAction = function (actionName, args) {
const timestamp = new Date().toISOString();
this.actions.push({ actionName, args, timestamp });
};
BaseObject.prototype.clearActions = function () {
this.actions = [];
};
BaseObject.prototype.getActions = function () {
return Array.from(this.actions);
};
BaseObject.prototype.formatAction = function (action) {
return `Действие: ${action.actionName}\n\tПараметры: ${action.args}\n\tВремя: ${action.timestamp}`;
};
BaseObject.prototype.logActions = function () {
console.log('Зарегистрированные действия:');
this.actions.forEach((action) => {
console.log(this.formatAction(action));
});
};

View File

@@ -0,0 +1,67 @@
function Complex(real, imaginary) {
BaseObject.call(this);
this.real = real || 0;
this.imaginary = imaginary || 0;
}
Complex.prototype = Object.create(BaseObject.prototype);
Complex.prototype.constructor = Complex;
Complex.prototype.setReal = function (real) {
this.real = real;
this.registerAction('setReal', [real]);
};
Complex.prototype.setImaginary = function (imaginary) {
this.imaginary = imaginary;
this.registerAction('setImaginary', [imaginary]);
};
Complex.prototype.getReal = function () {
this.registerAction('getReal', []);
return this.real;
};
Complex.prototype.getImaginary = function () {
this.registerAction('getImaginary', []);
return this.imaginary;
};
Complex.prototype.add = function (other) {
this.real += other.real;
this.imaginary += other.imaginary;
this.registerAction('add', [other]);
};
Complex.prototype.subtract = function (other) {
this.real -= other.real;
this.imaginary -= other.imaginary;
this.registerAction('subtract', [other]);
};
Complex.prototype.multiply = function (other) {
const real = this.real * other.real - this.imaginary * other.imaginary;
const imaginary = this.real * other.imaginary + this.imaginary * other.real;
this.real = real;
this.imaginary = imaginary;
this.registerAction('multiply', [other]);
};
Complex.prototype.divide = function (other) {
const denominator =
other.real * other.real + other.imaginary * other.imaginary;
const real =
(this.real * other.real + this.imaginary * other.imaginary) /
denominator;
const imaginary =
(this.imaginary * other.real - this.real * other.imaginary) /
denominator;
this.real = real;
this.imaginary = imaginary;
this.registerAction('divide', [other]);
};
Complex.prototype.toString = function () {
this.registerAction('toString', []);
return `${this.real} + ${this.imaginary}i`;
};

View File

@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Комплексные числа</title>
<script src="base.js" defer></script>
<script src="complex.js" defer></script>
<script src="main.js" defer></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.complex-number {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Комплексные числа</h1>
<div>
<h2>Создать комплексное число</h2>
<label for="real-part">Действительная часть:</label>
<input type="number" id="real-part" step="any">
<label for="imaginary-part">Мнимая часть:</label>
<input type="number" id="imaginary-part" step="any">
<button onclick="createComplexNumber()">Создать</button>
</div>
<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">
<button onclick="performOperation('add')">Сложить</button>
<button onclick="performOperation('subtract')">Вычесть</button>
<button onclick="performOperation('multiply')">Умножить</button>
<button onclick="performOperation('divide')">Разделить</button>
</div>
<h2>Лог действий</h2>
<pre id="actions-log"></pre>
</body>
</html>

97
Лаб8/Сайт/main.js Normal file
View File

@@ -0,0 +1,97 @@
let complexNumbers = [];
let selectedComplexIndex = null;
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);
renderComplexNumbers();
clearActionsLog();
}
function renderComplexNumbers() {
const container = document.getElementById('complex-numbers');
container.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>
`;
container.appendChild(complexDiv);
});
}
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 otherComplex = new Complex(realPart, imaginaryPart);
if (isNaN(realPart) || isNaN(imaginaryPart)) {
alert(
'Пожалуйста, введите корректные значения для действительной и мнимной частей.'
);
return;
}
if (operation === 'add') {
complex.add(otherComplex);
} else if (operation === 'subtract') {
complex.subtract(otherComplex);
} else if (operation === 'multiply') {
complex.multiply(otherComplex);
} else if (operation === 'divide') {
complex.divide(otherComplex);
}
renderComplexNumbers();
clearActionsLog();
logActions(complex);
}
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();
});