@anonymous/

StingyLargeState

PHP (Legacy)

No description

fork
loading
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

error_reporting(-1);
mb_internal_encoding('utf-8');

$input = '243+6743-78*2*0.5=';
$inputLenght = mb_strlen($input);

$number = 0;
$result = 0;
$op = '';

if (preg_match('~/0~', $input)) {
	echo "На ноль делить нельзя!\n";
	exit();
}

for ($i=0; $i < $inputLenght; $i++) { 
	$char = mb_substr($input, $i, 1);
	if ($char == '*' || $char == '+' || $char == '-' || $char == '=' || $char == '/') {
		echo "op = [{$op}], result = [{$result}], number = [{$number}], char = [{$char}]\n";
		if (!empty($op)) {
			switch ($op) {
					case '*':
						$result = $result * $number;
						break;
					case '+':
						$result = $result + $number;
						break;
					case '-':
						$result = $result - $number;
						break;
					case '/':
						$result = $result / $number;
				}	
		} else {
			$result = $number;
		}
		$number = 0;
		$op = $char;
	}
	elseif (is_numeric($char)) {
		$number = floatval($number . $char);
	} elseif ($char == '.') {
		$number = $number . $char;
	} else {
		echo "Неверный символ: '{$char}'\n";
	}
}
echo "Ответ: {$input}{$result}\n";
?>
PHP 7.0.8 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7