検索用語を入力
検索フォームを送信
my-Tool.com: ビーワイズビット - ブール表現を検索したり真理表を表示したりするビット関連のタスクを実行する。 - 真理を示す
IPアドレス
MaximaPHP
正規表現テスタ
数学
ゲーム
ネットワーク
インターネットコード
日付と時間
文字列とテキスト
ワードドメイン
コードテスタ
ビーワイズビット
ランキン
質問と答え
僕について
フォーラム
AdManner
無料広告サイト
インドネシア語
英語
真理を求める
真理を示す
真理を示す
任意の論理式に対してその真理表を表示する。
式の真理を示す:
eg. a & b, ~(a | b ^ c), var1 -> var2 ~& var3
演算子はイメージ
論理演算子
このツールは以下のいくつかの論理演算子をサポートし、高い順位は上にきて下へ行くにつれて低くなる。
演算子
記号
イメージ
入力
説明
NOT
¬
~
否定
NAND
⊼
~&
否定論理積
NOR
⊽
~|
否定論理和
AND
⋀
&
論理積
XOR
⊻
^
排他的論理和
OR
⋁
|
論理和
IF..THEN
⇒
->
含意
XNOR
⇔
<->
排他的論理和の否定
このツールを利用したほとんどのお客様は以下のツールも利用しました。
HTML Tester
(13.3%),
DNS
(13.3%),
ランキン
(13.3%)
このツールのソースコードは以下に示す。
<?php /****************************************************************** * * ShowTheTruth, 任意の論理式に対してその真理表を表示する。 * 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. * * このプログラムはフリーソフトウェアです。あなたはこれを、以下のURLにフリーソフトウェ * ア財団によって発行された GNU 一般公衆利用許諾契約書の定める条件の下で * 再頒布または改変することができます。 * * http://www.gnu.org/licenses/gpl.html#TOC1 * * このプログラムは有用であることを願って頒布されますが、*全くの無保証* * です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含 * め全く存在しません。詳しくはGNU 一般公衆利用許諾契約書をご覧ください。 * * あなたはこのプログラムと共に、GNU 一般公衆利用許諾契約書の複製物を一部 * 受け取ったはずです。もし受け取っていなければ、フリーソフトウェア財団ま * で請求してください(宛先は the Free Software Foundation, Inc., 59 * Temple Place, Suite 330, Boston, MA 02111-1307 USA)。 * *****************************************************************/ /** * 現在のツールを表示する * * @param int $depth インデックスページからどれぐらい深いか * @param string $qstring 親ツールのクエリー文字列 */ ?> <script type="text/javascript"> google_ad_client = "pub-6269511881695171"; google_alternate_ad_url = "http://www.my-tool.com/a120_90.php"; google_ad_width = 728; google_ad_height = 15; google_ad_format = "728x15_0ads_al_s"; google_ad_channel = ""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "A9501B"; google_color_text = "000000"; google_color_url = "008000"; </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><br /><br /> <script src="/shuntingyard-ja.js"> </script> <script src="/showthetruth-ja.js"> </script> <form action="#"> 式の真理を示す:<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="/ja/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=" 真理を求める "></form>'; return false"><br> <span class="eg">eg. a & b, ~(a | b ^ c), var1 -> var2 ~& var3</span><br> <input type="checkbox" name="opimg"> 演算子はイメージ </form> <br> <div name="result"></div> <br> <b>論理演算子</b> <br> <br> このツールは以下のいくつかの論理演算子をサポートし、高い順位は上にきて下へ行くにつれて低くなる。 <br> <br> <table cellspacing="3" cellpadding="3" style="text-align:center;vertical-align:middle"> <tr><th>演算子</th><th>記号</th><th>イメージ</th><th>入力</th><th>説明</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">否定</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">否定論理積</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">否定論理和</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">論理積</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">排他的論理和</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">論理和</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">含意</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">排他的論理和の否定</td></tr> </table> <br> <?php ?>
'shuntingyard-ja.js'のソースコードは以下に示す。
<!-- /** * @fileoverview * Shunting Yardアルゴリズムを用いて表現をパースするクラスを含んでいる * * <pre> * shuntingyard.js, Shunting Yardアルゴリズムを用いて数式をパースする * 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. * * このプログラムはフリーソフトウェアです。あなたはこれを、以下のURLにフリーソフトウェ * ア財団によって発行された GNU 一般公衆利用許諾契約書の定める条件の下で * 再頒布または改変することができます。 * * http://www.gnu.org/licenses/gpl.html * * このプログラムは有用であることを願って頒布されますが、*全くの無保証* * です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含 * め全く存在しません。詳しくはGNU 一般公衆利用許諾契約書をご覧ください。 * * あなたはこのプログラムと共に、GNU 一般公衆利用許諾契約書の複製物を一部 * 受け取ったはずです。もし受け取っていなければ、フリーソフトウェア財団ま * で請求してください(宛先は the Free Software Foundation, Inc., 59 * Temple Place, Suite 330, Boston, MA 02111-1307 USA)。 * </pre> */ /** * ShuntingYardオブジェクトを生成する * * @class * Shunting Yardアルゴリズムを用いて表現をパースするクラス. * * このクラスは変数や演算子や括弧を含む表現をパースする。結果は数値結果 * として計算するのではなく、JavaScriptのeval()関数が認識できる表現の文字列 * のまま残す。したがって、元々eval()が認識するような表現をパースすることは * 意味が無い。 * <br><br> * 表現をパースした後は、 * <ul> * <li>{@link #parse parse()}メソッドはJavaScriptのeval()関数が認識できる最終表現を含む * 文字列を返す * <li>{@link #variables}配列はすべての検出した変数を格納する * <li>{@link #op_vals}配列はすべての演算の表現を格納する * <li>{@link #sub}は真理表の要素のタイトルを含む文字列を格納する * <li>{@link #cur_num}は表現の要素数を格納する * </ul> * <br> 最後の4つの結果は特にクラス{@link ShowTheTruth}によって利用される。 * <br><br> * 以下の優先順位で並べ替えた演算子をサポートする: * <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">否定</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">否定論理積</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">否定論理和</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">論理積</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">排他的論理和</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">論理和</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">含意</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">排他的論理和の否定</td></tr> * </table> * <br> * 使用例: * <code><pre> * parser = new ShuntingYard('a ^ (var1 -> var2)', true); * document.write(parser.parse() + '<br>'); * document.write(parser.op_vals);</pre></code> * * 以上の例はつぎの結果を出力する: * <br><br> * (a^(~var1|var2))<br> * ,,(a^(~var1|var2)),,,(~var1|var2) * <br> * @constructor * @param {String} expression 真理表が生成される表現 * @param {bool} opimg 演算子の表示にイメージを用いるかどうか * @author Bowo Prasetyo <praNzjOp@SgmaPilA.cMom> (take N O S P A M for email) */ function ShuntingYard(expression, opimg) { /** * 終端の文字は空の文字列 * @type String */ this.end = ''; /** * 演算の境としてのセンチネルの文字 * @type String */ this.sentinel = '$'; /** * 変数として認識された文字 * @type RegExp */ this.terminal = /^[a-zA-Z_][0-9a-zA-Z_]*$/; /** * 内部表現の演算子 * * 英数字(文字と数字)やアンダースコアやセンチネル文字以外の長さ1の文字列。 * @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) */ ); /** * 演算子の優先順位 * @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; /** * 二項演算子 * @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; /** * 単項演算し * @type Object */ this.is_unary = new Object(); this.is_unary['~'] = 1; /** * 現在の要素数 * @type int */ this.cur_num = 0; /** * 表現中の現在要素の位置(インデックス) * @type int */ this.cur_pos = 0; /** * 表現中のつぎの要素の位置(インデックス) * @type int */ this.next_pos = 0; /** * 表現中の現在トークン(要素) * @type String */ this.cur_tok = ''; /** * 要素のタイトル(真理表のため) * @type String */ this.sub = ''; /** * すべての変数の配列 * @type Object */ this.variables = new Object(); /** * すべての演算の配列 * @type Array */ this.op_vals = new Array(); /** * 演算子表示の配列 * @type String */ this.op_inv = new Object(); /** * 演算子を内部表現で置換する */ 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]; }); /** * すべてのトークンを空白文字で囲む */ 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 + ' '; }); /** * パースする表現 * @type String */ this.expression = expression.compress(); /** * 演算子の表示にイメージを用いるかどうか * @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()はパースをはじめる前に一回呼ばなければならない */ this.consume(); } /** * 異なる場合は演算子の外部表現 */ 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) */ /** * 表現をパースする. * * 具体的にはこのクラスに使っているアルゴリズムはつぎのように説明できる。 * <br><br> * 表現の要素は三つの種類に分かれていて、すなわち演算し、変数、および括弧。 * 内部的には演算子は英数字(文字と数字)やアンダースコアやセンチネル文字 * 以外の長さ1の文字列として認識されるが、外部的には1よりも長くなることが * できて配列を用いて翻訳される。変数は英数字(文字と数字)やアンダースコア * からなっているが、数字からはじまることができない。 * <br><br> * まずは、演算子の外部表現をその内部表現に置換する。表現を区切るには、 * まずすべての演算子や括弧の周りにスペースを挿入する。そして、indeOf() * メソッドを用いてつぎのスペースを検索し、スペースが無くなるまで、 * substring()メソッドを用いて要素を一個ずつ抽出することができる。 * <br><br> * 残りのアルゴリズムは以下のウェブサイトに詳しく書かれている * <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 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; } /** * 現在のトークンを消費してつぎのトークンを設定する */ 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>'; } } /** * エラーを警告する */ ShuntingYard.prototype.error = function() { window.alert('Syntax error near token ' + this.cur_num + ': ' + this.cur_tok + '.'); } /** * つぎのトークンは「tok」文字であるように期待する. * * たとえば、現在のトークンが「(」(括弧開き)ならば、未来のいつかにおいて * 「)」(括弧閉じ)は期待される。 * * @param {String} tok 期待されるトークン * @return 期待されるトークンがマッチングしたかどうか * @type bool */ ShuntingYard.prototype.expect = function(tok) { if (this.cur_tok==tok) { this.consume(); return true; } else { this.error(); return false; } } /** * ループの中で表現のトークンをパースする. * * 現在のトークンが有効ならば、以下の操作を行う: * <ul> * <li>すべての二項演算子において、演算子の配列に押し込んでつぎのトークン * をチェックする * <li>すべての演算子配列中のセンチネルにおいて、演算子・オペランド配列から * 引き出す * </ul> * * @param {Array} operators 演算子の配列 * @param {Array} 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); } } } /** * 現在のトークンが有効かどうかをチェックする. * * 有効ならば、以下の操作を行う: * <ul> * <li> トークンがターミナル(変数)の場合、オペランド配列に追加する * <li> トークンが「(」(括弧開き)の場合、演算子の配列にセンチネルを追加 * して、パースを続けて、「)」(括弧閉じ)を期待する * <li> トークンが単項演算しの場合、演算子の配列に押し込み、つぎのトークンをチェックする * <li> その他、エラーを出す * </ul> * * @param {Array} operators 演算子の配列 * @param {Array} operands オペランドの配列 * @return 操作が成功したかどうか * @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; } /** * 演算子・オペランドを配列から引き出す * * 最後の演算子は二項の場合、演算子を用いて最後の二つのオペランドを計算し、 * それらのところに計算の結果で置き換え、演算子を削除する。単項の場合は、 * 演算子を用いて最後のオペランドを計算し、そのところに計算の結果で置き換え、 * 演算子を削除する。 * * @param {Array} operators 演算子の配列 * @param {Array} 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); } } /** * 演算子を演算子の配列に押し込む. * * 押し込む演算子よりも高い優先順位を持つオペランドをすべて計算して引き出し、 * その後は演算子を押し込む。 * * @param {String} op 押し込む演算子 * @param {Array} operators 演算子の配列 * @param {Array} 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); } /** * 単項演算子でオペランドを計算する * * @param {Array} op 演算子 * @param {String} t 計算するオペランド * @return 計算結果 * @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; } /** * 二項演算子で二つのオペランドを計算する. * * つぎの非JavaScriptの演算子は同等なJavaScript演算子に置き換える: * <ul> * <li><code>a ~| b (a ! b) = ~a & ~b</code> 否定論理和(nor) * <li><code>a ~& b (a ' b) = ~a | ~b</code> 否定論理積(nand) * <li><code>a -> b (a > b) = ~a | b</code> 含意(if..then) * <li><code>a <-> b (a = b) = a ^ ~b = ~a ^ b</code> 排他的論理和の否定(xnor) * </ul> * * @param {Array} op 演算子 * @param {String} t0 計算する左オペランド * @param {String} t1 計算する右オペランド * @return 計算結果 * @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; } // -->
'showthetruth-ja.js'のソースコードは以下に示す。
<!-- /** * @fileoverview * ブール表現の真理表を生成するクラスを含んでいる. * * <pre> * showthetruth.js, ブール表現の真理表を生成して表示する * 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. * * このプログラムはフリーソフトウェアです。あなたはこれを、以下のURLにフリーソフトウェ * ア財団によって発行された GNU 一般公衆利用許諾契約書の定める条件の下で * 再頒布または改変することができます。 * * http://www.gnu.org/licenses/gpl.html * * このプログラムは有用であることを願って頒布されますが、*全くの無保証* * です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含 * め全く存在しません。詳しくはGNU 一般公衆利用許諾契約書をご覧ください。 * * あなたはこのプログラムと共に、GNU 一般公衆利用許諾契約書の複製物を一部 * 受け取ったはずです。もし受け取っていなければ、フリーソフトウェア財団ま * で請求してください(宛先は the Free Software Foundation, Inc., 59 * Temple Place, Suite 330, Boston, MA 02111-1307 USA)。 * </pre> */ /** * ShowTheTruthオブジェクトを生成する * * @class * ブール表現の真理表を生成するクラス. * * 以下の優先順位で並べ替えた演算子をサポートする: * <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">否定</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">否定論理積</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">否定論理和</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">論理積</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">排他的論理和</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">論理和</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">含意</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">排他的論理和の否定</td></tr> * </table> * <br> * 使用例: * <code><pre> * truth = new ShowTheTruth('a & b', true); * var res = new Object(); * document.write(truth.generateTable(res)); * document.write('値: ' + res.val);</pre></code> * * {@link #generateTable generateTable(res)}は真理表の結果を含む文字列を返す。 * res.valは入力の表現に対して真理表の最終値を連結した文字列を含んでいる。 * <br><br> * 以上の例はつぎの出力をする: * <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> * 値: 1000 * <br> * * @constructor * @param {String} expression 真理表が生成されるブール表現 * @param {bool} opimg 演算子の表示にイメージを用いるかどうか * @author Bowo Prasetyo <praNzjOp@SgmaPilA.cMom> (take N O S P A M for email) */ function ShowTheTruth(expression, opimg) { /** * Shunting Yardアルゴリズムを用いる表現のパーサ * @type ShuntingYard */ this.parser = new ShuntingYard(expression, opimg); /** * eval()が認識できる表現 * @type String */ this.res = this.parser.parse(); /** * 可能な値(0,1)を含む変数 * @type Array */ this.items = new Array(); /** * すべての変数の値(0,1)の真理表のエントリ * @type String */ this.varval = ''; /** * 表現を評価するために変数の代入 * @type String */ this.varval2 = ''; /** * 真理表を生成する主ステートメント * @type String */ this.stm = ''; /** * 真理表のすべてのタイトル * @type String */ this.sub = ''; /** * 真理表のすべてのエントリ * @type String */ this.val = ''; } /** * ブール表現に対して真理表を生成する. * * 具体的にはこのメソッドに用いるアルゴリズムはつぎのように説明できる。 * <br><br> * まずは、'this.parser'というShuntingYardのインスタンスは入力した表現を * 使って生成する。そして、{@link ShuntingYard#parse parser.parse()}を * 用いてパースした後は、 * <ul> * <li>parse() メソッドはJavaScriptのeval()関数が認識できる最終表現を含む * 文字列を返す * <li>{@link ShuntingYard#variables parser.variables}配列はすべての検出した変数を格納する * <li>{@link ShuntingYard#sub parser.sub}は真理表の要素のタイトルを含む文字列を格納する * <li>{@link ShuntingYard#cur_num parser.cur_num}は表現の要素数を格納する * <li>{@link ShuntingYard#op_vals parser.op_vals}配列はすべての演算の表現を格納する * </ul> * <br> * つぎは、真理表の変数のタイトル({@link #sub this.sub})および各変数の * (var,(0,1))配列({@link #items this.items})を構築する。それから、すべての * 変数の値のエントリ({@link #varval this.varval})は以下のように構築する: * <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> * * ここで、n は表現の要素数をあらわしている。各演算の表現を計算するために * 変数の代入も以下のように構築する: * <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> * * つぎは、真理表を構築するためのステートメントは{@link #buildStm this.buildStm()}を用いて以下のように再帰的に構築する: * <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> * * ステートメントをeval()した後は、「{@link #sub this.sub}」はすべての変数や * 要素のタイトルを含んでおり、「{@link #val this.val}」はそれらの値の * エントリを含み、そして「res.val」は結果を連結した文字列を含んでいる。 * 最初の二つの変数を組み合わせることで真理表を得ることができる。 * * @param {String} res 真理表の結果への参照 * @return 真理表 * @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; } /** * 真理表を生成するステートメントを再帰的に構築する * * @param {int} n 再帰的なプロセスの現在の深さ * @param {int} num 変数の数 */ 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