コード例 #1
0
ファイル: Application6.php プロジェクト: EdenChan/Instances
 /**
  * Main program.
  *
  * @param array $args Command-line arguments.
  * @return integer Zero on success; non-zero on failure.
  */
 public static function main($args)
 {
     printf("Application program number 6. (expression tree)\n");
     $status = 0;
     $expression = ExpressionTree::parsePostfix(STDIN);
     printf("%s\n", str($expression));
     return $status;
 }
コード例 #2
0
<body>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="container">
    <h1>Result</h1>
    <pre>
        <?php 
include 'autoloader.php';
$expressionString = filter_input(INPUT_POST, 'expression', FILTER_SANITIZE_SPECIAL_CHARS);
if ($expressionString) {
    echo "<br>Processando a expressão: '{$expressionString}'<br>";
    try {
        $calc = new ExpressionTree($expressionString);
        echo "<br>Análise prefix: '{$calc->showPreFix()}'";
        echo "<br>Análise postfix: '{$calc->showPostFix()}'";
        echo "<br>Análise infix: '{$calc->showInFix()}'";
        echo "<br><br>Resultado:  '{$calc->evaluate()}'";
    } catch (Exception $e) {
        echo "<br>{$e->getMessage()}";
    }
} else {
    echo "Expressão inválida";
}
?>
    </pre>
    <br>
    <button class="btn btn-primary" onclick="voltar()">Voltar</button>
</div>
コード例 #3
0
ファイル: ExpressionTree.php プロジェクト: EdenChan/Instances
 /**
  * Parses a postfix expression read from the specified input stream
  * and constructs the corresponding expression tree.
  * The postfix expression consists of one-letter symbols,
  * one-digit numbers, +, -, * and /.
  *
  * @param resource $in The input stream.
  * @return object ExpressionTree An expression tree.
  */
 public static function parsePostfix($in)
 {
     $stack = new StackAsLinkedList();
     while (($c = fgetc($in)) != false) {
         if (ord('0') <= ord($c) && ord($c) <= ord('9') || ord('a') <= ord($c) && ord($c) <= ord('z') || ord('A') <= ord($c) && ord($c) <= ord('Z')) {
             $stack->push(new ExpressionTree($c));
         } elseif ($c == '+' || $c == '-' || $c == '*' || $c == '/') {
             $result = new ExpressionTree($c);
             $result->attachRight($stack->pop());
             $result->attachLeft($stack->pop());
             $stack->push($result);
         }
     }
     return $stack->pop();
 }