#!/usr/bin/perl
local @theoutput;
push @theoutput,'<html>
<!-- ';
push @theoutput,'This page and all its contents are Copyright (C) 1996-2026 by End of the World Production, LLC.. All rights reserved.';
push @theoutput,' -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta NAME="author" CONTENT="Particle">
<meta NAME="owner" CONTENT="particle at NO SPAM the particle dot com">
<meta NAME="rating" CONTENT="For Everyone.">
<meta NAME="description" CONTENT="Prof.Phreak - ';
push @theoutput,'End of the World Production, LLC.';
push @theoutput,'">
<meta NAME="keywords" CONTENT="Particle; Programming; Quark; Mr.Phreak; Infinity; Prof.Phreak">
<title>Prof.Phreak - ';
push @theoutput,'End of the World Production, LLC.';
push @theoutput,'</title>
<style>
<!--
A:link {text-decoration: none; font-weight: bold; font-family: Verdana, Arial, Helvetica;}
A:visited {text-decoration: none; font-weight: bold; font-family: Verdana, Arial, Helvetica;}
-->
</style>
</head>

<body topmargin="0" leftmargin="0" bgcolor="white" text="black" link="gray" vlink="gray" alink="gray">
<script language="JavaScript">
<!--
ua=navigator.userAgent;
v=navigator.appVersion.substring(0,1);
if((ua.lastIndexOf("MSIE")!=-1) && (v!=\'1\') && (v!=\'2\') && (v!=\'3\')){
    document.body.onmouseover=CMOvr;
    document.body.onmouseout=CMOut;
}
function CMOvr(){
    if(event.toElement.tagName == "A" || event.toElement.className == "ColorChanger"){
        event.toElement.oldcol = event.toElement.style.color;
        event.toElement.style.color = "000000";
    }
}
function CMOut(){
    if(event.fromElement.tagName == "A" || event.fromElement.className == "ColorChanger")
        event.fromElement.style.color = event.fromElement.oldcol;
}
// --></script>

<table cellspacing="0" cellpadding="0" width="100%" border=0><tr><td width="100%"><img
src="../files/library/images/decorations/x37AD87.gif" border="0" alt width="100%"
height="1"></td></tr></table><table
cellspacing=0 cellpadding=0 width="100%" border="0"><tr><td height="84" align="bottom"><nobr><img
src="../files/library/images/decorations/blank.gif" border="0" width="1" height="84"><FONT
face="Verdana, Arial, Helvetica"
size="6"><B><i>Prof.Phreak</i></B></FONT><nobr><br>';
push @theoutput,'<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td bgcolor="#006171" background="../files/library/images/decorations/grad_low_bg_18x40.gif" height="20">
              <nobr><font size="1" face="Verdana, Arial, Helvetica" color="#FFFFFF">
                  &nbsp;<b class="header_links">
                      <a href="../index.html"><font color="#FFFFFF">home</font></a> |
                      <a href="../corp_services.html"><font color="#FFFFFF">services</font></a> |
                      <a href="../corp_products.html"><font color="#FFFFFF">products</font></a> |
                      <a href="../corp_resources.html"><font color="#FFFFFF">resources</font></a> |
                      <a href="../corp_forum/corp_forum.php"><font color="#FFFFFF">forum</font></a>
                      </b>
              </font></nobr>
        </td>
        <td width="28" height="20"><img src="../files/library/images/decorations/grad_low_right_ang_28x20.gif" border="0" width="28" height="20"></td>
    </tr>
</table><!--

<map name="main_header_map">
      <area href="../index.html" shape="rect" coords="3, 4, 41, 19">
      <area href="../corp_news.html" shape="rect" coords="44, 3, 84, 19">
      <area href="../corp_services.html" shape="rect" coords="87, 3, 142, 17">
      <area href="../corp_products.html" shape="rect" coords="146, 3, 200, 19">
      <area href="../corp_research.html" shape="rect" coords="206, 2, 264, 19">
      <area href="../corp_resources.html" shape="rect" coords="269, 4, 330, 19">
      <area href="../corp_forum/corp_forum.php" shape="rect" coords="336, 0, 391, 19"></map><img height="20"
alt="" src="../files/library/images/decorations/main_news_services_products_forum_bar2.gif"
border="0" width="407" usemap="#main_header_map"> -->';
push @theoutput,'</td><td width="100%">&nbsp;</td><td valign="top"
align="right">';
push @theoutput,'<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="28" height="20"><img src="../files/library/images/decorations/grad_high_left_ang_28x20.gif" border="0" width="28" height="20"></td>
        <td bgcolor="#006171" background="../files/library/images/decorations/grad_high_bg_18x40.gif" height="20"><nobr><font size="1" face="Verdana, Arial, Helvetica" color="#FFFFFF">
                  <b>
                      <a href="../corp_about_us.html"><font color="#FFFFFF">about us</font></a> | <!--                      
                      <a target="_blank" href="http://theparticle.com/"><font color="#FFFFFF">careers</font></a> | --> <a href="../corp_contact.html"><font color="#FFFFFF">contact</font></a>
                  </b>
</font>&nbsp;&nbsp;</nobr></td></tr></table>';
push @theoutput,'<nobr><img
src="../files/library/images/logos/plogo.gif" alt="Particle" border="0" width="61" height="68"><img
src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"></nobr><br><nobr><font size="1"
face="Verdana, Arial, Helvetica">';
push @theoutput,'April 29th, 2026';
push @theoutput,'</font><img
src="../files/library/images/decorations/blank.gif" alt border="0" width="11"
height="1"></nobr></td></tr></table><table cellspacing="0"
cellpadding="0" width="100%" border=0><tbody><tr><td width="100%"><img
src="../files/library/images/decorations/x37AD87.gif" border="0" alt width="100%"
height="1"></td></tr></tbody></table>

<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td width="224" valign="top">
    <br><br>
    ';
push @theoutput,'<img src="../files/library/images/decorations/blank.gif" alt border="0" width="130"
height="1"><br><font size="1"
face="Verdana, Arial, Helvetica"><nobr><img
src="../files/library/images/decorations/bluebullet.gif" hspace="2"
alt border="0" width="7"
height="7"><b>www.theparticle.com</b></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../index.html">Main</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_services.html">Services</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_products.html">Products</a></nobr><br>

<!--    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_research.html">Research</a></nobr><br>
-->

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_resources.html">Resources</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_forum/corp_forum.php">Forum</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_about_us.html">About Us</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_contact.html">Contact</a></nobr><br>
    <br>

    <nobr><img src="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><b>Particle Revelation</b></nobr><br>
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../hardware/index.html">Hardware Destruction</a></nobr><br>
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../quotes.html">Quotes</a></nobr><br>
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../fun/fun.php" target="_blank">Humor [alpha]</a></nobr><br>
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../murphy.html">Murphy\'s Laws</a></nobr><br>
    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Programming</b></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_java.html">Java</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../javadata2.html">Java Data Structures</a></nobr><br>
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../csdata.html">C# Data Structures</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../documents/database_design.html">Database Design</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../pgraph.html">Graphics Tutorial</a></nobr><br>
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../ai.html">Artificial Intelligence</a></nobr><br>

    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Downloads</b></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../sqlrunner/index.html">SQLRunner</a></nobr><br>

    <nobr><img SRC="../files/';
push @theoutput,'library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_resources.html">Graphics Tutorials</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../hacking/index.html">Hacking Tutorials</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../corp_java.html">Java Applets</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../midi/index.html">MIDI Music</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../govmisc/index.html">Gov & Misc Docs</a></nobr><br>

    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Games</b></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../chess.html">Chess Game</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../pAsteroids.html">Asteroids</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/tictactoe/index.html">Tic-Tac-Toe</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/tetris/index.html">Tetris</a></nobr><br>

    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Applets</b></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/draw/index.html">DRAW!</a></nobr><br>
    
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/geometry/Q1FlightBox/index.html">FlightBox</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/wobble/index.html">pWobble 3D</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../pRunner.html">pRunner</a></nobr><br>
    
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/nyu/index.html">NYU HWs</a></nobr><br>

    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/swarm/index.html">Swarms</a></nobr><br>
    
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/geometry/index.html">Geometry</a></nobr><br>
   
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/chaos/index.html">Chaos</a></nobr><br>
    
    <nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../applets/ml/index.html">Machine Learning</a></nobr><br>
    
    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Academic</b></nobr><br>


    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/dsci/index.html" target="_blank">CISC7700</a> </nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/dbsys/index.html" target="_blank">CISC7510/7512</a> </nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11';
push @theoutput,'" height="1"><a
    href="../cs/bc/ai/index.html" target="_blank">AI (old)</a> </nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/mis/index.html" target="_blank">MIS</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/itproj/index.html" target="_blank">IT Mngmt</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/swdev/index.html" target="_blank">SW</a> (old)</nobr><br>
    
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/net/index.html" target="_blank">Networks</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/os/index.html" target="_blank">OS</a> (old)</nobr><br>
        
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/app/index.html" target="_blank">App Dev</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/cisc3110/index.html" target="_blank">C++</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/oop/index.html" target="_blank">OOP</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/cis53/index.html" target="_blank">Web</a> (old)</nobr><br>    

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/perl/index.html" target="_blank">Perl</a> (old)</nobr><br>
     
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/dbms/index.html" target="_blank">DBMS</a> (old)</nobr><br>
    
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/proglangs/index.html" target="_blank">ProgLangs</a> (old)</nobr><br>
    
    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/gc/php/index.html" target="_blank">PHP</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/bc/mcs/index.html" target="_blank">MltMedia</a> (old)</nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../cs/kg/cp207/index.html" target="_blank">Oracle</a> (old)</nobr><br>

    <br>
    <img SRC="../files/library/images/decorations/bluebullet.gif" hspace="2"
    alt border="0" width="7" height="7"><nobr><b>Misc</b></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../privacy/index.html">Privacy Policy</a></nobr><br>

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="../documents/index.html">Publications</a></nobr><br>
    

    <nobr><img SRC="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
    href="http://profphreak.com/" target="_blank">profphreak.com</a></nobr><br>

    <br><br><nobr><img src="../files/library/images/decorations/blank.gif" alt border="0" width="11" height="1"><a
        href="http://www.eff.org/br/" target="_blank"><img
        src="../files/library/images/logos/br_88x32.gif"
        width="88" height="32" border="0"
        alt=""></a></nobr></font>';
push @theoutput,'</td>

<td width="12" valign="top" background="../files/library/images/decorations/background_vertial_bar_small_0000.gif">
<img valign="top" src="../files/library/images/decorations/vertial_bar_top_small_0000.gif" border="0" alt width="10" height="150"><br>
<img src="../files/library/images/decorations/blank.gif" border="0" alt width="12" height="1"></td>

<td width="100%" valign="top">

<p><font face="Verdana, Arial, Helvetica" size="5"><nobr><i>Prof.Phreak</i></nobr></font></p>

<font face="Verdana, Arial, Helvetica" size="2">

<p></p>


';


# read input form

read(STDIN,$input,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$input);
push @pairs,split(/&/,$ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/\0//g;
        $value =~ s/<!--(.|\n)*-->//g;
        $FORM{$name} = $value;
}





#
# Experimental version of Prof.Phreak POC2
#
# Copyright(c) 2001, Particle Corporation
#

local %prof_phreak_environment;

# hashes the value of a number into it's name.
%numberToWordHash = ("0"=>"zero","1"=>"one","2"=>"two",
   "3"=>"three","4"=>"four","5"=>"five","6"=>"six","7"=>"seven","8"=>"eight","9"=>"nine",
   "10"=>"ten","11"=>"eleven","12"=>"twelve","13"=>"thirteen",
   "14"=>"fourteen","15"=>"fifteen","16"=>"sixteen",
   "17"=>"seventeen","18"=>"eighteen","19"=>"nineteen",
   "20"=>"twenty","30"=>"thirty","40"=>"forty","50"=>"fifty",
   "60"=>"sixty","70"=>"seventy","80"=>"eighty","90"=>"ninety",
   "100"=>"hundred","1000"=>"thousand","1000000"=>"million",
   "1000000000"=>"billion");

# hashes a 'name' into the actual numeric symbol.
# (note, hashes 'open' and 'close' ... this may not
# be appropriate in all contexts
%wordToNumberHash = ("zero"=>0,"one"=>1,"two"=>2,
   "three"=>3,"four"=>4,"five"=>5,"six"=>6,"seven"=>7,"eight"=>8,"nine"=>9,
   "ten"=>10,"eleven"=>11,"twelve"=>12,"thirteen"=>13,
   "fourteen"=>14,"fifteen"=>15,"sixteen"=>16,
   "seventeen"=>17,"eighteen"=>18,"nineteen"=>19,
   "twenty"=>20,"thirty"=>30,"forty"=>40,"fifty"=>50,
   "sixty"=>60,"seventy"=>70,"eighty"=>80,"ninety"=>90,
   "hundred"=>100,"thousand"=>1000,"million"=>1000000,
   "billion"=>1000000000,"open"=>'(',"close"=>')',
   "plus"=>'+',"minus"=>'-',
   "times"=>'*',"divide"=>'/');



# keywords used in expressions... (used to breakup
# input into an expression and remove all other 'junk')
@numericalWords = ("zero","one","two",
   "three","four","five","six","seven","eight","nine",
   "ten","eleven","twelve","thirteen",
   "fourteen","fifteen","sixteen",
   "seventeen","eighteen","nineteen",
   "twenty","thirty","forty","fifty",
   "sixty","seventy","eighty","ninety",
   "hundred","thousand","million","billion",
   "negative","plus","minus",
   "divide","times","open","close");

# keyword symbols. (same use as above; seperate because
# used differently in constructing a regular expression)
@symbols = ('(','/','*','+','-',')');

# create proper regular expressions (longest first)
my @numericalWords = sort {length($b) <=> length($a)} @numericalWords;
$numericalWordsRE = join '|',@numericalWords;
foreach(@numericalWords){
   $numericalWordsHash{$_} = $_;
}

# --------------------------------------------
# check whether input is a symbol.
sub isexpressionsymbol {
   my ($symb) = @_;
   foreach(@symbols){
      if($symb =~ m/(\Q$_\E)/){
         return 1;
      }
   }
   return 0;
}

# --------------------------------------------
# second conversion step
sub computeNumber2 {
   local ($arr_ref,$n,$bound) = @_;
   local @arr = @{$arr_ref};
   local $tmp=0;
   local $number = 0;
   for($n--;$n >= 0;){
      if(((($tmp=$arr[$n]) % 100) != 0) || $arr[$n] == 0){
         $number += $arr[$n--];
      }elsif($bound >= $arr[$n]){
         local $res = computeNumber2(\@arr,$n,$tmp);
         $number += $tmp * ${$res}[0];
         $n = ${$res}[1];
      }else{
         last;
      }
   }
   local @tarr;
   $tarr[0] = $number == 0 ? 1:$number;    # fixed? ;needs more testing.
   $tarr[1] = $n;
   return \@tarr;
}

# ------------------------------------------------
# routine to convert a bunch of numbers in an
# array into a composite number...
sub convertNumber {
   local @input = @_;
   local $i;
   local $tmp=0;
   local $number=0;
   # simple case first
   if($#input == 0){
      return $input[0];
   }

   # get number's sign (and get rid of all the "negative" strings out of the array.
   my $sign=1;
   for($i=0;$i<=$#input;$i++){
      if($input[$i] eq "negative"){
         $sign=-1;
         splice @input,$i,1;
      }
   }

   # go though the first step conversion process
   for($i=$#input;$i >= 0;){
      if(((($tmp=$input[$i]) % 100) != 0) || $tmp == 0){
         $number += $input[$i--];
      } else {
         local $res = computeNumber2(\@input,$i,$tmp);
         $number += $tmp * ${$res}[0];
         $i = ${$res}[1];
      }
   }
   return $sign * $number;
}

# --------------------------------------------------------------------
# check the parenthesis within the expression
sub check_parenthesis {
   my ($arr_ref) = @_;
   my @arr = @{$arr_ref};
   my $i;
   my $count=0;
   for($i=0;$i<=$#arr;$i++){
      if($arr[$i] eq '('){
         $count++;
      }elsif($arr[$i] eq ')'){
         if($count == 0){
            $count = 1;
            last;
         }else{
            $count--;
         }
      }
   }
   return $count == 0 ? 1:0;
}

# -------------------------------------------------------------
# convert an infix expression to post fix
sub infix_to_postfix {

   my ($arr_ref) = @_;
   my @arr = @{$arr_ref};
   splice @{$arr_ref};
   my @stack;
   my $i;

   for($i=0;$i<=$#arr;$i++){
      my $var = $arr[$i];
      if($var =~ m/[0-9]+/){
         push @{$arr_ref},$var;
      }elsif($var eq '+'|| $var eq '-' || $var eq '*' || $var eq '/'){
         if($#stack < 0){
            push @stack, $var;
         }else{
            my $sp = pop @stack;
            if($sp eq '(' || $sp eq ')'){
               push @stack,$sp;
               push @stack,$var;
            }else{
               if( ($sp eq '+' || $sp eq '-') &&
                  ($var eq '*' || $var eq '/')){
                  push @stack,$sp;
                  push @stack,$var;
               }else{
                  push @stack,$var;
                  push @{$arr_ref},$sp;
               }
            }
         }
      }elsif($var eq '(' || $var eq ')'){
         if($var eq '('){
            push @stack,$var;
         }elsif($var eq ')'){
            while($#stack >= 0){
               my $sp = pop @stack;
               if($sp ne '('){
                  push @{$arr_ref},$sp;
               }else{
                  last;
               }
            }
         }
      }else{
         return 0;
      }
   }
   while($#stack >= 0){
      my $sp = pop @stack;
      push @{$arr_ref},$sp;
   }
   return 1;
}




# -------------------------------------------------------
# evaluate a postfix expression...
sub solve_postfix_expression {
   my ($arr_ref) = @_;
   my @arr = @{$arr_ref};
   my @stack;
   my $i;
   my $var;
   for($i=0;$i<=$#arr;$i++){
      $var = $arr[$i];
      if($var =~ m/[0-9]+/){
         push @stack,$var;
      }else{
         if($#stack < 0){
            $prof_phreak_environment{"input.math.expression.solved"}=0;
            return 0;
         }
         my $a = pop @stack;
         if($#stack < 0){
            $prof_phreak_environment{"input.math.expression.solved"}=0;
            return 0;
         }
         my $b = pop @stack;
         my $tmp = 0;
         if($var eq '+'){
            $tmp = $b + $a;
         }elsif($var eq '-'){
            $tmp = $b - $a;
         }elsif($var eq '*'){
            $tmp = $b * $a;
         }elsif($var eq '/'){
            # check division by zero
            if($a == 0){
               $prof_phreak_environment{"input.math.expression.division_by_zero"}=$b;
               return 0;
            }
            $tmp = $b / $a;
         }else{
            $prof_phreak_environment{"input.math.expression.solved"}=0;
            return 0;
         }
         push @stack,$tmp;
      }
   }
   if($#stack < 0){
      $prof_phreak_environment{"input.math.expression.solved"}=0;
      return 0;
   }
   $var = pop @stack;
   if($#stack >= 0){
      $prof_phreak_environment{"input.math.expression.solved"}=0;
      return 0;
   }
   splice @{$arr_ref};
   @{$arr_ref}[0] = $var;
   return 1;
}


# -------------------------------------------------------------------------
# 3rd level helper function for number->word conversion
sub convert_number_to_words3 {
   local ($number,$v) = @_;
   local $rem = $number % 100;
   if(($number - $rem) > 0){
      local $n = ($number - $rem) / 100;
      if(defined $numberToWordHash{"$n"}){
         push @{$v},$numberToWordHash{"$n"};
         push @{$v},"hundred";
      }
   }
   if($rem != 0){
      if(defined $numberToWordHash{"$rem"}){
         push @{$v},$numberToWordHash{"$rem"};
      }else{
         my $rem2 = $rem % 10;
         my $tmp = $rem - $rem2;
         if(defined $numberToWordHash{"$tmp"}){
            push @{$v},$numberToWordHash{"$tmp"};
         }
         if(defined $numberToWordHash{"$rem2"}){
            push @{$v},$numberToWordHash{"$rem2"};
         }
      }
   }
}

# -------------------------------------------------------------------------------------
# helping function in number->word conversion
sub convert_number_to_words2 {
   local ($number,$what,$v) = @_;
   local $rem = $number % ($what * 1000);
   if(($number - $rem) > 0){
      convert_number_to_words2($number - $rem,$what*1000,$v);
   }
   if($rem != 0){
      $rem /= $what;
      convert_number_to_words3($rem,$v);
      if($what > 1){
         if(defined $numberToWordHash{"$what"}){
            push @{$v},$numberToWordHash{"$what"};
         }
      }
   }
}

# ----------------------------------------------------------------------------
# convert a number back to word representation (support decimals)
sub convert_number_to_words {
   local ($number) = @_;
   local @v;
   if($number == 0){
      push @v,"zero";
   }else{
      if($number < 0){
         push @v,"negative";
         $number = -$number;
      }
      my $whole = int $number;
      convert_number_to_words2($whole,1,\@v);

      # get decimal, and write it (if it exists)
      my $part_of_whole = (int(($number - $whole) * 10000))/10000;
      if($part_of_whole != 0){
         # put code to print the decimal point
         push @v,"point";
         $part_of_whole =~ s/[0-9]+\.|0+$//g;
         my @chars = unpack "c*",$part_of_whole;
         for($i=0;$i<=$#chars && $i<3;$i++){
            push @v,$numberToWordHash{chr($chars[$i])};
         }
         # continue number 'forever'
         if($i<=$#chars){
            push @v,"..."
         }
      }
   }
   return @v;
}




# -------------------------------------------------------------------
# check size of a number using 'bits of numbers'...
sub numberSizeOk {
   my @input = @_;
   my $count = 0;
   foreach(@input){
      my $str = "$_";
      $count += length $str;
   }
   return $count > 32 ? 0:1;
}

#----------------------------------------------------------------------------------------
# process the expression (parse out numbers, change expression, evaluate (if possible))
sub process_expression {
   local ($input) = @_;
   local $i;

   # check if there are any numbers with more than 12 digits
   if($input =~ m/[0-9]{15,}/){
      $prof_phreak_environment{"input.math.expression.number_out_of_range"}=1;
      return;
   }

   # get rid of all words that have number words as their substring
   open(NUMBER_WORDS,"<phreak_math_exclude.db");
   while(<NUMBER_WORDS>){
      chomp;
      $input =~ s/$_//g;
   }
   close(NUMBER_WORDS);


   # give symbols some space
   foreach(@symbols){
      $input =~ s/(\Q$_\E)/ \1 /g;
   }

   # give numbers (less than 12 digits) some space
   $input =~ s/(([0-9]+)(.[0-9]+)?)/ \1 /g;

   # give word numbers (ie: 'one' 'two', etc.) some space
   $input =~ s/($numericalWordsRE)/ \1 /g;

   # go through the input, and get rid of anything that's not part of an 'expression'
   @input = split ' ',$input;
   for($i=0;$i<=$#input;){
      if(defined $numericalWordsHash{$input[$i]} ||
         isexpressionsymbol($input[$i]) ||
         $input[$i]=~m/(([0-9]+)(.[0-9]+)?)/){

         # change the 'word' representation into a number (not finished yet)
         if(defined $wordToNumberHash{$input[$i]}){
            $input[$i] = $wordToNumberHash{$input[$i]};
         }
         $i++;
      }else{
         splice @input,$i,1;
      }
   }


   # create an expression by joining 'number bits' into larger number
   # using operators as delimiters.
   my @currentNumber;
   my @inputExpression;
   for($i=0;$i<=$#input;$i++){
      if($input[$i] eq '(' || $input[$i] eq ')' || $input[$i] eq '+' ||
         $input[$i] eq '-' || $input[$i] eq '*' || $input[$i] eq '/'){
         if($#currentNumber >= 0){
            # check to make sure the resulting number is 12 digits or less
            if(!numberSizeOk(@currentNumber)){
               $prof_phreak_environment{"input.math.expression.number_out_of_range"}=1;
               return;
            }
            push @inputExpression, convertNumber(@currentNumber);
            splice @currentNumber;
         }
         push @inputExpression,$input[$i];
      }else {
         push @currentNumber,$input[$i];
      }
   }
   if($#currentNumber >= 0){
      # check to make sure the resulting number is 12 digits or less
      if(!numberSizeOk(@currentNumber)){
         $prof_phreak_environment{"input.math.expression.number_out_of_range"}=1;
         return;
      }
      push @inputExpression, convertNumber(@currentNumber);
      splice @currentNumber;
   }
   @input = @inputExpression;
   undef @inputExpression;

   # if nothing on input by this time, there is no rexpression.
   return unless $#input >= 0;

   # check to see if it's a singleton; or if no expression
   if($#input == 0){
      if($input[0] =~ m/(([0-9]+)(.[0-9]+)?)/){
         my @result = convert_number_to_words ($input[0]);
         $prof_phreak_environment{"input.math.expression.singleton"} = join ' ',@result;
      }
      return;
   }


   # if expression has no numbers, return (no expression)
   my $flag=0;
   foreach(@input){
      if(/[0-9]+/){
         $flag=1;
         last;
      }
   }
   return unless $flag;


   # if parenthesis don't match
   if(!check_parenthesis(\@input)){
      $prof_phreak_environment{"input.math.expression.parenthesis.match"}=0;
      return;
   }

   # convert the infix expression to postfix
   if(!infix_to_postfix(\@input)){
      $prof_phreak_environment{"input.math.expression.infix_to_postfix"}=0;
      return;
   }

   # solve the postfix expression (result is first element of array)
   if(!solve_postfix_expression(\@input)){
      return;
   }

   # convert result to words, and return
   my @result = convert_number_to_words ($input[0]);
   $prof_phreak_environment{"input.math.expression.result.word"} = join ' ',@result;
}



#--------------------------------------------------------------------

local @from = ("are you","you are","my",  "your","me", "i am",    "am i",    "i",  "myself",  "you");
local @to = (  "I am",   "I am",   "your","my",  "you","you are", "you are", "you","yourself","I");

local $db_load_count = 0;

#-------------------------------------------------------------------
# given name, loads database. returns reference to db
sub load_database {
   my ($dbname) = @_;
   # load database of simple keywords
   open(IN,"<$dbname") || die;
   my $db_ref;
   while(<IN>){
      chomp;
      s/^\s+|\s+$//g;
      s/^\/\/.*$//g;
      next unless length;
      my ($keywords,$keyvalues,$code) = split /##/;
      my $obj = bless {};
      my @keywords = split /#/,$keywords;
      my @keyvalues = split /#/,$keyvalues;
      # enable easier space matching (if there is a space, match 0 or more spaces)
      for($i=0;$i<=$#keywords;$i++){
         $keywords[$i] =~ s/\s+/\\s\*/g;
      }
      $obj->{keywords} = \@keywords;
      $obj->{keyvalues} = \@keyvalues;
      $obj->{code} = $code;
      $obj->{id} = $db_load_count++;
      push @{$db_ref},$obj;
   }
   return $db_ref;
}



# ----------------------------------------------------------------------
# get reply
sub getreply {
   my ($s,$db_ref) = @_;
   my $r;
   my @match_list;

   # search for a match
REFLOOP: for($db_ref_index=0;$db_ref_index<=$#{$db_ref};$db_ref_index++){
      $obj = ${$db_ref}[$db_ref_index];
      my @keywords = @{$obj->{keywords}};
      for($keywords_index=0;$keywords_index<=$#keywords;$keywords_index++){
         $_ = $keywords[$keywords_index];
         if($s =~ m/($_)/){
            $obj->{s} = length $1;
            my @vars = ($2,$3,$4,$5,$6,$7,$8,$9);
            # get rid of spaces in matched variables
            for($i=0;$i<=$#vars;$i++){
               $vars[$i] =~ s/^\s*|\s*$//g;
            }
            $obj->{vars} = \@vars;
            push @match_list,$obj;
            next REFLOOP;
         }
      }
   }

   # (longer string matches get precedense over few letters matching)
   if($#match_list >= 0){

      # find max
      $obj = $match_list[0];
      for($i=1;$i<=$#match_list;$i++){
         if($obj->{s} < $match_list[$i]->{s}){
            $obj = $match_list[$i];
         }
      }
      my @keyvalues = @{$obj->{keyvalues}};

      my $storage_tmp_filename = "/tmp/theparticle_cgi_shared_tmp.dbm";
      dbmopen(%NUMBS,$storage_tmp_filename,0600);
      $number = int $NUMBS{ $obj->{id} };
      if($number > $#keyvalues){
         $number = 0;
      }
      $r = $keyvalues[$number++];
      $NUMBS{ $obj->{id} } = $number;
      dbmclose(%NUMBS);

      # process code fragments (the code works with (and replaces) @vars)
      if(defined $obj->{code}){
         my @vars = @{$obj->{vars}};
         eval($obj->{code});
         # get rid of spaces in matched variables
         for($i=0;$i<=$#vars;$i++){
            $vars[$i] =~ s/^\s*|\s*$//g;
         }
         $obj->{vars} = \@vars;
      }

      # do variable replacements
      my @vars = @{$obj->{vars}};
      for($i=0;$i<=$#vars;$i++){
         for($j=0;$j<=$#from;$j++){
            last if $vars[$i] =~ s/^$from[$j]\s/$to[$j] /g;
            last if $vars[$i] =~ s/^$from[$j]$/$to[$j]/g;
            last if $vars[$i] =~ s/\s$from[$j]\s/ $to[$j] /g;
            last if $vars[$i] =~ s/\s$from[$j]$/ $to[$j]/g;
         }
         $j = $i+1;
         $r =~ s/\$$j/$vars[$i]/g;
      }
   }
   return $r;
}

sub doreplies {
   my ($s) = @_;
   my $r = getreply($s,load_database("phreak_c.db"));
   if(!length $r){
      $r = getreply($s,load_database("phreak_s.db"));
   }
   if(!length $r){
      $r = getreply($s,load_database("phreak_t.db"));
   }
   return $r;
}



$_ = $FORM{'u'};

if(!length){
        $_ = "Hello.";
}

$u = $_;

   %prof_phreak_environment = ();
   $input = $_ = lc;
   process_expression (" $_ ");

   $_ = $input;
   s/^\s*|\s*$//g;
   s/\.|\,|\!|\?//g;
   s/\s+/ /g;
   s/\'re/ are/g;
   s/\'m/ am/g;
   s/\'d/ would/g;
   s/can\'t/can not/g;
   s/won\'t/will not/g;
   s/n\'t/ not/g;
   s/'s/ is/g;
   s/'ve/ have/g;
   s/^\s*|\s*$//g;

   $input = " $_ ";

   my $input_more = "";
   while (($key,$value) = each %prof_phreak_environment) {
      $input_more = "$input_more :$key=$value: ";
      print "key-value: $key=$value\n";
   }
   $input = "$input $input_more";
   $r = doreplies($input);

$formdata = $FORM{'o'};

# monitor conversations so that we can improve the database
open(OUT,">>profphreak.log");
$tim = scalar localtime();
print OUT "U($tim): $u\nP($tim): $r\n";
close(OUT);


push @theoutput,'<p><i><b>You:</b> ';
push @theoutput,"$u";
push @theoutput,'</i></p>
<p><b>Prof.Phreak:</b> ';
push @theoutput,"$r";
push @theoutput,'</p>
</font>
';


# get rid of CR from CRLF pair (the CR causes another extra LF in textarea)
$formdata =~ s/\x0D//g;
$formdata =~ s/\x0A+/\x0A/g;

if(length $formdata){
        $conversation_log = "$formdata\nYou: $u\nProf.Phreak: $r";
}else{
        $conversation_log = "You: $u\nProf.Phreak: $r";
}


push @theoutput,'<form name="f" method="POST" action="profphreak_ss.cgi">
<input type="text" name="u" value="" size="70"><input type=submit value="Send">
<hr size="1">
<font face="Verdana, Arial, Helvetica" size="2">Conversation Log</font><br>
<textarea cols=70 rows=15 name="o" readonly>';
push @theoutput,"$conversation_log";
push @theoutput,'</textarea></form>

<script language="JavaScript">
<!--
    document.f.u.focus();
// -->
</script>

<font face="Verdana, Arial, Helvetica" size="2">
<p><a href="profphreak_ss.cgi">restart conversation</a></p>
<p>&nbsp; </p>

<hr size="1">
<p>To learn more about Prof.Phreak, visit the <a href="index.html">main Prof.Phreak page</a>.</p>

</font>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

<table width="100%" border="0" cellpadding="0" cellspacing="0" valign="bottom">
  <tr>
    <td align="right">';
push @theoutput,'<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="28" height="20"><img src="files/library/images/decorations/grad_low_left_ang_28x20.gif" border="0" width="28" height="20"></td>
        <td bgcolor="#006171" background="files/library/images/decorations/grad_low_bg_18x40.gif" height="20"><font size="1" color="white"
face="Verdana, Arial, Helvetica"><b><nobr>&copy; 1996-2026 by End of the World Production, LLC.&nbsp;&nbsp;</nobr></b></font></td>
    </tr>
</table>';
push @theoutput,'</td></tr></table></td></tr></table></body></html>
';
print "content-type: text/html\n\n";
print @theoutput;
