on website

This commit is contained in:
Shchoholiev 2021-12-02 14:39:07 +02:00
parent 4b163d40d1
commit 163cfe6de0
6 changed files with 703 additions and 3 deletions

BIN
Boolean-Algebra/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 B

102
Boolean-Algebra/index.html Normal file
View File

@ -0,0 +1,102 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="favicon.png">
<title>Boolean Algebra Calculator</title>
<link rel="stylesheet" href="../style.css">
</head>
<body>
<div class="container">
<header>
<div class="content">
<h3><a href="../index.html">CDM</a></h3>
<div class="theme">
<h3><a href="../Set-Algebra/index.html">Set Algebra</a></h3>
<h3><a href="">Boolean Algebra</a></h3>
</div>
</div>
</header>
<div class="wrapper">
<div class="wrap-side">
<h1>Values</h1>
<div id="values">
<div class="input-wrap">
<h1 class="text">Value A = </h1>
<div class="input"><input type="value" id="value0" onkeydown="return checkInputValue(event.key, this.value)" placeholder="e.g. 0 or 1" /></div>
</div>
<div class="input-wrap">
<h1 class="text">Value B = </h1>
<div class="input"><input type="value" id="value1" onkeydown="return checkInputValue(event.key, this.value)" placeholder="e.g. 0 or 1" /></div>
</div>
</div>
<input class="addValue" type="button" value="Add Value" onclick="AddValue()" />
</div>
<div class="wrap-side">
<h1>Formulua</h1>
<h2><input type="problem" id="formula" onkeydown="return checkInputProblem(event.key)" placeholder="e.g. !A*(B+C)" /></h2>
<div class="buttons">
<input class="button" type="button" id="calculate" value="Evaluate" onclick="Evaluate()" />
<input class="button" type="button" id="calculate" value="Step by step" onclick="StepByStep()" />
</div>
<div class="wrap-result">
<div class="input-wrap">
<h1 class="text">Result </h1>
<div class="input"><input type="result" id="result" readonly/></div>
</div>
</div>
<h6 class="description">Define variables then write the formula you need to solve</h6>
<h6 class="operations">
<div class="operation">
<div class="operation-bg"> !</div>
<h6>Negation</h6>
</div>
<div class="operation">
<div class="operation-bg"> *</div>
<h6>Conjunction</h6>
</div>
<div class="operation">
<div class="operation-bg"> +</div>
<h6>Disjunction</h6>
</div>
<div class="operation">
<div class="operation-bg"> ></div>
<h6>Implication</h6>
</div>
<div class="operation">
<div class="operation-bg"> =</div>
<h6>Equivalence</h6>
</div>
</h6>
</div>
</div>
<div class="wrapper hide" id="stepsNode">
<div class="step-by-step" id="stepsWrapper">
<h1>Step by step</h1>
<div class="input-wrap" id="input-wrap">
<h1 class="text">1.</h1>
<div class="input"><input type="result" id="step1" readonly/></div>
</div>
</div>
</div>
</div>
<!-- -----------------------------------JS---------------------------- -->
<script src="script.js"></script>
</body>
</html>

598
Boolean-Algebra/script.js Normal file
View File

