Location: PHPKode > scripts > Simple RPN Interpreter > simple-rpn-interpreter/example2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RPN Interpreter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
.style1 {color: #CCCCCC}
.style2 {font-size: large}
.style3 {
	color: #CCCCCC;
	font-size: large;
	font-weight: bold;
	font-family: "Courier New", Courier, mono;
}
.style4 {color: #990000}
.style5 {font-family: "Courier New", Courier, mono}
.style7 {font-family: "Courier New", Courier, mono; font-weight: bold; }
.style8 {
	color: #FFFFFF;
	font-weight: bold;
}
.style9 {	font-size: medium;
	font-weight: bold;
}
-->
</style>
</head>
<body>
<span class="style5 style1"><a href="index.html"><tt>PhpSqlAsp.com</tt></a> <tt><a href="rpn.html">RPN Interpreter (NEW)</a></tt> </span>
<h1 align="center">EXAMPLE OF<br />
   <span class="style4">RPN INTERPRETER IN PHP</span> </h1>
 <h3>(C) Arturo Gonzalez-Mata Santana </h3>
 <p>This example read and execute a RPN commands txt file in the server. </p>
 <table  border="1" align="center" cellpadding="2" cellspacing="1">
      <tr bgcolor="#0066FF">
        <td colspan="2"><div align="center" class="style1 style2 style5"><strong>&quot;Program&quot; Line</strong></div></td>
        <td><div align="center" class="style3">Result</div></td>
      </tr>
<?php 
/* this example read a "RPN program file" (RPN.txt) and execute some actions
" " IS THE COMMAND SEPARATOR
NO SYMBOL FOR LINE END
			RPN "PROGRAM"		EQUIVALENT ALGEBRAIC OPERATIONS
Line 1: 	2 12 +      		····> 12 + 2 = 14
Line 2: 	8 - 9 *     		····> 14 - 8 = 6;      6 *9 = 54
Line 3:		5 / 78 5 + -		····> 54 / 5 = 10.8;   78 + 5 = 83;    10.8 - 83 = -72.2
Line 4:		25 10 * 50 +		····> 25 * 10 = 250;   250 + 50 = 300;
Line 5:		DUP *               ····> 300 * 300 = 90000    
Line 6:		SWAP -  			····> 90000 - -72.2 = 90072.2 
Line 7:		23 50 > IF + * 40 + then  ····> 6575310.6

Try to change condition Line 7 to "<" . 23 50 > IF + * 40 + then ····> 90
because " + *" are not evaluated when condition is false
*/
require_once("rpn.class.php");
$s=&new RPNstack();  // pila como variable global 

$ProgramFile = fopen("RPN.txt","r"); 


#READ A RPN COMMANDS FILE line by line
$num_lineas = 1;
while(!feof($ProgramFile)): 
    $buffer = trim(fgets($ProgramFile,4096)); 
?>
      <tr>
        <td bgcolor="#FFCC99"><div align="center" class="style5">#<?php echo $num_lineas++;?></div></td>
        <td bgcolor="#FFCC99"><span class="style7"><?php echo $buffer;?></span></td>
        <td bgcolor="#003300"><div align="right" class="style8" ><?php echo $s->parse_line($buffer); ?></div></td>
      </tr>
<?php
	//echo "<br>"; $s->dump();  // this line is only for debuging
endwhile;
?>
</table>
 <?php
printf("<h2>The Result is %s </h2>",$s->first());
?>
 <p>&nbsp;</p>
</body>
</html>
Return current item: Simple RPN Interpreter