$temp = 'v' . (string) ($cnt - 1); if ($turn > $cnt1 + 1) { echo "<td align='center'>"; echo $viterbi_matrix[($cnt - 1) * sizeof($words) + $cnt1]; echo "</td>"; $cnt1 = $cnt1 + 1; } else { if ($turn == $cnt1 + 1) { if ($flag == 1) { $prob1 = $viterbi[$cnt - 1]; $prob2 = $viterbi_matrix[($cnt - 1) * sizeof($words) + $cnt1]; try { //$prob1_f=(float)$prob1; //$prob2_f=(float)$prob2; $prob1_f = calculator($prob1); $prob2_f = calculator($prob2); } catch (Exception $e) { $flag_chk = 1; } $prob1_f = (string) round($prob1_f, 3); $prob2_f = (string) round($prob2_f, 3); $tmp = 1; if (preg_match("/[^0-9\\ \\.\\(\\)\\+\\-\\*\\/]/", $prob1) == 1) { $tmp = 0; } //if($prob1_f!=$prob2_f or ctype_alpha($prob1) or strlen($prob1)==0) if ($prob1_f != $prob2_f or $tmp == 0 or strlen($prob1) == 0) { $flag_chk = 1; } } if ($flag == 0) {
<html> <?php if (isset($_GET['fvalue']) && isset($_GET['lvalue']) && isset($_GET['operator'])) { $result = calculator($_GET['fvalue'], $_GET['lvalue'], $_GET['operator']); echo $result; } function calculator($fvalue, $lvalue, $operator) { if ($operator == "+") { $result = $fvalue + $lvalue; //return $result; } elseif ($operator == "-") { $result = $fvalue - $lvalue; //return $result; } elseif ($operator == "*") { $result = $fvalue * $lvalue; } elseif ($operator == "/" && $_GET['lvalue'] != 0) { $result = $fvalue / $lvalue; //return $result; } elseif ($lvalue == 0 && $operator == "/") { echo "Last value can not be zero"; } else { echo "Invalid"; } return $result; } ?> <body> <form action="calculator_with_function.php" method="get" align="center"> First Value: <input type="text" name="fvalue"><br><br> Last Value :<input type="text" name="lvalue"><br><br>
<form method="post" action="index.php"> <input type="text" name="string" placeholder="введите выражение"><br/><br/> <input type="submit" name="send" value="подсчитать"> </form> <?php $str = trim($_POST['string']); $button = $_POST['send']; if (empty($button)) { return FALSE; } else { function calculator($str) { $str = preg_replace("/[^0-9+\\-.*\\/]/", "", $str); if ($str == "") { $res = "строка не должна быть пустой"; } else { eval("\$res=" . $str . ";"); } return $res; } echo "Ваш ответ: " . calculator($str); }
<?php /** * Created by PhpStorm. * User: trener - Mateusz Marmolowski * Date: 05.10.15 */ echo calculator(10, '+', 5); // wynik 15 echo calculator(10, '-', 5); // wynik 5 echo calculator(10, '*', 5); // wynik 50 echo calculator(10, '/', 5); // wynik 2 function calculator($a, $operation, $b) { // ZADANIE - napisz cialo funkcji calculator ktora przyjmuje 3 argumenty i zwraca wynik operacji // ......... // ........ // ......... }
function calculator() { $operand1 = 20; $operand2 = 10; //$operator="+"; //$operator="-"; //$operator="*"; //$operator="/"; $operator = "%"; if ($operator == "+") { $res = $operand1 + $operand2; echo "Addition is ", $res; } elseif ($operator == "-") { $res = $operand1 - $operand2; echo "Subtraction is ", $res; } elseif ($operator == "*") { $res = $operand1 * $operand2; echo "Multiplication is ", $res; } elseif ($operator == "/") { $res = $operand1 / $operand2; echo "Division is ", $res; } elseif ($operator == "%") { $res = $operand1 % $operand2; echo "Modulus is ", $res; } else { echo "Invalid"; } } calculator();
<form method="post" action="index.php"> <input type="text" name="string" ><br/><br/> <input type="submit" name="send" value="count"> </form> <?php $str = trim($_POST['string']); $button = isset($_POST['send']); if ($button == TRUE) { echo "Your Answer: " . calculator($str); } function calculator($str) { $str = preg_replace("/[^0-9+\\-.*\\/]/", "", $str); if ($str == "") { $res = "line should not be empty"; } else { eval("\$res=" . $str . ";"); } return $res; }
<input type="number" name="number1" placeholder= "Enter your first number"/> </br> </br> <input type="number" name="number2"placeholder= "Enter your second number"/> </br> </br> <input type = "text" name = "operator" placeholder= "+ or - or * or / or % "/> </br> </br> <button type="submit">submit</button> </form> <?php // NORMAL CALCULATOR SOTUTION using function $result = ""; include "C:/xampp/htdocs/Training/PHP/functions.php"; if (isset($_GET['number1']) && isset($_GET['number2'])) { $number1 = $_GET['number1']; $number2 = $_GET['number2']; $operator = $_GET['operator']; $result = calculator($number1, $number2, $operator); // echo $result;(function er vitor echo kora ache, tai echo korte hoche na ekhane ) } ?> </div> </div> </div> </div> <?php /* function calculator($number1,$number2, $operator) { if($operator == "+") { $result = $number1 + $number2; echo "Result of addition is ".$result;
<html> <?php if (isset($_GET['fvalue']) && isset($_GET['lvalue']) && isset($_GET['operator'])) { $fvalue = $_GET['fvalue']; $lvalue = $_GET['lvalue']; $operator = $_GET['operator']; $result = calculator($fvalue, $lvalue, $operator); echo $result; } function calculator($fvalue, $lvalue, $operator) { if ($operator == "+") { $res = $fvalue + $lvalue; return $res; } elseif ($operator == "-") { $sub = $fvalue - $lvalue; return $sub; } elseif ($operator == "*") { $mul = $fvalue * $lvalue; return $mul; } elseif ($operator == "/" && $_GET['lvalue'] != 0) { $div = $fvalue / $lvalue; return $div; } elseif ($lvalue == 0 && $operator == "/") { echo "Last value can not be zero"; } else { echo "Invalid"; } //calculator(); } ?>
<?php header("Content-Type: text/plain"); include 'calculator.php'; if (isset($_GET['expr'])) { echo calculator($_GET['expr']); }
<?php include "C:/xampp/htdocs/training/php/OOP/myFunction/function.php"; include "C:/xampp/htdocs/training/php/OOP/myFunction/stack_oop.php"; include "C:/xampp/htdocs/training/php/OOP/myFunction/explode_functions.php"; $expression = "4 65 - 3 71 - +"; $exparr = explode(" ", $expression); /*echo "<pre>"; print_r($exparr); echo "</pre>";*/ $myStack = new Stack(); foreach ($exparr as $operator) { $temp = intval($operator); if ($temp) { $myStack->push($temp); /* echo "<pre>"; print_r($myStack); echo "</pre>"; */ } else { $op2 = $myStack->pop(); $op1 = $myStack->pop(); //echo "<br>",$op1," ",$op2," ",$operator,"<br>"; $result = calculator($op1, $op2, $operator); //echo $result; $myStack->push($result); } } echo "my result is :", $myStack->pop();
echo "<pre>"; print_r($expArray); echo "</pre>"; $myStack = new Stack(); foreach ($expArray as $op) { $temp = intval($op); if ($temp) { $myStack->push($temp); echo "<pre>"; print_r($myStack); echo "</pre>"; } else { $op2 = $myStack->pop(); $op1 = $myStack->pop(); echo "<br>", $op1, " ", $op2, " ", $op, "<br>"; $result = calculator($op1, $op2, $op); echo "<br>", $result, "<br>"; $myStack->push($result); echo "<pre>"; print_r($myStack); echo "</pre>"; } } echo "Result is: ", $myStack->pop(); } ?> <form action="polish_notation.php" method="post"> Enter an expression: <input type="text" name="exp"> <br>
<?php function calculator($a, $operator, $b) { if ($operator == "+") { echo $a + $b; } elseif ($operator == "-") { echo $a - $b; } elseif ($operator == "*") { echo $a * $b; } elseif ($operator == "/") { if ($b == 0) { echo "You can't divide by zero"; } echo $a / $b; } } calculator(15, "/", 3);
if ($btn == '*') { $result = $first_number * $second_number; } else { if ($btn == '/') { $result = $first_number / $second_number; } else { if ($btn == '%') { $result = $first_number % $second_number; } } } } } return $result; } $show = calculator($first_number, $second_number, $btn); ?> <html> <head> <title>Function Calculator</title> </head> <body> <form action="#" method="POST"> <table> <tr> <td>First Number</td> <td><input type="number" name="first_number"></td> </tr>
<?php require_once './function.php'; if (isset($_POST['submit_btn'])) { $first_number = $_POST['first_number']; $second_number = $_POST['second_number']; $btn = $_POST['submit_btn']; $result = calculator($first_number, $second_number, $btn); } ?> <html> <head></head> <body> <form action="#" method="post"> <table> <tr> <td>First Number</td> <td><input type="text" name="first_number"</td> </tr> <tr> <td>Second Number</td> <td><input type="text" name="second_number"</td> </tr> <tr> <td>Result</td> <td><input type="text" value="<?php if (isset($result)) { echo $result; } ?> "</td>
<?php $result = ""; include "C:/xampp/htdocs/training/php/phpForms/myFunctions/functions.php"; echo $_SERVER["PHP_SELF"]; if (isset($_GET["operand1"]) && isset($_GET["operand2"]) && isset($_GET["operator"])) { $result = calculator($_GET["operator"], $_GET["operand1"], $_GET["operand2"]); } ?> <html> <form action="add.php" method="get"> <input type="text" name="operand1" /> <input type="text" name="operand2" /> <input type="text" name="operator" /> <button type="submit">Calculate</button> </form> <div> Sum: <?php echo $result; ?> </div> </html>
function cac_pre($s) { global $func, $pi; $pi = '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128'; $func = array('radians', 'degrees', 'root', 'abs', 'avedev', 'count', 'analyze', 'average', 'stdevp', 'stdev', 'round', 'floor', 'ceil', 'sqrt', 'log', 'sum', 'pow', 'exp', 'mod', 'sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'ln', 'c'); $func_s = array('radians', 'degrees', 'analyze', 'round', 'floor', 'ceil', 'sqrt', 'log', 'exp', 'sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'ln'); if ($s == '') { return 0; } $m = $_POST['calculator']; if ($m == '') { $m = 'x'; } $m = strtolower($m); $m = str_replace('x', '(' . $s . ')', $m); $m = str_replace('((' . $s . '))', '(' . $s . ')', $m); $m = preg_replace('/(\\d)\\s+(-?)(\\d)/', '\\1,\\2\\3', $m); $m = preg_replace('/(\\d)\\s+(-?)(\\d)/', '\\1,\\2\\3', $m); //Dont delete! $m = preg_replace('/\\(\\s+(-?)(\\d)/', '(\\1\\2', $m); $m = preg_replace('/(\\d)\\s+\\)/', '\\1)', $m); $m = preg_replace('/\\s/', '', $m); $m = preg_replace('/(\\d)\\.(\\D)/', '\\1.0\\2', $m); $m = preg_replace('/pi/i', '(' . $pi . ')', $m); if (ereg('[<>]', $m)) { addmsg(ERR, 'Wrong input'); return $s; } $f = 0; for ($i = 0; $i < strlen($m); ++$i) { if (substr($m, $i, 1) == '(') { ++$f; } elseif (substr($m, $i, 1) == ')') { $f--; } if ($f < 0) { addmsg(ERR, 'Wrong parentheses order'); return $s; } } if ($f > 0) { addmsg(ERR, 'Lone parenthesis'); return $s; } $f = 0; $p = 0; $t = ''; $temp = array(); for ($i = 0; $i < strlen($m); ++$i) { $e = substr($m, $i, 1); if ($e == '|') { if ($f == 0) { $t .= '{'; $f = 1; } else { $t .= '}'; $f = 0; } } else { $t .= $e; if ($e == '(') { array_push($temp, $f); $f = 0; } elseif ($e == ')') { if ($f == 1) { addmsg(ERR, 'Wrong |'); break; } $f = array_pop($temp); } } } $m = $t; $m = preg_replace('/(-?\\d+(\\.\\d)?\\d*(e\\+\\d|e-\\d|e)?\\d*)/', '<\\1>', $m); $m = preg_replace('/([)}!>])<-/', '\\1-<', $m); $m = preg_replace('/([*\\/])-/', '\\1<-1>*', $m); $m = preg_replace('/([>)}!])([<({])/', '\\1*\\2', $m); while (preg_match('/(\\+-|--|-\\+|\\+\\+)/', $m)) { $m = str_replace('+-', '-', $m); $m = str_replace('-+', '-', $m); $m = str_replace('++', '+', $m); $m = str_replace('--', '+', $m); } if (preg_match('/[+\\-*\\/\\^%,]{2,}/', $m)) { addmsg(ERR, 'Wrong operator'); } for ($i = 0; $i < count($func); ++$i) { $m = preg_replace('/([!)>}])' . $func[$i] . '/', '\\1*' . $func[$i], $m); } for ($i = 0; $i < count($func_s); ++$i) { $m = preg_replace('/' . $func_s[$i] . '(<[^>]*>)/', $func_s[$i] . '(\\1)', $m); } $t = $m; for ($i = 0; $i < count($func); ++$i) { $t = str_replace($func[$i] . '(', '', $t); } $t = preg_replace('/<[^>]*>/i', '', $t); $t = preg_replace('/[,()+\\^\\-*\\/!{}\\[\\]%]/', '', $t); if (strlen($t) > 0) { addmsg(ERR, 'Invalid input', 1); } $m = calculator('(' . $m . ')'); $m = substr($m, 1, strlen($m) - 2); $m = preg_replace('/\\.0+$/', '', $m); $m = preg_replace('/\\.(.*[^0])0+$/', '.\\1', $m); return $m; }