Оцените тестовое задание на php
Прислали тествое задание.
Напишите на PHP функцию, получающую на входе строку, содержащую математическое выражение в обратной польской нотации (например, «5 8 3 + *»), и возвращающую значение этого выражения (в примере «55»).
listNotation = explode(' ', $_GET['inputNotation']); $this->stackNotation = array(); $this->arifmeticSign = array(); } function setArifmetic() { foreach ($this->listNotation as $this->key => $this->value) { if (preg_match('#^([\+\-\*\/]{1})$#', $this->value) ) { $this->arifmeticSign[]= $this->value; } } } function setStack() { foreach ($this->listNotation as $this->key => $this->value) { if (preg_match('#^(-[0-9]+|[0-9]+)$#', $this->value) ) { $this->stackNotation []= $this->value; } } } function reverseStack() { $this->stackNotation = array_reverse($this->stackNotation); $this->lenght = count($this->stackNotation); return $this->lenght; } function calculateNotation() { $this->result = $this->stackNotation[0]; for($this->i = 1; $this->i < $this->lenght; $this->i++) { $this->j = $this->i - 1; switch($this->arifmeticSign[$this->j] ) { case '*': $this->result = $this->result * $this->stackNotation[$this->i]; break; case '/': $this->result = $this->result / $this->stackNotation[$this->i]; break; case '+': $this->result = $this->result + $this->stackNotation[$this->i]; break; case '-': $this->result = $this->result - $this->stackNotation[$this->i]; break; } } } function runCalculate() { $this->setArifmetic(); $this->setStack(); $this->reverseStack(); $this->calculateNotation(); echo "$this->result"; } } $n = new Notation; $n->runCalculate(); ?> <form action="new.php"> <p>input the expression</p> <input type="text" required="" name="inputNotation"> <input type="submit" value="clacni menya :)"> </form>
В результате отказали, вернее не ответили. В чем я ошибся?
77 коментарів
Додати коментар Підписатись на коментаріВідписатись від коментарів