Skip to content
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

Практическая работа 10.7 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 227 additions & 23 deletions bjs/07_Number_and_string/index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,236 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<head>
<meta charset="UTF-8" />
<title>Калькулятор</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div class="container">
<div class="row calc-row align-items-center">
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"
/>
<link
href="https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="container">
<div class="row calc-row align-items-center">
<div class="col col-md-4 offset-md-4">
<div class="form-group">
<div class="card" id="calc">
<div class="card-body">
<input class="form-control input-window" id="inputWindow" value="">
<div class="row no-gutters">
<div class="col">
<button class="btn btn-outline-danger form-control" id="btn_clr">C</button>
</div>
</div>
</div>
<div class="form-group">
<div class="card" id="calc">
<div class="card-body">
<input
class="form-control input-window"
id="inputWindow"
value=""
/>
<select
class="custom-select form-control form-control-sm"
id="history_selector"
>
<option></option>
</select>
<div class="row no-gutters">
<div class="col">
<button
class="btn btn-outline-danger form-control"
data-action="clr"
id="btn_clr"
>
AC
</button>
</div>

<div class="col">
<button
class="btn btn-outline-danger form-control"
data-action="clr_last"
id="btn_clr_last"
>
C
</button>
</div>

<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="sign"
id="btn_sign"
>
&PlusMinus;
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="divide"
id="btn_divide"
>
&divide;
</button>
</div>
</div>
<div class="row no-gutters">
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="1"
id="btn_1"
>
1
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="2"
id="btn_2"
>
2
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="3"
id="btn_3"
>
3
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="multi"
id="btn_multi"
>
*
</button>
</div>
</div>
<div class="row no-gutters">
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="4"
id="btn_4"
>
4
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="5"
id="btn_5"
>
5
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="6"
id="btn_6"
>
6
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="minus"
id="btn_minus"
>
-
</button>
</div>
</div>
<div class="row no-gutters">
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="7"
id="btn_7"
>
7
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="8"
id="btn_8"
>
8
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="9"
id="btn_9"
>
9
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="sum"
id="btn_sum"
>
+
</button>
</div>
</div>
<div class="row no-gutters">
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="0"
id="btn_0"
>
0
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="point"
id="btn_point"
>
.
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="sqrt"
id="btn_sqrt"
>
&Sqrt;
</button>
</div>
<div class="col">
<button
class="btn btn-outline-primary form-control"
data-action="equal"
id="btn_equal"
>
=
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
<script src="script.js"></script>
</body>
</html>
128 changes: 120 additions & 8 deletions bjs/07_Number_and_string/script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,124 @@
let lastOperand = 0;
let operation = null;
let operations = { sum: "+", minus: "-", multi: "*", divide: "/" };
let history = [];
let allHistory = [];
const PRECISION = 2;

const inputWindow = document.getElementById('inputWindow');
const inputWindow = document.getElementById("inputWindow");
const inputHistory = document.getElementById("history");
const selectHistory = document.getElementById("history_selector");

renderCalc();

document.getElementById('btn_clr').addEventListener('click', function () {
lastOperand = 0;
operation = null;
inputWindow.value = '';
})
/* Очистка калькулятора */
document.getElementById("btn_clr").addEventListener("click", function () {
history = [];
renderCalc();
});

/* Перерисовка калькулятора */
function renderCalc() {
inputWindow.value = history.length > 0 ? history[history.length - 1] : "0";
selectHistory.options[0].text = history.length > 0 ? history.join("") : "0";
}

/* Проверка, что последней была введена операция */
function isLastOperation() {
if (Object.values(operations).indexOf(history[history.length - 1]) >= 0)
return true;
else return false;
}

/* отмена последнего ввода */
document.getElementById("btn_clr_last").addEventListener("click", function () {
let isLast = isLastOperation();
if (history.length) {
if (!isLast) {
let item = history.pop();
history.push(item.slice(0, item.length - 1));
} else {
history.pop();
}
}
renderCalc();
});

/* Нажатие клавиши с операцией */
Object.keys(operations).forEach((oper) => {
document.getElementById(`btn_${oper}`).addEventListener("click", function () {
if (isLastOperation()) history.pop();
history.push(operations[oper]);
renderCalc();
});
});

/* Квадратный корень */

document.getElementById("btn_sqrt").addEventListener("click", function () {
if (history.length) {
let isLast = isLastOperation();
if (!isLast) {
let item = history.pop();
history.push(Math.sqrt(parseFloat(item)).toPrecision(PRECISION));
}
}
renderCalc();
});

/* Нажатие цифры на клавиатуре - запись числа в массив history */
for (let i = 0; i < 10; i++) {
document.querySelector(`#btn_${i}`).addEventListener("click", function (evt) {
if (!history.length) history.push(i.toString());
else if (isLastOperation()) history.push(i.toString());
else history.push(history.pop() + i.toString());
renderCalc();
});
}

/* Нажатие точки */
document.getElementById("btn_point").addEventListener("click", function () {
if (!isLastOperation() && !history[history.length - 1].match(/\./)) {
history.push(history.pop() + ".");
}
renderCalc();
});

/* Изменение знака */
document.getElementById("btn_sign").addEventListener("click", function () {
if (!isLastOperation() && history.length) {
history.push((-1 * history.pop()).toString());
}
renderCalc();
});

/* Вычисление результата */
document.getElementById("btn_equal").addEventListener("click", function () {
let result = history.reduce((result, current, index, array) => {
if (index === 0) result = parseFloat(current);
let currentIndex = Object.values(operations).indexOf(current);
if (currentIndex >= 0) {
let nextItem = parseFloat(array[index + 1]);
switch (Object.keys(operations)[currentIndex]) {
case "sum":
result += nextItem;
break;
case "minus":
result -= nextItem;
break;
case "multi":
result *= nextItem;
break;
case "divide":
result /= nextItem;
break;
}
}
return result;
}, 0);
history.push("=" + result.toFixed(PRECISION));
allHistory.push(history);
let newOption = new Option(history.join(""));
selectHistory.append(newOption);
history = [];
history.push(result.toFixed(PRECISION));
renderCalc();
});