<?php
$input = "1+2*3-8/2+4*4-6/3";
$tester = '#[\d\+\-*/\s]#';
echo "Формат неверен";
}
}
echo "Вычисляем значение выражения: $input\n";
$devider = '#(\+|-|\*|=|/)#';
$parts = preg_split($devider, $input,0, PREG_SPLIT_DELIM_CAPTURE
);
for ($i=0; $i<count($parts); $i++) { if ($parts[$i] == "*") {
echo "Выполняем умножение {$parts[$i-1]} * {$parts[$i+1]}\n";
$temp = $parts[$i-1] * $parts[$i+1];
$parts[$i-1] = $temp;
}
}
for ($i=0; $i<count($parts); $i++) { if ($parts[$i] == "/") {
echo "Выполняем деление {$parts[$i-1]} / {$parts[$i+1]}\n";
$temp = $parts[$i-1] / $parts[$i+1];
$parts[$i-1] = $temp;
}
}
for ($i=0; $i<count($parts); $i++) { if ($parts[$i] == "+") {
echo "Выполняем сложене {$parts[$i-1]} + {$parts[$i+1]}\n";
$temp = $parts[$i-1] + $parts[$i+1];
$parts[$i-1] = $temp;
}
}
for ($i=0; $i<count($parts); $i++) { if ($parts[$i] == "-") {
echo "Выполняем вычетание {$parts[$i-1]} - {$parts[$i+1]}\n";
$temp = $parts[$i-1] - $parts[$i+1];
$parts[$i-1] = $temp;
}
}
PD9waHAKZXJyb3JfcmVwb3J0aW5nKC0xKTsKJGlucHV0ID0gIjErMiozLTgvMis0KjQtNi8zIjsKCiR0ZXN0ZXIgPSAnI1tcZFwrXC0qL1xzXSMnOwpmb3IgKCRpPTA7ICRpPG1iX3N0cmxlbigkaW5wdXQpOyAkaSsrKSB7CglpZiAoIXByZWdfbWF0Y2goJHRlc3RlciwgbWJfc3Vic3RyKCRpbnB1dCwgJGksIDEpKSkgewoJCWVjaG8gItCk0L7RgNC80LDRgiDQvdC10LLQtdGA0LXQvSI7CgkJZXhpdCgpOwoJfSAKfQoKZWNobyAi0JLRi9GH0LjRgdC70Y/QtdC8INC30L3QsNGH0LXQvdC40LUg0LLRi9GA0LDQttC10L3QuNGPOiAkaW5wdXRcbiI7CgokZGV2aWRlciA9ICcjK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