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

Дз4 #5

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
42 changes: 42 additions & 0 deletions Thing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="calc.css" rel="stylesheet" type="text/css" />

<title>Калькулятор</title>
</head>

<body>
<p>Для сброса пока что перезапускаем страницу.</p>
<table>
<tr><th colspan="4" id="result">0</th></tr>
<tr>
<td class="number" id="7">7</td>
<td class="number" id="8">8</td>
<td class="number" id="9">9</td>
<td class="operation" id="add">+</td>
</tr>
<tr>
<td class="number" id="4">4</td>
<td class="number" id="5">5</td>
<td class="number" id="6">6</td>
<td class="operation" id="sub">-</td>
</tr>
<tr>
<td class="number" id="1">1</td>
<td class="number" id="2">2</td>
<td class="number" id="3">3</td>
<td class="operation" id="division">/</td>
</tr>
<tr>
<td class="number" colspan="3" id="0">0</td>
<td class="operation" id="mul">*</td>
</tr>
<tr>
<td class="operation" id="enter" colspan="4">Посчитать</td>
</tr>
</table>
<script src="jquery-1.8.2.min.js"></script>
<script src="calc.js"></script>
</html>
10 changes: 10 additions & 0 deletions calc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
table{ border:thin #000 solid;}
td, th {
border: #009 solid;
font-size: 2em;
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
color: #0C3;
width: 60px;
height: 30px;
vertical-align: middle;
text-align: center;}
104 changes: 104 additions & 0 deletions calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// JavaScript Document

var stack = {};
stack['result'] = 0; //Почему результат становится NaN?
stack['elements'] = ['0','+'];
stack['get_result'] = function(){
return stack['result'];
};
var numberFlag = false //Дописываем цифры если тру, начинаем писать заново если фалс
function getOperation(){
/*
Выполняет операцию для последних введёных чисел
*/
var l = stack['elements'][stack['elements'].length];
var operation = stack['elements'][l-1] ;
if (operation = 'add'){
stack['result'] = parseInt(stack['elements'][l-2]) + parseInt(stack['elements'][l])
}
else if (operation = 'sub') {
stack['result'] = parseInt(stack['elements'][l-2]) - parseInt(stack['elements'][l])
}
else if (operation = 'mul') {
stack['result'] = parseInt(stack['elements'][l-2]) * parseInt(stack['elements'][l])
}
else if (operation = 'division'){
stack['result'] = parseInt(stack['elements'][l-2]) / parseInt(stack['elements'][l])
}
else {
// Ситуация, когда ввод нажали сразу после начала работы приложения
}
stack['elements'].pop();
stack['elements'].pop();
stack['elements'].pop();
stack['elements'].push(stack['result']);
};
function forNumber(element){
/*
Ставит число в стек на обработку
*/
if ((stack['elements'][stack['elements'].length] == 'add') || (stack['elements'][stack['elements'].length] == 'mul') ||
(stack['elements'][stack['elements'].length] == 'sub') || (stack['elements'][stack['elements'].length] == 'division'))
{
stack['elements'].push(element);
}
else {
stack['elements'][stack['elements'].length] += element;
};
console.log(stack['elements']);
numberFlag = true;
};

function forOperation(operation){
/*
Добавляет оператор в очередь на обработку
*/
if ((stack['elements'][stack['elements'].length] == 'add') || (stack['elements'][stack['elements'].length] == 'mul') ||
(stack['elements'][stack['elements'].length] == 'sub') || (stack['elements'][stack['elements'].length] == 'division'))
{
stack['elements'][stack['elements'].length] = operation;
}
else {
getOperation();
stack['elements'].push(operation);
}
numberFlag = false;
};
function forResult(){
/*
Обрабатывает нажатие на 'Посчитать'
*/
getOperation();
};

$('.number').each(function(){
var current = this;
this.onclick = function(event){
var value = event.target.id;
if (numberFlag == true){
$('#result').text(document.getElementById('result').innerHTML+value);
}
else{
document.getElementById('result').innerHTML='';
$('#result').text(value);
}
forNumber(value) // Почему значение undefined+value?

};
})
$('.operation').each(function(){
var current = this;
this.onclick = function(event){
var value = event.target.id;
forOperation(value) // Почему значение undefined+value?
$('#result').text(stack.result);

};
})
$('#enter').each(function(){
var current = this;
this.onclick = function(event){
forResult() // Почему значение undefined+value?
$('#result').text(stack['result']);
};
})
2 changes: 2 additions & 0 deletions jquery-1.8.2.min.js

Large diffs are not rendered by default.