input check, tests and fixes

This commit is contained in:
Shchoholiev 2021-11-24 20:48:56 +02:00
parent b15633988f
commit 4be3c98c97
2 changed files with 62 additions and 29 deletions

View File

@ -34,11 +34,11 @@
<div id="sets"> <div id="sets">
<div class="input-wrap"> <div class="input-wrap">
<h1 class="text">Set A = </h1> <h1 class="text">Set A = </h1>
<div class="input"><input type="set" id="set0" placeholder="1,2,3" /></div> <div class="input"><input type="set" onkeydown="return checkInputSet(event.key, this.value)" id="set0" placeholder="1,2,3" /></div>
</div> </div>
<div class="input-wrap"> <div class="input-wrap">
<h1 class="text">Set B = </h1> <h1 class="text">Set B = </h1>
<div class="input"><input type="set" id="set1" placeholder="Define set" /></div> <div class="input"><input type="set" onkeydown="return checkInputSet(event.key, this.value)" id="set1" placeholder="Define set" /></div>
</div> </div>
</div> </div>
<input class="addSet" type="button" value="Add Set" onclick="AddSet()" /> <input class="addSet" type="button" value="Add Set" onclick="AddSet()" />
@ -46,9 +46,9 @@
<div class="wrap-side"> <div class="wrap-side">
<h1>Problem</h1> <h1>Problem</h1>
<h2><input type="problem" id="formula" placeholder="!A-C+B/U" /></h2> <h2><input type="problem" id="formula" onkeydown="return checkInputProblem(event.key)" placeholder="!A-C+B/U" /></h2>
<div class="buttons"> <div class="buttons">
<input class="button" type="button" id="calculate" value="Evaluate" onclick="Evaluate()" /> <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()" /> <input class="button" type="button" id="calculate" value="Step by step" onclick="stepByStep()" />
</div> </div>
<div class="wrap-result"> <div class="wrap-result">

View File

@ -11,7 +11,7 @@ function AddSet() {
node = document.getElementById('sets'); node = document.getElementById('sets');
node.insertAdjacentHTML('beforeend', ` <div class="input-wrap"> node.insertAdjacentHTML('beforeend', ` <div class="input-wrap">
<h1 class="text">Set &#${charNum} = </h1> <h1 class="text">Set &#${charNum} = </h1>
<div class="input"><input type="set" id="set${charNum - 65}" placeholder="Define set"/></div> <div class="input"><input type="set" onkeydown="return checkInputSet(event.key, this.value)" id="set${charNum - 65}" placeholder="Define set"/></div>
</div>`); </div>`);
} else { } else {
@ -22,12 +22,7 @@ function AddSet() {
let universalSet = new Set(); let universalSet = new Set();
function Complement(set) { function Complement(set) {
return (checkEmpty(set)) ? universalSet : Difference(universalSet, set);
let _complement = new Set();
_complement = Difference(universalSet, set);
return _complement;
} }
function Intersection(set1, set2) { function Intersection(set1, set2) {
@ -40,7 +35,7 @@ function Intersection(set1, set2) {
} }
}); });
return _intersection; return sortSet(_intersection);
} }
function Union(set1, set2) { function Union(set1, set2) {
@ -51,7 +46,7 @@ function Union(set1, set2) {
_union.add(element); _union.add(element);
}); });
return _union; return sortSet(_union);
} }
function Difference(set1, set2) { function Difference(set1, set2) {
@ -65,9 +60,20 @@ function Difference(set1, set2) {
} }
}); });
return _difference; return sortSet(_difference);
} }
function sortSet(set) {
let entries = [];
for (let member of set) {
entries.push(+member);
}
set.clear();
for (let entry of entries.sort((a, b) => a - b)) {
set.add(entry);
}
return set;
};
const OPERATORS = new Set(['~', '!', '∩', '/', '', '+', '-']); const OPERATORS = new Set(['~', '!', '∩', '/', '', '+', '-']);
const BRACKETS = new Set(['(', ')']); const BRACKETS = new Set(['(', ')']);
@ -75,7 +81,11 @@ const BRACKETS = new Set(['(', ')']);
let SETSNAMES = new Set(); let SETSNAMES = new Set();
let SETS = new Array(); let SETS = new Array();
function Evaluate() { function Evaluate(hide = false) {
if (hide == true) {
let stepByStep = document.getElementById('stepByStep');
stepByStep.classList.add('hide');
}
FetchSets(); FetchSets();
@ -200,7 +210,6 @@ function SolveRPNFormula(RPN_Array) {
stack.push(result); stack.push(result);
} }
} else { } else {
stack.push(element); stack.push(element);
} }
} }
@ -231,13 +240,13 @@ function FetchSets() {
}); });
SETSNAMES.add(String.fromCharCode(65 + i)); SETSNAMES.add(String.fromCharCode(65 + i));
SETS.push(newSet); SETS.push(sortSet(newSet));
} else { } else {
SETS.push(new Set()); SETS.push(new Set());
} }
} }
universalSet = new Set(universalArray); universalSet = sortSet(new Set(universalArray));
} }
@ -266,13 +275,11 @@ function GetActionPriority(action) {
} }
function ConvertToReadableResult(unconverted) { function ConvertToReadableResult(unconverted) {
let converted; return (checkEmpty(unconverted)) ? "Empty Set" : Array.from(unconverted).sort((a, b) => a - b).join(', ');
if (unconverted == undefined || unconverted.size == 0) { }
converted = "Empty Set";
} else { function checkEmpty(set) {
converted = Array.from(unconverted).sort().join(', '); return (set == undefined || set.size == 0) ? true : false;
}
return converted;
} }
//----------------------------- Step by Step --------------------------- //----------------------------- Step by Step ---------------------------
@ -295,8 +302,7 @@ let step = 0;
function printStep(operation, firstSet, secondSet, result) { function printStep(operation, firstSet, secondSet, result) {
step++; step++;
str = ''; let str = '';
switch (operation) { switch (operation) {
case '!': case '!':
str = `!${setToString(firstSet)} = ${setToString(result)}`; str = `!${setToString(firstSet)} = ${setToString(result)}`;
@ -327,12 +333,39 @@ function printStep(operation, firstSet, secondSet, result) {
function setToString(set) { function setToString(set) {
let str = ''; let str = '';
if (set == undefined || set.size == 0) { if (checkEmpty(set)) {
return '{ Empty Set }' return '{ Empty Set }'
} else { } else {
for (let num of set) { for (let num of set) {
str += ' ,' + num; str += ', ' + num;
} }
} }
return '{ ' + str.slice(2, str.length) + ' }'; return '{ ' + str.slice(2, str.length) + ' }';
}
//----------------------------- Check Input ---------------------------
const symbols = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Delete'];
function checkInputSet(key, value) {
if (value[value.length - 1] == ',' && symbols.indexOf(key) !== -1) {
return true;
} else if (symbols.indexOf(value[value.length - 1]) !== -1 && key == ',') {
return true;
} else if (symbols.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;
} }