<?php
$page_name = "Boolean Calculator V1";
$menu_type = "spare";
$menu_name = "Boolean Calc";
$blog_num = 88;
$page_rate = 7;
$page_tags = array("erudition");
$page_date = "May 10, 2012";
include_once("./files/head.php");


/*
//#
//#####     FORMAT EQUATION     #####
//#

$num_var = $_POST['num_var'];
$equation = $_POST['equation'];
$steps = array();

//uppercase everything
$equ_work = strtoupper($equation);
//remove all spaces
$equ_work = str_replace(" ", null, $equ_work);
//convert to an array
$equ_work = str_split($equ_work);

//step 1
$equ_work = bool_make_nots($equ_work);
$steps[] = record_step($equ_work);

//step 2
$equ_work = bool_exp_brack($equ_work);
$steps[] = record_step($equ_work);

//step 3
$equ_groups = bool_make_groups($equ_work);
$steps[] = record_step($equ_groups);

//step 4
$equ_groups = bool_group_sort($equ_groups);
$equ_groups = bool_cancel_opps($equ_groups);
$steps[] = record_step($equ_groups);

//step 5
$equ_groups = bool_cancel_groups($equ_groups);
$steps[] = record_step($equ_groups);

//step 6
$equ_groups = bool_reduce_groups($equ_groups);
$steps[] = record_step($equ_groups);

//step 7
$equ_truth = truth_fill_vars($equ_groups);
$steps[] = record_step($equ_truth);

//step 8
$equ_truth = bool_con_var($equ_truth);
$steps[] = record_step($equ_truth);



//#
//#####    CREATE TRUTH TABLE    #####
//#
//headers
$truth_head = array("A", "B", "C", "D", "E", "F");

//generate truth table size, binary representation, and if group is true
$table_size = pow(2, $num_var);
for ($i=0; $i<$table_size; $i++) {
  $truth_body[$i][0] = str_pad(decbin($i), $num_var, 0, STR_PAD_LEFT);
  $truth_body[$i][1] = 0;
  foreach($equ_truth as $value) {
    if ($value == $truth_body[$i][0]) { $truth_body[$i][1] = 1;}
  }
}



//#
//#####    CREATE KARNAUGH MAP    #####
//#
$map_pattern = array(
  array("1"),
  array("0", "1"), //1 var
  array("00", "01", "11", "10"), //2 var
  array("000", "001", "011", "010", "110", "111", "101", "100") //3 var
  );
$map_vars = array(
  array("0"),
  array("0", "1"), //1 var
  array(0, 2, 1, 3), //2 var
  array(0, 2, 6, 4, 1, 3, 7, 5), //3 var
  array(0, 4, 12, 8, 1, 5, 13, 9, 3, 7, 15, 11, 2, 6, 14, 10), //4 var
  array(0, 4, 12, 8, 24, 28, 20, 16, 1, 5, 13, 9, 25, 29, 21, 17, 3, 7, 15, 11, 27, 31, 23, 19, 2, 6, 14, 10, 26, 30, 22, 18), //5 var
);
//create both axis
$x_num = ceil($num_var/2);
$y_num = floor($num_var/2);
$x_axis = pow(2, $x_num);
$y_axis = pow(2, $y_num);
$x_half = $x_axis/2;
$y_half = $y_axis/2;
$x_head = $map_pattern[$x_num];
$y_head = $map_pattern[$y_num];
for ($i=0; $i<$x_num; $i++) { $x_title .= $truth_head[$i]; }  // x_title
for ($i=0; $i<$y_num; $i++) { $y_title .= $truth_head[$i + $x_num]; }  // y_title
$map_vars_disp = $map_vars[$num_var];
foreach($map_vars_disp as $value) {
  $equ_map[] = $truth_body[$value][1];
}










//#
//#####    BODY - CALCULATOR   #####
//#

echo "<p>Online boolean calculators seem to be few and far between.  So here is one I made.</p>\n";
echo "<ul>\n";
  echo "<li>Please use brackets around each term, it's painful, but this is a beta after all - <span class=\"green\">X = (A'+B)(AC) + (CC)+ (AC)</span></li>\n";
  echo "<li>Use an apostrophy ( ' ) for 'not' (can handle double nots too)- <span class=\"green\">X = (A' + B'' + C''')</span></li>\n";
  //echo "<li>You can 'not' a whole term- <span class=\"green\">X = (A'B + C)' + (AC')</span></li>\n";
  echo "<li>Two variables together is an 'and' - <span class=\"green\">X = (AB)</span></li>\n";
  echo "<li>A plus ( + ) is an 'or' - <span class=\"green\">X = (A'B) + (C)</span></li>\n";
  echo "<li>Will simplify and'd brackets - <span class=\"green\">X = <span class=\"pink\">(A'+B)(AC)</span> + (CC)+ (AC) = <span class=\"pink\">(A'AC + ABC)</span> + (CC) + (AC)</span></li>\n";
  echo "<li>Will simplify redundant variables - <span class=\"green\">X = (A'AC + ABC) + (<span class=\"pink\">CC</span>)+ (AC) = (A'AC + ABC) + (<span class=\"pink\">C</span>) + (AC)</span></li>\n";
  echo "<li>Will cancel opposing variables - <span class=\"green\">X = (<span class=\"pink\">A'AC</span> + ABC) + (C)+ (AC) = (ABC) + (C) + (AC)</span></li>\n";
  echo "<li>Will simplify redundant terms - <span class=\"green\">X = (ABC) + (C) + (AC) = (C)</span></li>\n";
  echo "<li>Currently the calculator only accepts 'A', 'B', 'C', and 'D' as variables</li>\n";
  echo "<li>The calculator is rather fragile, nested brackets won't work - <span class=\"green\">X = ((AB+C) + AC)(BC) won't work</span></li>\n";
  echo "<li>It doesn't do demorgans at the moment. Sorry - <span class=\"green\">X = (A'B+C)' + (BC)' won't work</span></li>\n";
echo "</ul>\n";

echo "<h3>The Calculator</h3>\n";
echo "<form enctype=\"multipart/form-data\" id=\"boolean_calc\" action=\"./boolean_calc.php" . $admin_link . "\" method=\"POST\">\n";
echo "<table>\n";
  echo "<tr>\n";
    echo "<td><strong>Number of Variables: </strong></td><td>";
      echo "<select name=\"num_var\" id=\"num_var\" >\n";
	  for ($i=2; $i<=6; $i++) {
	    echo "<option value=\"" . $i . "\"";
		echo ($i == $num_var) ? " SELECTED " : null;
	    echo ">" . $i . "</option>\n";
	  }
	  echo "</select>\n";
    echo "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td><strong>Equation: </strong></td><td> X = ";
      echo "<input type=\"text\" name=\"equation\" id=\"equation\" value=\"" . $equation . "\" size=\"75\"/>";
	echo "</td>\n";
  echo "</tr>\n";
echo "</table>\n";
echo "<input type=\"submit\" value=\"Submit\" />\n";
echo "</form>\n";



//#
//#####    BODY - SOLUTION   #####
//#

echo "<h3>The Solution</h3>\n";

echo "<h4>The Work</h4>\n";
echo "<p>X = " . $equation . "\n";
foreach ($steps as $key => $value) {
echo "<br />Step " . ($key+1) . ":&nbsp;&nbsp;&nbsp;X = " . $value . "\n";
}
echo "<br />Step 9:&nbsp;&nbsp;&nbsp;Create truth table\n";
echo "<br />Step 10:&nbsp;&nbsp;&nbsp;Create Karnaugh map";
echo "</p>\n";



//#
//#####    BODY - TRUTH TABLE   #####
//#

echo "<h4>Truth Table</h4>\n";
echo "<br />";
echo "<table>\n";
  echo "<tr>\n";
    for ($i=0; $i<$num_var; $i++) { echo "<th>" . $truth_head[$i] . "</th>\n"; }
    echo "<th class=\"spacer\"></th>\n<th>X</th>\n";
  echo "</tr>\n";
  for ($i=0; $i<$table_size; $i++) {
  echo "<tr>\n";
      for ($j=0; $j<$num_var; $j++) {
	    echo "<td>" . substr($truth_body[$i][0], $j, 1) . "</td>\n";
	  }
    echo "<td class=\"spacer\"></td>\n";
	echo "<td>" . $truth_body[$i][1] . "</td>";
  echo "</tr>\n";
  }
echo "</table>\n";
echo "<p>Our function for the Karnaugh map is:</p>\n";
echo "<h3 class=\"equation\">X = F(";
foreach($equ_truth as $key => $value) { echo bindec($value); echo ($value != end($equ_truth)) ? ", " : null;}
echo ")</h3>\n";



//#
//#####    BODY - KARNAUGH MAP  #####
//#

echo "<h4>Karnaugh Map</h4>\n";
echo "<br />";
echo "<table>\n";
  echo "<tr>\n";
    echo "<th colspan=2 rowspan=2>X=</th>\n";
    echo "<th colspan=" . $x_axis . ">" . $x_title . "</th>\n";
  echo "</tr>\n";
  echo "<tr>\n";
	foreach($x_head as $value) {
      echo "<th>" . $value . "</th>\n";
    }
  echo "</tr>\n";
  foreach($y_head as $y_key => $y_value) {
    if ($y_key == 0) {
      echo "<tr>\n";
      echo "<th rowspan=" . $y_axis . ">" . $y_title . "</th>\n";
	} else {
      echo "<tr>\n";
	}
    echo "<th>" . $y_value . "</th>\n";
	for ($i=0; $i<$x_axis; $i++) {
      $disp_num = ($y_key * $x_axis) + $i;
	  echo "<td>";
	  echo $equ_map[$disp_num];
	  echo "</td>\n";
	}
  echo "</tr>\n";
  }
  
  echo "<tr>\n";
  echo "</tr>\n";

echo "</table>\n";



//#
//#####    BODY - EQUATION   #####
//#

echo "<h4>Equation</h4>\n";




echo "<div class=\"leftpic\"><img src=\"./images/art/web_art_01.jpg.jpg\" width=100 height=100></div>\n";
*/
include_once("./files/close.php");
?>