利用php+mysql來做一個功能強大的在線計算器
有天在努力的搜索計算器,發現都是JavaScript,而且要一個個地點擊,並且不能記錄,輸入計算式子時容易出錯,於是就想了想該怎樣才能讓它好用點呢,能夠用鍵盤直接輸入。就跟隨小編去看看吧!
代碼還超級簡單 就做了一個ajax的'計算器
有式子錯誤提示 還可以時時顯示輸入的式子
演示地址:
自己去下載
複製代碼 代碼如下:
<?php
//鏈接數據庫的
$db=mysql_connect("localhost","root","123");
header("Content-Type:text/html;charset=GB2312");
$str=iconv('utf-8','gbk',trim($_POST['t_ask']));
$str=str_replace(" ","",str_replace("rn","",$str));
$str=str_replace("(","(",$str);
$str=str_replace(")",")",$str);
/*三角函數替換*/
$str=preg_replace("/sin((.*))/is","sin(${1}*pi()/180)",$str);//替換sin
$str=preg_replace("/cos((.*))/is","cos(${1}*pi()/180)",$str);//替換cos
$str=preg_replace("/tan((.*))/is","tan(${1}*pi()/180)",$str);//替換tan
$str=preg_replace("/cot((.*))/is","1/tan(${1}*pi()/180)",$str);//替換餘切
$str=preg_replace("/asin((.*))/is","asin(${1}/pi()*180)*180/pi()",$str);//反正弦
$str=preg_replace("/acos((.*))/is","acos(${1}/pi()*180)*180/pi()",$str);//反餘弦
$str=preg_replace("/atan((.*))/is","atan(${1}/pi()*180)*180/pi()",$str);//替換反正切
$sql="select ".$str ;
$res=mysql_query($sql,$db) or die('<font color=red>你輸入的式子有錯誤</font>');
$rs=mysql_fetch_array($res);
echo $rs[0];
?>
複製代碼 代碼如下:
<html>
<head>
<title>手寫輸入計算器</title>
<meta name="keywords" content="在線計算器,輸入式子直接計算,手寫計算器" />
<meta name="description" content="在線計算器,手寫輸入計算器,輸入式子直接計算" />
<script src="" language="javascript"></script>
<script language="javascript">
$(function(){
$("#t_ask")p(function(){
$(
"",
{
t_ask : $("#t_ask")()
},function(data,textStatus)
{
$("#res")(data);
}
);
});
});
</script>
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" height="40"><h2>手寫輸入計算器</h2></td>
</tr>
</table>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="34" align="center">在這裏你可以手寫式子計算哦,還不快試試! <a href="">返回首頁</a></td>
</tr>
</table>
<form method="post">
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="27%" align="right">計算式子:</td>
<td width="73%"><textarea name="t_ask" cols="60" rows="6" id="t_ask"></textarea></td>
</tr>
<tr>
<td height="23" align="right">=</td>
<td><p id="res"></p></td>
</tr>
<tr>
<td height="31" align="right"></td>
<td><input type="button" name="tj" id="tj" value="按鈕" />
<input type="reset" name="qc" id="qc" value="重置" /></td>
</tr>
</table>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><p>説明:<br />
三角函數:
<p>sin(60)正弦 cos(60)餘弦 tan(60)正切 cot(60)餘切
<p>asin(0.5)反正弦 acos(0.5)
反餘弦 atan(0.5)反正切
<p>abs(-1)=1絕對值 ceil(0.1)=1進一
<p>指數對數
<p>exp(float arg)// 計算 <strong>e</strong>(自然對數的底)的指數
<p>log(10,100)=2//自然對數 pow(2,4)=16 指數 sqrt(4)=2平方根
<p><br />
</td>
</tr>
</table>
</form>
</body>
</html>
-
PHP自動加載
很多的初學者在學習PHP的時候最早面對的問題之一就是require、include和require_once、include_once的相愛相殺。那麼在瞭解了它們相愛相殺的故事後,往往就開始使用起了框架。以下是關於框架的介紹,希望對大家有用,更多內容請關注應屆畢業生網!框架固然是幹活的好...
-
用PHP讀取文件的正確方法
處理諸如PHP之類的現代編程語言的樂趣之一就是有大量的選項可用。PHP可以輕鬆地贏得Perl的座右銘There‘smorethanonewaytodoit(並非只有一種方法可做這件事),尤其是在文件處理上。但是在這麼多可用的選項中,哪一種是完成作業的最佳工具?當然,實際答案取決於...
-
在PHP中使用隨機數的三個步驟
如何做一個隨機的出現的網頁,可是用rand()就是不行,聽説要初始化隨機數,可是怎麼初始化,請不吝賜教,謝謝!//第一步:初始化種子$seedarray=microtime();$seedstr=split("",$seedarray,5);$seed=$seedstr[0]*10000;//第二步:使用種子初始化隨機數發生器srand($seed);//...
-
PHP中Http協議post請求參數
本文給大家介紹PHP中Http協議post請求參數,具體內容如下所示,就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!WEB開發中信息基本全是在POST與GET請求與響應中進行,GET因其基於URL的直觀,易被我們瞭解,可POST請求因其信息的隱蔽,在...