#!/usr/bin/env perl

use strict;
use CGI qw(:standard);
use CGI::Carp 'fatalsToBrowser';

my $f1 = param("f1");
my $f2 = param("f2");
my ($of1, $of2);

sub parseF {
  my $f = shift;
  my $of = $f;
  if (defined $f) {
    $f =~ s/\s+//g;
    undef $f unless $f =~ /^([!+*()]|x\d|[01])+$/;
  }
  if (defined $f) {
    $f =~ s/([^(!+*])([x!])/$1*$2/g;
    $f =~ s/\+/ or /g;
    $f =~ s/\*/ and /g;
    $f =~ s/x(\d)/\$x\[$1\]/g;
  }
  return ($of, $f);
}

($of1, $f1) = parseF($f1);
($of2, $f2) = parseF($f2);

print <<EOF
Content-type: text/html

<html>

<head>
  <title>Programiranje 1: Logika sudova</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body bgcolor="#ffffda" text="#400000" link="#a00000" alink="#ff2020" vlink="#a00000" onLoad="document.f.b.focus();">

<h1>Programiranje 1: Logika sudova</h1>

<p>Ovdje možete usporediti ekvivalentnost dvije formule logike sudova</p>

EOF
;

if (defined $of1 and defined $of2) {
  my (@diff, $bad1, $bad2);

  print "<h2>Provjera:</h2>\n\n";

  $bad1 = 1 unless defined $f1;
  $bad2 = 1 unless defined $f2;
  unless ($bad1 or $bad2) {
    my ($bits, @trans) = (0);
    while ($bits < 10) {
      push @trans, $bits if $f1 =~ /x\[$bits\]/ or $f2 =~ /x\[$bits\]/;
      $bits++;
    }
    $bits = @trans;
    foreach my $x (0..(1<<$bits) - 1) {
      my ($xt, $s, %bits, @x) = ($x, "  <li><tt>");
      foreach my $b (0..$bits-1) {
        my $t = $trans[$b];
        $s .= "x<sub>$t</sub>=".($xt & 1)."; ";
        $x[$t] = $xt & 1;
        $xt >>= 1;
      }
      my $res1 = (eval($f1) ? 1 : 0);
      $bad1 = 1 if $@;
      my $res2 = (eval($f2) ? 1 : 0);
      $bad2 = 1 if $@;
      last if $bad1 or $bad2;
      if ($res1 != $res2) {
        $s .= "f<sub>1</sub>=$res1; f<sub>2</sub>=$res2</tt></li>\n";
        push @diff, $s;
      }
    }
  }

  if ($bad1 and $bad2) {
    print "<p>Formule su pogrešno zadane!</p>\n\n";
  } elsif ($bad1) {
    print "<p>Prva formula (<tt>$of1</tt>) je pogrešno zadana!</p>\n\n";
  } elsif ($bad2) {
    print "<p>Druga formula (<tt>$of2</tt>) je pogrešno zadana!</p>\n\n";
  } elsif (@diff) {
    print "<p><b>Formule nisu ekvivalentne!</b> Za slijedeće vrijednosti ulaznih varijabli imaju različite vrijednosti:</p>\n\n</ul>\n@diff</ul>\n\n";
  } else {
    print "<p><b>Formule su ekvivalentne!</b></p>\n\n";
  }
}

print <<EOF
<h2>Formule za usporedbu:</h2>

<form action="logika_sudova.pl" method="post">
<p>f<sub>1</sub>: <input type="text" size="40" name="f1" value="$of1" /><br />
f<sub>2</sub>: <input type="text" size="40" name="f2" value="$of2" /><br />
<input type="submit" value="Usporedi!" /></p>
</form>
<p><b>Uputa:</b> Izraz se zadaju pomoću x-eva: x0,x1,x2,...<br />
Operator "i" se zapisuje zvjezdicom ("*"), a operator "ili" plusom ("+"). Negacija se zapisuje pomoću uskličnika ("!") ispred izraza koji se negira.<br />
Na primjer, izraz<br />
<span class="o">x</span><sub>2</sub>&sdot;<span class="o">x</span><sub>1</sub>&sdot;x<sub>0</sub> + <span class="o">x</span><sub>2</sub>&sdot;x<sub>1</sub>&sdot;x<sub>0</sub><br />
zapisujemo ovako:<br />
<tt>!x2*!x1*x0+!x2*x1*x0</tt><br />
Provjeravati možete i pojednostavljene izraze, a dozvoljena je i upotreba zagrada.</p>

<p>Pitanja, komentari, pohvale i slično: <a href="http://degiorgi.math.hr/forum/viewtopic.php?t=5327">na Forumu</a>.</p>

<hr align="left" width="17%" />

<address>Created by Vedran Šego, 2005</address>

</body>

</html>
EOF
;
