added favicon, changed name

changed Lab-1 to Set Algebra
This commit is contained in:
Shchoholiev 2021-11-26 10:06:17 +02:00
parent 4be3c98c97
commit a51f67ae1c
6 changed files with 48 additions and 732 deletions

View File

@ -1,102 +0,0 @@
<!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">
<title>Boolean Algebra</title>
<link rel="stylesheet" href="../style.css">
</head>
<body>
<div class="container">
<header>
<div class="content">
<h3><a href="">CDM</a></h3>
<div class="theme">
<h3><a href="../Lab-1/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" 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" 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" 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>

View File

@ -1,570 +0,0 @@
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" 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];
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}`);
}

BIN
Set-Algebra/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 795 B

View File

@ -5,8 +5,8 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lab-1</title>
<link rel="icon" type="image/x-icon" href="../Set-Algebra/favicon.png">
<title>Set Algebra Calculator</title>
<link rel="stylesheet" href="../style.css">
@ -18,10 +18,10 @@
<header>
<div class="content">
<h3><a href="">CDM</a></h3>
<h3><a href="../index.html">CDM</a></h3>
<div class="theme">
<h3><a href="">Set Algebra</a></h3>
<h3><a href="">Boolean Algebra</a></h3>
<h3><a href="../Lab-2/index.html">Boolean Algebra</a></h3>
</div>
</div>

View File

@ -1,78 +1,66 @@
<!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">
<title>CDM Utils</title>
<title>Lab-1</title>
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
</head>
<body>
<nav class="navbar is-white">
<div class="navbar-brand">
<a href="index.html" class="navbar-item">
<!-- <img class="mr-2 py-1" src="assets/cuqmbr.png" alt="Cucumber emoji image"> -->
CDM Utils
<!-- <img class="ml-2 py-1" src="assets/Shchoholev-logo.png" alt="Cucumber emoji image"> -->
</a>
<div class="container">
<a class="navbar-burger" id="burger">
<span></span>
<span></span>
<span></span>
</a>
</div>
<div class="navbar-menu" id="nav-links">
<din class="navbar-start">
<a href="index.html" class="navbar-item">
Home
</a>
</din>
<div class="navbar-end">
<div class="navbar-item has-dropdown is-hoverable" id='dropdown'>
<a class="navbar-link">
Introduction. Set Theory
</a>
<div class="navbar-dropdown">
<a href="pages/lab-1.html" class="navbar-item">
Set Algebra
</a>
</div>
<header>
<div class="content">
<h3><a href="">CDM</a></h3>
<div class="theme">
<h3><a href="/Lab-1/index.html">Set Algebra</a></h3>
<h3><a href="/Lab-2/index.html">Boolean Algebra</a></h3>
</div>
</div>
</div>
</nav>
</header>
<div class="columns gapless my-0 has-background-info">
<div class="column is-6-desktop is-12-tablet py-0">
<section class="hero is-fullheight-with-navbar">
<div class="hero-body">
<div class="">
<p class="title">
<span class="icon-text has-text-white">
<span>Hello there</span>
<span class="icon">
<i class="fas fa-handshake ml-5"></i>
</span>
</span>
</p>
<p class="subtitle has-text-white">The web-site is created for people who want to learn<br> more about Computer Discrete Mathematics</p>
<div class="wrapper">
<div class="wrap-side">
<h1>Sets</h1>
<div id="sets">
<div class="input-wrap">
<h1 class="text">Set A = </h1>
<div class="input"><input type="set" onkeydown="return checkInputSet(event.key, this.value)" id="set0" placeholder="1,2,3" /></div>
</div>
<div class="input-wrap">
<h1 class="text">Set B = </h1>
<div class="input"><input type="set" onkeydown="return checkInputSet(event.key, this.value)" id="set1" placeholder="Define set" /></div>
</div>
</div>
</section>
<input class="addSet" type="button" value="Add Set" onclick="AddSet()" />
</div>
<div class="wrap-side">
<h1>Problem</h1>
<h2><input type="problem" id="formula" onkeydown="return checkInputProblem(event.key)" placeholder="!A-C+B/U" /></h2>
<div class="buttons">
<input class="button" type="button" id="calculate" value="Evaluate" onclick="Evaluate(true)" />
<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 sets: A, B, C, and Universal set. Then write the problem you need to solve. </h6>
</div>
</div>
</div>
<script src="js/index.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/js/all.min.js'></script>
</body>
</html>