在这种情况下,你基本上需要自己实现计算器 - 我曾经在一次工作面试中遇到过这个问题,所以这是我的代码。记住,对我来说这真的是遗留的东西,但我想它可能会给你一些启示:
<?php
if(isset($_POST['inp'])) {
$time_start = microtime(true);
$inp = preg_replace(array('/\s+/', '/Pi/', '/e/', '/T/', '/G/', '/M/', '/k/', '/m/', '/u/', '/n/', '/p/', '/f/'),
array('', M_PI, exp(1), '*'. 1e12, '*'. 1e9, '*'. 1e6, '*'. 1e3, '*'. 1e-3, '*'. 1e-6, '*'. 1e-9, '*'. 1e-12, '*'. 1e-15),
$_POST['inp']);
function rectify($exp, $mod = "+") {
$res = recCalc($exp);
debug("Pre rectify", $res);
if($mod == '-') {
$res *= -1;
}
debug("Post rectify", $res);
return $res;
}
function do_error($str) {
die($str);
return false;
}
function recCalc($inp) {
debug("RecCalc input", $inp);
$p = str_split($inp);
$level = 0;
foreach($p as $num) {
if($num == '(' && ++$level == 1) {
$num = 'BABRAX';
} elseif($num == ')' && --$level == 0) {
$num = 'DEBRAX';
}
$res[] = $num;
}
if($level != 0) {
return do_error( 'Chyba: špatný počet závorek');
}
$res = implode('', $res);
$res = preg_replace('#([\+\-]?)BABRAX(.+?)DEBRAX#e', "rectify('\\2', '\\1')", $res);
debug("After parenthesis proccessing", $res);
preg_match_all('#[+-]?([^+-]+)#', $res, $ar, PREG_PATTERN_ORDER);
for($i = 0; $i <count($ar[0]); $i++) {
$last = substr($ar[0][$i], -1, 1);
if($last == '/' || $last == '*' || $last == '^' || $last == 'E') {
$ar[0][$i] = $ar[0][$i].$ar[0][$i+1];
unset($ar[0][$i+1]);
}
}
$result = 0;
foreach($ar[0] as $num) {
$result += multi($num);
}
debug("RecCalc output", $result);
return $result;
}
function multi($inp) {
debug("Multi input", $inp);
$inp = explode(' ', ereg_replace('([\*\/\^])', ' \\1 ', $inp));
foreach($inp as $va) {
if($va != '*' && $va != '/' && $va != '^') {
$v[] = (float)$va;
} else {
$v[] = $va;
}
}
$inp = $v;
$res = $inp[0];
for($i = 1; $i< count($inp); $i++) {
if($inp[$i] == '*') {
$res *= $inp[$i + 1];
} elseif($inp[$i] == '/') {
if($inp[$i + 1] == 0) do_error('Dělení nulou');
$res /= $inp[$i + 1];
} elseif($inp[$i] == '^') {
$res = pow($res, $inp[$i + 1]);
}
}
debug("Multi output", $res);
return $res;
}
function debug($msg, $var) {
if(isset($_POST['out']) && $_POST['out'] == '1') {
echo "\n".$msg.": ".$var;
}
}
echo '<pre>';
if(eregi('(^[\*\/\+\^])|[a-dg-z \?<>;:"\'\\|\}\{_]|([\*\/\+\-\^]$)', $inp)) {
do_error('Nalezen neplatný či nesmyslný znak. Překontorlujte si prosím syntax.');
}
$result = recCalc($inp);
$time_end = microtime(true);
$time = ($time_end - $time_start) *1000;
$time .= 'ms';
echo "\n<strong>".$result."</strong>";
debug('Execution time', $time);
echo '</pre>';
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Calculator</title>
<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
background:
margin: 0;
padding: 0;
text-align: center;
color:
}
.oneColElsCtr
width: 46em;
background:
margin: 0 auto;
border: 1px solid
text-align: left;
}
.oneColElsCtr
padding: 0 20px;
}
.noshow {
display: none;
}
-->
</style>
<link rel="stylesheet" href="styles/COHEN_style.css"/>
<script src="scripts/spry/SpryData.js"></script>
<!--<script src="scripts/spry/xpath.js"></script>-->
<script src="scripts/spry/SpryUtils.js"></script>
<script src="scripts/spry/SpryDOMUtils.js" type="text/javascript"></script>
<script src="scripts/spry/SpryCollapsiblePanel.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function updateResultDiv(req)
{
Spry.Utils.setInnerHTML('result', req.xhRequest.responseText);
}
function submitit() {
if(document.getElementById('auto').checked) {
Spry.Utils.submitForm(document.getElementById('calc'), updateResultDiv);
}
}
-->
</script>
<link href="scripts/spry/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
</head>
<body class="oneColElsCtr">
<div id="container">
<div id="mainContent">
<h1>Calculator</h1>
<form method="post" id="calc" onsubmit="return Spry.Utils.submitForm(document.getElementById('calc'), updateResultDiv);">
<input type="text" name="inp" size="80" value="" onkeyup="submitit()"/><br />
<input type="checkbox" value="1" name="out" /><label>Debug</label> <input onclick="Spry.$$('#submit').toggleClassName('noshow');" checked="checked" type="checkbox" value="1" id="auto" /><label for="auto">Count automatically</label><br />
<input class="noshow" value="Počítej" type="submit" id="submit" />
</form>
<div id="result">
</div>
</div>
</div>
</body>
</html>
我写的时候进行了检查,所以抱歉如果有奇怪的注释。