-
Notifications
You must be signed in to change notification settings - Fork 103
6214 Смирнов МВ Лаб. 4 #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
The head ref may contain hidden characters: "\u041B\u0420-4"
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="ru"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Простой калькулятор</title> | ||
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> | ||
| <link rel="stylesheet" href="style.css"> | ||
| </head> | ||
| <body> | ||
| <div class="calculator-container"> | ||
| <div class="header"> | ||
| <h1><i class="fas fa-calculator"></i> Калькулятор</h1> | ||
| </div> | ||
|
|
||
| <div class="input-group"> | ||
| <label for="num1">Первое число:</label> | ||
| <input type="number" id="num1" class="input-field" placeholder="Введите число" step="any" value="15"> | ||
| </div> | ||
|
|
||
| <div class="input-group"> | ||
| <label for="num2">Второе число:</label> | ||
| <input type="number" id="num2" class="input-field" placeholder="Введите число" step="any" value="5"> | ||
| </div> | ||
|
|
||
| <div class="operation-selector"> | ||
| <label for="operation">Операция:</label> | ||
| <select id="operation" class="operation-select"> | ||
| <option value="add">Сложение (+)</option> | ||
| <option value="subtract">Вычитание (-)</option> | ||
| <option value="multiply">Умножение (×)</option> | ||
| <option value="divide">Деление (÷)</option> | ||
| </select> | ||
|
|
||
| <div class="operation-icons"> | ||
| <div class="operation-icon" data-operation="add"> | ||
| <i class="fas fa-plus"></i> | ||
| <span>Сложение</span> | ||
| </div> | ||
| <div class="operation-icon" data-operation="subtract"> | ||
| <i class="fas fa-minus"></i> | ||
| <span>Вычитание</span> | ||
| </div> | ||
| <div class="operation-icon" data-operation="multiply"> | ||
| <i class="fas fa-times"></i> | ||
| <span>Умножение</span> | ||
| </div> | ||
| <div class="operation-icon" data-operation="divide"> | ||
| <i class="fas fa-divide"></i> | ||
| <span>Деление</span> | ||
| </div> | ||
| <div class="operation-icon" data-operation="clear"> | ||
| <i class="fas fa-broom"></i> | ||
| <span>Очистить</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <button id="calculate" class="calculate-button"> | ||
| <i class="fas fa-equals"></i> Вычислить | ||
| </button> | ||
|
|
||
| <div class="result-container"> | ||
| <span class="result-label">Результат:</span> | ||
| <div id="result" class="result-value">20</div> | ||
| </div> | ||
| </div> | ||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| document.addEventListener('DOMContentLoaded', function() { | ||
| const num1Input = document.getElementById('num1'); | ||
| const num2Input = document.getElementById('num2'); | ||
| const operationSelect = document.getElementById('operation'); | ||
| const calculateButton = document.getElementById('calculate'); | ||
| const resultDiv = document.getElementById('result'); | ||
| const operationIcons = document.querySelectorAll('.operation-icon'); | ||
|
|
||
| operationIcons.forEach(icon => { | ||
| icon.addEventListener('click', function() { | ||
| const operation = this.getAttribute('data-operation'); | ||
|
|
||
| operationIcons.forEach(icon => icon.classList.remove('active')); | ||
| this.classList.add('active'); | ||
|
|
||
| if (operation === 'clear') { | ||
| clearCalculator(); | ||
| this.classList.remove('active'); | ||
| return; | ||
| } | ||
|
|
||
| operationSelect.value = operation; | ||
|
|
||
| if (num1Input.value && num2Input.value) { | ||
| calculate(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| operationSelect.addEventListener('change', function() { | ||
| updateActiveIcon(this.value); | ||
|
|
||
| if (num1Input.value && num2Input.value) { | ||
| calculate(); | ||
| } | ||
| }); | ||
|
|
||
| num1Input.addEventListener('input', function() { | ||
| if (this.value && num2Input.value) { | ||
| calculate(); | ||
| } | ||
| }); | ||
|
|
||
| num2Input.addEventListener('input', function() { | ||
| if (this.value && num1Input.value) { | ||
| calculate(); | ||
| } | ||
| }); | ||
|
|
||
| calculateButton.addEventListener('click', calculate); | ||
|
|
||
| function calculate() { | ||
| const num1 = parseFloat(num1Input.value); | ||
| const num2 = parseFloat(num2Input.value); | ||
| const operation = operationSelect.value; | ||
|
|
||
| if (isNaN(num1) || isNaN(num2)) { | ||
| showError("Введите оба числа"); | ||
| return; | ||
| } | ||
|
|
||
| let result; | ||
| let operationSymbol; | ||
|
|
||
| try { | ||
| switch(operation) { | ||
| case 'add': | ||
| result = num1 + num2; | ||
| operationSymbol = '+'; | ||
| break; | ||
| case 'subtract': | ||
| result = num1 - num2; | ||
| operationSymbol = '-'; | ||
| break; | ||
| case 'multiply': | ||
| result = num1 * num2; | ||
| operationSymbol = '×'; | ||
| break; | ||
| case 'divide': | ||
| if (num2 === 0) { | ||
| throw new Error("Деление на ноль"); | ||
| } | ||
| result = num1 / num2; | ||
| operationSymbol = '÷'; | ||
| break; | ||
| default: | ||
| throw new Error("Неизвестная операция"); | ||
| } | ||
|
|
||
| result = Math.round(result * 10000) / 10000; | ||
|
|
||
| resultDiv.textContent = result; | ||
| resultDiv.classList.remove('error'); | ||
|
|
||
| } catch (error) { | ||
| showError(error.message); | ||
| } | ||
| } | ||
|
|
||
| function showError(message) { | ||
| resultDiv.textContent = "Ошибка: " + message; | ||
| resultDiv.classList.add('error'); | ||
| } | ||
|
|
||
| function clearCalculator() { | ||
| num1Input.value = ''; | ||
| num2Input.value = ''; | ||
| operationSelect.value = 'add'; | ||
| resultDiv.textContent = '0'; | ||
| resultDiv.classList.remove('error'); | ||
| updateActiveIcon('add'); | ||
| } | ||
|
|
||
| function updateActiveIcon(operation) { | ||
| operationIcons.forEach(icon => icon.classList.remove('active')); | ||
|
|
||
| if (operation !== 'clear') { | ||
| const activeIcon = document.querySelector(`.operation-icon[data-operation="${operation}"]`); | ||
| if (activeIcon) { | ||
| activeIcon.classList.add('active'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| updateActiveIcon('add'); | ||
| calculate(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ладно, так как сегодня последний день сдачи лаб + у вас все остальные не в срок сданы, не буду вас заставлять исправлять лабу (хотя тут немало исправить нужно).
Чтобы я зачел лабу, напишите в комментарии, почему тот блок кода, который я выделил (35 - 57 строка) не имеют смысла в существовании
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Потому что это дублирует функционал и не требуется по заданию?