Enter your search terms
Submit search form
my-Tool.com: Be Wise Bitwise - Performs bitwise related tasks such as search boolean expression or show the truth table etc. - Show the Truth
IP Address
MaximaPHP
Regexp Tester
Mathematics
String and Text
Word Domain
Games
Code Tester
Internet Codes
Networking
Date and Time
Be Wise Bitwise
Ranqueen
Q & A
About me
Forum
Open Source Facts
AdManner Free
Classified Ads
Indonesian
Japanese
Truth Seeker
Show the Truth
Show the Truth
Show the truth table for any logical expression.
Show the truth for:
eg. a & b, ~(a | b ^ c), var1 -> var2 ~& var3
Operator as image
Logical Operators
This tool supports several logical operators below, with the highest precedence on top to the lowest in bottom.
Operator
Symbol
Image
Input
Description
NOT
¬
~
Negation
NAND
⊼
~&
Alternative denial
NOR
⊽
~|
Joint denial
AND
⋀
&
Conjunction
XOR
⊻
^
Exclusive disjunction
OR
⋁
|
Disjunction
IF..THEN
⇒
->
Implication
XNOR
⇔
<->
Biconditional
Most of the visitors who used this tool also used
Source
(7.1%),
WHOIS
(7.1%),
Text
(7.1%)
Source code of this tool is:
<?php /****************************************************************** * * ShowTheTruth, Show the truth table for any logical expression. * Copyright (C) 2006, Bowo Prasetyo * * http://www.my-tool.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation below, * * http://www.gnu.org/licenses/gpl.html#TOC1 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * *****************************************************************/ /** * Shows current tool * * @param int $depth how much deep it from index page * @param string $qstring query string of parent tool */ ?> <script src="/shuntingyard.js"> </script> <script src="/showthetruth.js"> </script> <form action="#"> Show the truth for:<br> <input type="text" name="expression" size="47"> <input type="submit" onclick="res = new Object(); res.val = ''; truth = new ShowTheTruth(this.form.expression.value, this.form.opimg.checked); document.getElementsByTagName('div')[0].innerHTML = truth.generateTable(res) + '<br><form name="my_tool_form" action="/bitwise/truth-seeker/" method="post"><input type="hidden" name="seek" value="'+res.val+'"><input type="hidden" name="num" value="'+(Math.log(res.val.length)/Math.log(2))+'"><input type="submit" value=" Bring to Truth Seeker "></form>'; return false"><br> <span class="eg">eg. a & b, ~(a | b ^ c), var1 -> var2 ~& var3</span><br> <input type="checkbox" name="opimg"> Operator as image </form> <br> <div name="result"></div> <br> <b>Logical Operators</b> <br> <br> This tool supports several logical operators below, with the highest precedence on top to the lowest in bottom. <br> <br> <table cellspacing="3" cellpadding="3" style="text-align:center;vertical-align:middle"> <tr><th>Operator</th><th>Symbol</th><th>Image</th><th>Input</th><th>Description</th></tr> <tr><td style="text-align:left">NOT</td><td>¬</td><td><img src="/images/not.gif" border="0"></td><td>~</td><td style="text-align:left">Negation</td></tr> <tr><td style="text-align:left">NAND</td><td>⊼</td><td><img src="/images/nand.gif" border="0"></td><td>~&</td><td style="text-align:left">Alternative denial</td></tr> <tr><td style="text-align:left">NOR</td><td>⊽</td><td><img src="/images/nor.gif" border="0"></td><td>~|</td><td style="text-align:left">Joint denial</td></tr> <tr><td style="text-align:left">AND</td><td>⋀</td><td><img src="/images/and.gif" border="0"></td><td>&</td><td style="text-align:left">Conjunction</td></tr> <tr><td style="text-align:left">XOR</td><td>⊻</td><td><img src="/images/xor.gif" border="0"></td><td>^</td><td style="text-align:left">Exclusive disjunction</td></tr> <tr><td style="text-align:left">OR</td><td>⋁</td><td><img src="/images/or.gif" border="0"></td><td>|</td><td style="text-align:left">Disjunction</td></tr> <tr><td style="text-align:left">IF..THEN</td><td>⇒</td><td><img src="/images/ifthen.gif" border="0"></td><td>-></td><td style="text-align:left">Implication</td></tr> <tr><td style="text-align:left">XNOR</td><td>⇔</td><td><img src="/images/xnor.gif" border="0"></td><td><-></td><td style="text-align:left">Biconditional</td></tr> </table> <br> <?php ?>
Source code of 'shuntingyard.js' is:
<!-- /** * @fileoverview * Contains a class to parse expression using shunting yard algorithm * * <pre> * shuntingyard.js, Parses math equation using shunting yard algorithm * Copyright (C) 2007, Bowo Prasetyo * * http://www.my-tool.com * * This is a javascript implementation of shunting yard algorithm * which is explained in detail at * * http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation below, * * http://www.gnu.org/licenses/gpl.html * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * </pre> */ /** * Creates ShuntingYard object * * @class * A class to parse expression using shunting yard algorithm. * * This class will parse expression containing variables, operators and parentheses. The * result is not calculated as numerical result, instead left in a string * of expression recognized by JavaScript's eval() function. Thus it has * no meaning to parse expression already recognized by eval(). * <br><br> * After parsing the expression, then: * <ul> * <li>{@link #parse parse()} method returns a string containing final * expression that can be evaluated using JavaScript's eval() function * <li>{@link #variables} array stores all detected variables * <li>{@link #op_vals} array stores expression of all operations * <li>{@link #sub} stores a string containing element's subject * title for truth table * <li>{@link #cur_num} stores number of elements in expression * </ul> * <br> * Last four results are especially used by {@link ShowTheTruth} class. * <br><br> * The following operators sorted by its precedence are supported: * <br><br> * <table cellspacing="3" cellpadding="3" style="text-align:center;vertical-align:middle"> * <tr><th>Operator</th><th>Symbol</th><th>Image</th><th>Input</th><th>Description</th></tr> * <tr><td style="text-align:left">NOT</td><td>¬</td><td><img src="/images/not.gif" border="0"></td><td>~</td><td style="text-align:left">Negation</td></tr> * <tr><td style="text-align:left">NAND</td><td>⊼</td><td><img src="/images/nand.gif" border="0"></td><td>~&</td><td style="text-align:left">Alternative denial</td></tr> * <tr><td style="text-align:left">NOR</td><td>⊽</td><td><img src="/images/nor.gif" border="0"></td><td>~|</td><td style="text-align:left">Joint denial</td></tr> * <tr><td style="text-align:left">AND</td><td>⋀</td><td><img src="/images/and.gif" border="0"></td><td>&</td><td style="text-align:left">Conjunction</td></tr> * <tr><td style="text-align:left">XOR</td><td>⊻</td><td><img src="/images/xor.gif" border="0"></td><td>^</td><td style="text-align:left">Exclusive disjunction</td></tr> * <tr><td style="text-align:left">OR</td><td>⋁</td><td><img src="/images/or.gif" border="0"></td><td>|</td><td style="text-align:left">Disjunction</td></tr> * <tr><td style="text-align:left">IF..THEN</td><td>⇒</td><td><img src="/images/ifthen.gif" border="0"></td><td>-></td><td style="text-align:left">Implication</td></tr> * <tr><td style="text-align:left">XNOR</td><td>⇔</td><td><img src="/images/xnor.gif" border="0"></td><td><-></td><td style="text-align:left">Biconditional</td></tr> * </table> * <br> * Usage example: * <code><pre> * parser = new ShuntingYard('a ^ (var1 -> var2)', true); * document.write(parser.parse() + '<br>'); * document.write(parser.op_vals);</pre></code> * * Above example will produce the following output: * <br><br> * (a^(~var1|var2))<br> * ,,(a^(~var1|var2)),,,(~var1|var2) * <br> * @constructor * @param {String} expression expression whose truth table to generate * @param {bool} opimg whether to use image or not for operator * @author Bowo Prasetyo <praNzjOp@SgmaPilA.cMom> (take N O S P A M for email) */ function ShuntingYard(expression, opimg) { /** * End character is empty character * @type String */ this.end = ''; /** * Sentinel character as boundary between operation * @type String */ this.sentinel = '$'; /** * Characters recognized as variable * @type RegExp */ this.terminal = /^[a-zA-Z_][0-9a-zA-Z_]*$/; /** * Operator's internal representations * * 1-length character non-alphanum nor underscore nor sentinel character * @type Array */ this.tokens = new Array( '=', /* biconditional (xnor) */ '>', /* implication (if..then) */ "'", /* alternative denial (nand) */ '!', /* joint denial (nor) */ '|', /* disjunction (or) */ '^', /* exclusive disjunction (xor) */ '&', /* conjunction (and) */ '~' /* negation (not) */ ); /** * Operator precedence * @type Object */ this.precedence = new Object(); this.precedence['$'] = 0; this.precedence['='] = 1; this.precedence['>'] = 2; this.precedence['|'] = 3; this.precedence['^'] = 4; this.precedence['&'] = 5; this.precedence['!'] = 6; this.precedence["'"] = 7; this.precedence['~'] = 8; /** * Binary operators * @type Object */ this.is_binary = new Object(); this.is_binary['='] = 1; this.is_binary['>'] = 1; this.is_binary["'"] = 1; this.is_binary['!'] = 1; this.is_binary['|'] = 1; this.is_binary['^'] = 1; this.is_binary['&'] = 1; /** * Unary operator * @type Object */ this.is_unary = new Object(); this.is_unary['~'] = 1; /** * Current number of elements * @type int */ this.cur_num = 0; /** * Position (index) of current element in expression * @type int */ this.cur_pos = 0; /** * Position (index) of next element in expression * @type int */ this.next_pos = 0; /** * Current token (element) in expression * @type String */ this.cur_tok = ''; /** * Subject title of elements (for truth table) * @type String */ this.sub = ''; /** * Array of all variables * @type Object */ this.variables = new Object(); /** * Array of all operations * @type Array */ this.op_vals = new Array(); /** * Array of operator displaying * @type String */ this.op_inv = new Object(); /** * Replaces operators with internal representations */ var op; var regexp = ''; for (op in ShuntingYard.op_conv) { regexp += op.replace(/(\||\^|\.)/g, '\\$1') + '|'; } regexp = regexp.substring(0, regexp.length-1); expression = expression.replace(new RegExp(regexp, 'g'), function(c){ return ShuntingYard.op_conv[c]; }); /** * Surrounds all tokens with whitespaces */ regexp = ''; for (var i=0; i<this.tokens.length; i++) { regexp += this.tokens[i].replace(/(\||\^)/, '\\$1') + '|'; } regexp += '\\(|\\)'; expression = expression.replace(new RegExp(regexp, 'g'), function(c){ return ' ' + c + ' '; }); /** * Expression to parse * @type String */ this.expression = expression.compress(); /** * Whether to use image or not to display operators * @type bool */ this.opimg = opimg; if (opimg) { this.op_inv['='] = '<img src="/images/xnor.gif" border="0">'; /* biconditional (xnor) */ this.op_inv['>'] = '<img src="/images/ifthen.gif" border="0">'; /* implication (if..then) */ this.op_inv["'"] = '<img src="/images/nand.gif" border="0">'; /* alternative denial (nand) */ this.op_inv['!'] = '<img src="/images/nor.gif" border="0">'; /* joint denial (nor) */ this.op_inv['|'] = '<img src="/images/or.gif" border="0">'; /* disjunction (or) */ this.op_inv['^'] = '<img src="/images/xor.gif" border="0">'; /* exclusive disjunction (xor) */ this.op_inv['&'] = '<img src="/images/and.gif" border="0">'; /* conjunction (and) */ this.op_inv['~'] = '<img src="/images/not.gif" border="0">'; /* negation (not) */ } else { this.op_inv['='] = '⇔'; /* biconditional (xnor) */ this.op_inv['>'] = '⇒'; /* implication (if..then) */ this.op_inv["'"] = '⊼'; /* alternative denial (nand) */ this.op_inv['!'] = '⊽'; /* joint denial (nor) */ this.op_inv['|'] = '⋁'; /* disjunction (or) */ this.op_inv['^'] = '⊻'; /* exclusive disjunction (xor) */ this.op_inv['&'] = '⋀'; /* conjunction (and) */ this.op_inv['~'] = '¬'; /* negation (not) */ } /** * consume() must be called once before parsing */ this.consume(); } /** * Operator external representation if differs */ ShuntingYard.op_conv = new Object(); ShuntingYard.op_conv['<->'] = '='; /* biconditional (xnor) */ ShuntingYard.op_conv['->'] = '>'; /* implication (if..then) */ ShuntingYard.op_conv['~&'] = "'"; /* alternative denial (nand) */ ShuntingYard.op_conv['~|'] = '!'; /* joint denial (nor) */ /** * Parses the expression. * * In technical detail the algorithm used in this class can be explained as below. * <br><br> * Expression's element falls into three types: operator, variable and * parentheses. Internally operator is recognized as 1-length character * of non-alphanum nor underscore, but externally it can be a longer * character sequence translated using array. Variable consists of * alphanum or underscore character but cannot start with number. * <br><br> * First, external representation of operators are replaced by its * internal representation. Then to separate expression into elements, * firstly spaces are inserted around all operators and parentheses. Then * it is tokenized by finding the next space using indexOf() method and * the element can be obtained one by one using subtring() method until * there is no more space left. * <br><br> * The rest of algorithm is written in detail at * <br><br> * <a href="http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm">http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm</a> * * @return final expression ready to eval() * @type String */ ShuntingYard.prototype.parse = function() { var res = ''; var operators = new Array(); var operands = new Array(); operators[operators.length] = new Array(this.sentinel, 0); this.e_func(operators, operands); if (this.expect(this.end)) { res = operands[operands.length-1]; } return res; } /** * Consumes current token and set the next token */ ShuntingYard.prototype.consume = function() { this.cur_num++; this.cur_pos = this.next_pos; this.next_pos = this.expression.indexOf(' ', this.cur_pos + 1); if (this.next_pos < 0) this.next_pos = this.expression.length; this.cur_tok = this.expression.substring(this.cur_pos, this.next_pos).trim(); if (this.cur_tok != this.end) { var tok = this.op_inv[this.cur_tok]; this.sub += '<th style="border-bottom: thin black solid">' + (tok?tok:this.cur_tok) + '</th>'; } } /** * Alerts any errors */ ShuntingYard.prototype.error = function() { window.alert('Syntax error near token ' + this.cur_num + ': ' + this.cur_tok + '.'); } /** * Expects next token to be 'tok' character. * * For example, if current token is '(' (open parentheses) then the ')' * (close parentheses) token is expected some time in the future. * * @param {String} tok the expected token * @return whether the expected token matched or not * @type bool */ ShuntingYard.prototype.expect = function(tok) { if (this.cur_tok==tok) { this.consume(); return true; } else { this.error(); return false; } } /** * Parses tokens of expression in a loop. * * If curent token is valid then does the following actions: * <ul> * <li>for all binary operators, pushes into operators array and checks * the next token * <li>for all sentinels in operators array, pops from operators/operands * array * </ul> * * @param {Array} operators array of operators * @param {Array} operands array of operands */ ShuntingYard.prototype.e_func = function(operators, operands) { if (this.p_func(operators, operands)) { while (this.is_binary[this.cur_tok]) { this.push_operator(this.cur_tok, operators, operands); this.consume(); if (!this.p_func(operators, operands)) break; } while (operators[operators.length-1][0]!=this.sentinel) { this.pop_operator(operators, operands); } } } /** * Checks whether current token is valid or not. * * In case valid token, then does the following actions * <ul> * <li> if token is terminal (variables), adds to operands array * <li> if token is '(' (open parentheses), adds sentinel to operators * array, continues parsing and expects ')' (close parentheses) * <li> if token is unary operators, pushes into operators array and * checks the next token * <li> otherwise raises error * </ul> * * @param {Array} operators array of operators * @param {Array} operands array of operands * @return whether the action succeeded or not * @type bool */ ShuntingYard.prototype.p_func = function(operators, operands) { var res = false; if (this.cur_tok.match(this.terminal)) { this.variables[this.cur_tok] = 1; operands[operands.length] = this.cur_tok; this.consume(); res = true; } else if (this.cur_tok=='(') { this.consume(); operators[operators.length] = new Array(this.sentinel, 0); this.e_func(operators, operands); res = this.expect(')'); operators.length--; } else if (this.is_unary[this.cur_tok]) { this.push_operator(this.cur_tok, operators, operands); this.consume(); res = this.p_func(operators, operands); } else { this.error(); res = false; } return res; } /** * Pops operator/operand(s) from operators/operands arrays. * * If the last operator is binary, calculates last two operands using the * operator, replaces them with the result and removes the operator. If it * is unary, calculates last operands using the operator, replaces it with * the result and removes the operator. * * @param {Array} operators array of operators * @param {Array} operands array of operands */ ShuntingYard.prototype.pop_operator = function(operators, operands) { if (this.is_binary[operators[operators.length-1][0]]) { var t1 = operands[operands.length-1]; operands.length--; var t0 = operands[operands.length-1]; operands.length--; var op = operators[operators.length-1]; operators.length--; operands[operands.length] = this.mk_node_binary(op, t0, t1); } else { var t = operands[operands.length-1]; operands.length--; var op = operators[operators.length-1]; operators.length--; operands[operands.length] = this.mk_node_unary(op, t); } } /** * Pushes an operator into operators array. * * Calculates and pops all operands with operator having precedence higher * than pushed operator, then pushes the operator. * * @param {String} op an operator to push * @param {Array} operators array of operators * @param {Array} operands array of operands */ ShuntingYard.prototype.push_operator = function(op, operators, operands) { while (this.precedence[operators[operators.length-1][0]] > this.precedence[op]) { this.pop_operator(operators, operands); } operators[operators.length] = new Array(op, this.cur_num); } /** * Calculates an operand using unary operator * * @param {Array} op operator * @param {String} t operand to calculate * @return calculation result * @type String */ ShuntingYard.prototype.mk_node_unary = function(op, t) { var res = ''; switch(op[0]) { case '~': /* negation (not) */ res = '(~' + t + ')'; break; } this.op_vals[op[1]] = res; return res; } /** * Calculates two operands using binary operator. * * The following non-javascript operators are replaced by equivalent * javascript operators: * <ul> * <li><code>a ~| b (a ! b) = ~a & ~b</code> joint denial (nor) * <li><code>a ~& b (a ' b) = ~a | ~b</code> alternative denial (nand) * <li><code>a -> b (a > b) = ~a | b</code> implication (if..then) * <li><code>a <-> b (a = b) = a ^ ~b = ~a ^ b</code> biconditional (xnor) * </ul> * * @param {Array} op operator * @param {String} t0 left operand to calculate * @param {String} t1 right operand to calculate * @return calculation result * @type String */ ShuntingYard.prototype.mk_node_binary = function(op, t0, t1) { var res = ''; switch(op[0]) { case '&': /* conjunction (and) */ res = '(' + t0 + '&' + t1 + ')'; break; case '^': /* exclusive disjunction (xor) */ res = '(' + t0 + '^' + t1 + ')'; break; case '|': /* disjunction (or) */ res = '(' + t0 + '|' + t1 + ')'; break; case '!': /* joint denial (nor) */ res = '(~' + t0 + '&~' + t1 + ')'; break; case "'": /* alternative denial (nand) */ res = '(~' + t0 + '|~' + t1 + ')'; break; case '>': /* implication (if..then) */ res = '(~' + t0 + '|' + t1 + ')'; break; case '=': /* biconditional (xnor) */ res = '(~' + t0 + '^' + t1 + ')'; break; } this.op_vals[op[1]] = res; return res; } // -->
Source code of 'showthetruth.js' is:
<!-- /** * @fileoverview * Contains a class to generate truth table for a boolean expression. * * <pre> * showthetruth.js, Generates and shows truth table of boolean expression * Copyright (C) 2007, Bowo Prasetyo * * http://www.my-tool.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation below, * * http://www.gnu.org/licenses/gpl.html * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * </pre> */ /** * Creates ShowTheTruth object * * @class * A class to generate truth table for a boolean expression. * * The following operators sorted by its precedence are supported: * <br><br> * <table cellspacing="3" cellpadding="3" style="text-align:center;vertical-align:middle"> * <tr><th>Operator</th><th>Symbol</th><th>Image</th><th>Input</th><th>Description</th></tr> * <tr><td style="text-align:left">NOT</td><td>¬</td><td><img src="/images/not.gif" border="0"></td><td>~</td><td style="text-align:left">Negation</td></tr> * <tr><td style="text-align:left">NAND</td><td>⊼</td><td><img src="/images/nand.gif" border="0"></td><td>~&</td><td style="text-align:left">Alternative denial</td></tr> * <tr><td style="text-align:left">NOR</td><td>⊽</td><td><img src="/images/nor.gif" border="0"></td><td>~|</td><td style="text-align:left">Joint denial</td></tr> * <tr><td style="text-align:left">AND</td><td>⋀</td><td><img src="/images/and.gif" border="0"></td><td>&</td><td style="text-align:left">Conjunction</td></tr> * <tr><td style="text-align:left">XOR</td><td>⊻</td><td><img src="/images/xor.gif" border="0"></td><td>^</td><td style="text-align:left">Exclusive disjunction</td></tr> * <tr><td style="text-align:left">OR</td><td>⋁</td><td><img src="/images/or.gif" border="0"></td><td>|</td><td style="text-align:left">Disjunction</td></tr> * <tr><td style="text-align:left">IF..THEN</td><td>⇒</td><td><img src="/images/ifthen.gif" border="0"></td><td>-></td><td style="text-align:left">Implication</td></tr> * <tr><td style="text-align:left">XNOR</td><td>⇔</td><td><img src="/images/xnor.gif" border="0"></td><td><-></td><td style="text-align:left">Biconditional</td></tr> * </table> * <br> * Usage example: * <code><pre> * truth = new ShowTheTruth('a & b', true); * var res = new Object(); * document.write(truth.generateTable(res)); * document.write('Value: ' + res.val);</pre></code> * * {@link #generateTable generateTable(res)} will return a string containing the truth table * result. res.val will contain concatenated string of the final value * of input expression in the truth table. * <br><br> * Above example will produce the following output: * <br><br> * <table style="border: thin solid black; text-align: center;" cellpadding="3" cellspacing="0"><tr> * <th style="border-right: thin solid black; border-bottom: thin solid black;">a</th> * <th style="border-right: thin solid black; border-bottom: thin solid black;">b</th> * <th style="border-bottom: thin solid black;">a</th> * <th style="border-bottom: thin solid black;"><img src="/images/and.gif" border="0"></th> * <th style="border-bottom: thin solid black;">b</th> * </tr><tr> * <td style="border-right: thin solid black;">1</td> * <td style="border-right: thin solid black;">1</td> * <td></td><td><b class="bluefont">1</b></td><td></td> * </tr><tr> * <td style="border-right: thin solid black;">1</td> * <td style="border-right: thin solid black;">0</td> * <td></td><td><b class="bluefont">0</b></td><td></td> * </tr><tr> * <td style="border-right: thin solid black;">0</td> * <td style="border-right: thin solid black;">1</td> * <td></td><td><b class="bluefont">0</b></td><td></td> * </tr><tr> * <td style="border-right: thin solid black;">0</td> * <td style="border-right: thin solid black;">0</td> * <td></td><td><b class="bluefont">0</b></td><td></td> * </tr></table> * Value: 1000 * <br> * * @constructor * @param {String} expression expression whose truth table to generate * @param {bool} opimg whether to use image or not for operator * @author Bowo Prasetyo <praNzjOp@SgmaPilA.cMom> (take N O S P A M for email) */ function ShowTheTruth(expression, opimg) { /** * Expression parser with shunting yard algorithm * @type ShuntingYard */ this.parser = new ShuntingYard(expression, opimg); /** * Expression recognized by eval() * @type String */ this.res = this.parser.parse(); /** * Variables with its possible values (0,1) * @type Array */ this.items = new Array(); /** * Truth table's entries for all variables values (0,1) * @type String */ this.varval = ''; /** * Variable's assignment to evaluate expression * @type String */ this.varval2 = ''; /** * Main statement to generate truth table * @type String */ this.stm = ''; /** * All subject headers for the truth table * @type String */ this.sub = ''; /** * All entries for the truth table * @type String */ this.val = ''; } /** * Generates truth table for a boolean expression. * * In technical detail the algorithm used in this method can be explained as below. * <br><br> * First, a ShuntingYard instance called 'this.parser' is created using input * expression. After it is parsed using {@link ShuntingYard#parse parser.parse()}, then: * <ul> * <li>parse() method returns a string containing final * expression that can be evaluated using JavaScript's eval() function * <li>{@link ShuntingYard#variables parser.variables} array stores all detected variables * <li>{@link ShuntingYard#sub parser.sub} stores a string containing element's subject * title for truth table * <li>{@link ShuntingYard#cur_num parser.cur_num} stores number of elements in expression * <li>{@link ShuntingYard#op_vals parser.op_vals} array stores expression of all operations * </ul> * <br> * Next, variable's subject title for truth table ({@link #sub this.sub}), and * (var,(0,1)) array for each variable is constructed ({@link #items this.items}). Then * entries for all variable's value ({@link #varval this.varval}) will be constructed as * follows: * <code> * <pre> * '<td style="border-right: thin black solid">'+this.items[0][1][i[0]]+'</td>' + * ... + * '<td style="border-right: thin black solid">'+this.items[n][1][i[n]]+'</td>'</pre></code> * * Here, n is representing number of elements in expression. * The variable assignment to calculate expression of each operation * ({@link #varval2 this.varval2}) also constructed as * <code><pre> * var val = ''; * val += this.items[0][0] + ' = ' + this.items[0][1][i[0]] + ', '; * val += ... * val += this.items[n][0] + ' = ' + this.items[n][1][i[n]] + ', ';</pre></code> * * Next, the statement to build truth table is built using * {@link #buildStm this.buildStm()} recursively as * <code> * <pre> * for (i[0]=0; i[0] < 2; i[0]++) * { * ... * ... * for (i[n]=0; i[n] < 2; i[n]++) * { * this.val += eval(this.varval); * var val = ''; * val += this.items[0][0] + ' = ' + this.items[0][1][i[0]] + ', '; * val += ... * val += this.items[n][0] + ' = ' + this.items[n][1][i[n]] + ', '; * res.val += eval(val + this.res.replace(/~/g, '1-')); * for (var j=1; j < this.parser.cur_num-1; j++) * val += "'<td>'+" + * (this.parser.op_vals[j]? * (this.parser.op_vals[j]==this.res? * ("'<b class=\"bluefont\">'+" + this.parser.op_vals[j] + "+'</b>'+"): * this.parser.op_vals[j] + "+"):"''+") + "'</td>'+"; * val = eval(val.substring(0,val.length-1).replace(/~/g, '1-')); * if (val) this.val += val + '</tr><tr>'; * } * ... * ... * }</pre></code> * * After eval'ing the statement '{@link #sub this.sub}' will contain subject title * of all variables as well as elements, and '{@link #val this.val}' will contain * their value entries, while 'res.val' will contain concatenated string * of the results. By combining first two variables, the truth table can * be obtained. * * @param {String} res reference to the result of truth table * @return the truth table * @type String */ ShowTheTruth.prototype.generateTable = function(res) { this.sub = ''; this.items = new Array(); var v; for (v in this.parser.variables) { if (v) { this.sub += '<th style="border-right: thin black solid; border-bottom: thin black solid">' + v + '</th>'; this.items[this.items.length] = new Array(v, new Array(1,0)); } } this.varval = ''; this.varval2 = "var val = '';\n"; for (var m=0; m < this.items.length; m++) { this.varval += "'<td style=\"border-right: thin black solid\">'+this.items["+m+"][1][i["+m+"]]+'</td>'+"; this.varval2 += "val += this.items["+m+"][0] + ' = ' + this.items["+m+"][1][i["+m+"]] + ', ';\n"; } this.varval = this.varval.substring(0, this.varval.length-1); this.val = ''; this.stm = ''; var i = new Array(); res.val = ''; this.buildStm(0, this.items.length); eval(this.stm); this.sub += this.parser.sub; var answer = ''; answer += '<table cellspacing="0" cellpadding="3" style="border: thin black solid; text-align:center"><tr>'; answer += this.sub + '</tr><tr>'; answer += this.val + '</tr></table>'; return answer; } /** * Builds statement to generate truth table recursively * * @param {int} n current depth of recursive process * @param {int} num number of variables */ ShowTheTruth.prototype.buildStm = function(n, num) { this.stm += 'for (i['+n+']=0; i['+n+'] < 2; i['+n+']++)\n'; this.stm += '{\n'; if (n < num-1) this.buildStm(n+1, num); else { this.stm += "this.val += eval(this.varval);\n"; this.stm += this.varval2; this.stm += "res.val += eval(val + this.res.replace(/~/g, '1-'));\n"; this.stm += "for (var j=1; j < this.parser.cur_num-1; j++)\n"; this.stm += "val += \"'<td>'+\" + (this.parser.op_vals[j]?(this.parser.op_vals[j]==this.res?(\"'<b class=\\\"bluefont\\\">'+\" + this.parser.op_vals[j] + \"+'</b>'+\"):this.parser.op_vals[j] + \"+\"):\"''+\") + \"'</td>'+\";\n"; this.stm += "val = eval(val.substring(0,val.length-1).replace(/~/g, '1-'));\n"; this.stm += "if (val) this.val += val + '</tr><tr>';\n"; } this.stm += '}\n'; } // -->
©2006 my-Tool.com