@ -0,0 +1,598 @@
let maxNumOfValues = 26;
let currNumOfValues = 2;
function AddValue() {
if (currNumOfValues < 26) {
let charNum = 65 + currNumOfValues;
currNumOfValues++;
let delBtn = document.querySelectorAll('#delBtn');
if (delBtn[delBtn.length - 1]) {
delBtn[delBtn.length - 1].style="display: none;";
}
node = document.getElementById('values');
node.insertAdjacentHTML('beforeend', ` <div class="input-wrap">
<h1 class="text">Value &#${charNum} = </h1>
<div class="input"><input type="value" onkeydown="return checkInputValue(event.key, this.value)" id="value${currNumOfValues - 1}" placeholder="e.g. 0 or 1"/></div>
</div>`);
} else {
alert('You have reached a limint of available values');
}
}
function DeleteValue(valNum) {
let valueField = document.querySelector(`#valueField${valNum}`);
valueField.remove();
currNumOfValues--;
let delBtn = document.querySelectorAll('#delBtn');
if (delBtn[delBtn.length - 1]) {
delBtn[delBtn.length - 1].style="display: inline-block;";
}
}
let VALUES = new Set();
let userValues = new Array();
function FetchValues() {
userValues = new Array();
for (let i = 0; i < currNumOfValues; i++) {
let inputField = document.getElementById(`value${i}`);
let currCharStr = String.fromCharCode(65 + i);
if (inputField.value === '1' || inputField.value === 'true' || inputField.value === 'True'|| inputField.value === 'TRUE' || inputField.value === 't' || inputField.value === 'T') {
VALUES.add(currCharStr);
userValues.push([currCharStr, true]);
} else if (inputField.value === '0' || inputField.value === 'false' || inputField.value === 'False' || inputField.value === 'FALSE' || inputField.value === 'f' || inputField.value === 'F') {
VALUES.add(currCharStr);
userValues.push([currCharStr, false]);
}
}
for (const iterator in VALUES) {
console.log(iterator, VALUES[iterator]);
}
}
let stepByStep = false;
function Evaluate() {
FetchValues();
let formulaField = document.getElementById('formula');
let formulaChraArray = formulaField.value.split('');
let formulaRPNArray = ConvertCharArrayToRPNArray(formulaChraArray);
let formulaValuesRPNArray = ConvertLettersToValues(formulaRPNArray);
let result = SolveRPNFormula(formulaValuesRPNArray);
let readableResult = convertToReadableResult(result[0]);
let resultField = document.getElementById('result');
resultField.value = readableResult;
let stepsNode = document.querySelector('#stepsNode');
let stepsWrapper = document.querySelector('#stepsWrapper');
stepsNode.classList.add('hide');
stepsWrapper.querySelectorAll('#input-wrap').forEach((element) => {
element.remove();
});
if (stepByStep) {
stepsNode.classList.remove('hide');
let stepsActionsArray = StepByStepRPNFormula(formulaRPNArray);
let stepsResultsArray = result[1];
console.log(stepsActionsArray);
console.log(stepsResultsArray);
for (let i = 0; i < stepsActionsArray.length; i++) {
const action = stepsActionsArray[i];
const result = stepsResultsArray[i];
stepsWrapper.insertAdjacentHTML('beforeend', ` <div class="input-wrap" id="input-wrap">
<h1 class="text">${i+1}. </h1>
<div class="input"><input type="result" id="step1" value="${action} = ${result}" readonly/></div>
</div>`);
}
stepByStep = false;
}
}
function StepByStep() {
stepByStep = true;
Evaluate();
}
function Negation(value) {
if (value != undefined) { return !value; }
}
function Conjunction(firstValue, secondValue) {
if (firstValue === true && secondValue === true) {
return true;
} else if (firstValue === false && secondValue === true || firstValue === true && secondValue === false || firstValue === false && secondValue === false) {
return false;
}
}
function Disjunction(firstValue, secondValue) {
if (firstValue === true || secondValue === true) {
return true;
} else if (firstValue === false && secondValue === false) {
return false;
}
}
function Equivalence(firstValue, secondValue) {
if (firstValue === secondValue) {
return true;
} else if (firstValue !== secondValue) {
return false;
}
}
function ConvertCharArrayToRPNArray(chars) {
let _values_stack = new Array();
let _actions_stack = new Array();
for (let i = 0; i < chars.length; i++) {
const element = chars[i];
if (VALUES.has(element)) {
_values_stack.push(element);
} else if (OPERATORS.has(element)) {
while (GetActionPriority(_actions_stack[_actions_stack.length - 1]) >= GetActionPriority(element)) {
let last = _actions_stack.pop();
if (last != '(') {
_values_stack.push(last);
} else {
break;
}
}
if (_actions_stack[0] == undefined || GetActionPriority(_actions_stack[_actions_stack.length - 1]) < GetActionPriority(element)) {
_actions_stack.push(element);
}
} else if (BRACKETS.has(element)) {
if (element == '(') {
_actions_stack.push(element);
}
if (element == ')') {
let _last = _actions_stack.pop();
while (_last != '(') {
_values_stack.push(_last);
_last = _actions_stack.pop();
}
}
} else {
Error('The programm cant solve given formula. Do you typed everything right? Maybe you forgot to define some value?');
}
}
while (_actions_stack[0] != undefined) {
_values_stack.push(_actions_stack.pop());
}
return _values_stack;
}
function ConvertLettersToValues(RPNArray) {
let valuesRNPArray = new Array();
RPNArray.forEach(element => {
if (VALUES.has(element)) {
valuesRNPArray.push(GetValueFromIndex(element));
} else if (element !== '(' || element !== ')') {
valuesRNPArray.push(element);
}
});
return valuesRNPArray;
}
function SolveRPNFormula(valuesRPNArray) {
let stepByStepResults = new Array();
let _stack = new Array();
for (let i = 0; i < valuesRPNArray.length; i++) {
const element = valuesRPNArray[i];
if (OPERATORS.has(element)) {
if (element == '!') {
let _currValue = _stack.pop();
let _result = Negation(_currValue);
_stack.push(_result);
stepByStepResults.push(convertToReadableResult(_result));
} else if (element == '*') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = Conjunction(_firstValue, _secondValue);
_stack.push(_result);
stepByStepResults.push(convertToReadableResult(_result));
} else if (element == '+') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = Disjunction(_firstValue, _secondValue);
_stack.push(_result);
stepByStepResults.push(convertToReadableResult(_result));
} else if (element == '>') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = Disjunction(Negation(_firstValue), _secondValue);
_stack.push(_result);
stepByStepResults.push(convertToReadableResult(_result));
} else if (element == '=') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = Equivalence(_firstValue, _secondValue);
_stack.push(_result);
stepByStepResults.push(convertToReadableResult(_result));
}
} else {
_stack.push(element);
}
}
return [_stack[0], stepByStepResults];
}
function StepByStepRPNFormula(RPNArray) {
let stepsArray = new Array();
let _stack = new Array();
for (let i = 0; i < RPNArray.length; i++) {
const element = RPNArray[i];
if (OPERATORS.has(element)) {
if (element == '!') {
let _currValue = _stack.pop();
let _result;
if (_currValue.length <= 2) {
_result = '!'.concat(_currValue);
} else {
_result = '!'.concat('(').concat(_currValue).concat(')');
}
_stack.push(_result);
stepsArray.push([_result]);
} else if (element == '*') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result;
let _firstHasExclemMark = Boolean(_firstValue.split('').filter(x => x === '!').length);
let _secondHasExclemMark = Boolean(_secondValue.split('').filter(x => x === '!').length);
if (_firstValue.length <= 2 && _secondValue.length <= 2) {
_result = _firstValue.concat('*').concat(_secondValue);
} else if (_firstValue.length <= 2 && _secondValue.length > 2) {
if (_secondHasExclemMark) {
_result = _firstValue.concat('*').concat(_secondValue);
} else {
_result =
_firstValue.concat('*').concat('(').concat(_secondValue).concat(')');
}
} else if (_firstValue.length > 2 && _secondValue.length <= 2) {
if (_firstHasExclemMark) {
_result = _firstValue.concat('*').concat(_secondValue);
} else {
_result = '('.concat(_firstValue).concat(')').concat('*').concat(_secondValue);
}
} else if (_firstValue.length > 2 && _secondValue.length > 2) {
if (_firstHasExclemMark && _secondHasExclemMark) {
_result = _firstValue.concat('*').concat(_secondValue);
} if (_firstHasExclemMark && !_secondHasExclemMark) {
_result = _firstValue.concat('*').concat('(').concat(_secondValue).concat(')');
} else if (!_firstHasExclemMark && _secondHasExclemMark) {
_result = '('.concat(_firstValue).concat(')').concat('*').concat(_secondValue);;
} else {
_result = '('.concat(_firstValue).concat(')').concat('*').concat('(').concat(_secondValue).concat(')');
}
}
_stack.push(_result);
stepsArray.push([_result]);
} else if (element == '+') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = _firstValue.concat('+').concat(_secondValue);
_stack.push(_result);
stepsArray.push([_result]);
} else if (element == '>') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = _firstValue.concat('>').concat(_secondValue);
_stack.push(_result);
stepsArray.push([_result]);
} else if (element == '=') {
let _secondValue = _stack.pop();
let _firstValue = _stack.pop();
let _result = _firstValue.concat('=').concat(_secondValue);
_stack.push(_result);
stepsArray.push([_result]);
}
} else {
_stack.push(element);
}
}
return stepsArray;
// return _stack[0];
}
function InputFormulaFotTruthTable() {
let _formulaString = prompt('>>> ');
if (_formulaString.search('[!,*,+,>,=]') == -1) {
Error('The programm cant solve given formula. Do you typed everything right?');
}
if (_formulaString.match(/[a-z]/g) != null) {
_formulaString.match(/[a-z]/g).forEach (element => {
TTVALUESNAMES.add(element);
})
} else {
Error('The programm cant solve given formula. Do you typed everything right?');
}
TTVALUESNAMES.forEach(element => {
TTVALUESCONTENTS.push([element]);
})
let _numColumns = TTVALUESNAMES.size;
let _numRows = Math.pow(2, _numColumns);
let _rowsContent = new Array();
for (let r = 0; r < _numRows; r++) {
_rowsContent.push(new Array());
for (let c = 0; c < _numColumns; c++) {
_rowsContent[r].push(false);
}
}
for (let c = 0; c < _numColumns; c++) {
let _period = Math.pow(2, _numColumns) / Math.pow(2, c+1);
let _zeros = true;
for (let r = 0; r < _numRows; r++) {
if (_zeros) {
_rowsContent[r][c] = false;
TTVALUESCONTENTS[c].push(false);
}
if (!_zeros) {
_rowsContent[r][c] = true;
TTVALUESCONTENTS[c].push(true);
}
if ((r + 1) % _period == 0) {
_zeros = !_zeros;
}
}
}
// console.log(_rowsContent);
// console.log(TTVALUESCONTENTS);
let _formulaCharArray = ConvertFormulaToCharArray(_formulaString);
let _result = new Array();
for (let i = 0; i < _numRows; i++) {
let _formula_RPN_Array = ConvertCharArrayToRPNArray(_formulaCharArray, i + 1);
_result.push(SolveRPNFormula(_formula_RPN_Array));
}
// console.log(_result);
_result.forEach(element => {
if (element == undefined) {
Error('The programm cant solve given formula. Do you typed everything right?');
}
});
ttformula = _formulaString;
numRows = _numRows;
rowsContent = _rowsContent;
truthTable = _result;
console.log('');
for (let r = 0; r < _numRows; r++) {
console.log(` ${_rowsContent[r].join(', ')} : ${_result[r]}`);
}
console.log('');
ClearTruthTableData();
ReturnToMenu();
}
const OPERATORS = new Set(['!', '*', '+', '>', '=']);
const BRACKETS = new Set(['(', ')']);
function GetActionPriority(action) {
if (action == '!') {
return 5;
} else if (action == '*') {
return 4;
} else if (action == '+' || action == '>' || action == '=') {
return 3;
} else if (action == '(') {
return 2;
} else {
return 0;
}
}
function GetValueFromIndex(valueIndex) {
for (let i = 0; i < userValues.length; i++) {
const element = userValues[i];
if (element[0] == valueIndex) {
return element[1];
}
}
}
function convertToReadableResult(unconverted) {
return unconverted === true ? '1' : '0';
}
function Error(errMsg) {
console.log(` [ERROR] ${errMsg}`);
}
//----------------------------- Check Input ---------------------------
const symbols = ['0', '1'];
const specialSymbols = ['Backspace', 'ArrowLeft', 'ArrowRight', 'Delete'];
function checkInputValue(key, value) {
if (value.length < 1 && symbols.indexOf(key) !== -1) {
return true;
} else if (value.length == 1 && specialSymbols.indexOf(key) !== -1) {
return true;
}
else {
return false;
}
}
const symbols2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Delete',
'>', '=', '+', '*', '!', '(', ')'
];
function checkInputProblem(key) {
return (symbols2.indexOf(key) != -1) ? true : false;
}

View File

@ -21,7 +21,7 @@
<h3><a href="../index.html">CDM</a></h3>
<div class="theme">
<h3><a href="">Set Algebra</a></h3>
<h3><a href="../Lab-2/index.html">Boolean Algebra</a></h3>
<h3><a href="../Boolean-Algebra/index.html">Boolean Algebra</a></h3>
</div>
</div>

BIN
img/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -21,7 +21,7 @@
<h3><a href="">CDM</a></h3>
<div class="theme">
<h3><a href="Set-Algebra/index.html">Set Algebra</a></h3>
<h3><a href="">Boolean Algebra</a></h3>
<h3><a href="Boolean-Algebra/index.html">Boolean Algebra</a></h3>
</div>
</div>
@ -37,7 +37,7 @@
</div>
<div class="wrap-side">
<a href="" class="calculator">
<a href="Boolean-Algebra/index.html" class="calculator">
<img src="img/Boolean-Algebra.png">
<h1>Boolean Algebra Calculator</h1>
</a>