正常写计算器肯定要转化成逆波兰表达式,然后再计算,但是考虑到eval函数我想偷个懒,确实可以解决绝大多数计算器问题,然而问题要求有次方......
还得去写逆波兰表达式...
虽然真正接触js还很短暂,但是我觉得他还是比较有意思的,尤其是搭oj的经历让我深刻地体会到,前台做的漂亮是王道啊。
今天在路上,作为一个宅男猛然听到喊自己学长瞬间感到自己好老....但是看着自己写的东西被学弟学妹用着还是挺开心的~~
虽然自己大一大二也确实浪,但是想想人也不可能一心一意在一件事情太久,更何况生活中不应当只有代码(如果能有个妹子就perfect了哈哈)
这个程序处理不了次方,以上。
<html>
<head>
<title>
我的计算器
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var s="";//显示字符串
var s2="";//执行字符串
function myfunction(t){
if (t=='q')
{
s="";
s2="";
document.getElementById("show").value=s;
return;
}
if (t=='=')
{
s=eval(s2);
document.getElementById("show").value=s;
return;
}
if (t=='^')
{
s=s+'^';
s2=s2+'Math.pow';
document.getElementById("show").value=s;
return;
}
if (t=='sin')
{
s=s+'sin';
s2=s2+'Math.sin';
document.getElementById("show").value=s;
return;
}
if (t=='cos')
{
s=s+'cos';
s2=s2+'Math.cos';
document.getElementById("show").value=s;
return;
}
s=s+t;
s2=s2+t;
document.getElementById("show").value=s;
}
</script>
<style type="text/css">
.second{
padding-top:50px;
background-image:url(bg2.png);
background-size:584px 600px;
background-repeat:no-repeat;
background-position:402px 60px;
font-family: "microsoft yahei",Helvetica,sans-serif;
}
#third{
padding-top:4px;
padding-bottom:5px;
font-family: "microsoft yahei",Helvetica,sans-serif;
}
#show{
font-size:14px;
text-align:right;
padding: 0px 5px;
font-family: "microsoft yahei",Helvetica,sans-serif;
}
}
</style>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body class="first">
<div class="second">
<h2 style="text-align:center;color:red">科学计算器</h2>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<textarea cols="79" rows="1" id="show">
</textarea>
</div>
</div>
<div class="row" >
<div class="col-md-1 col-md-offset-3" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" οnclick="myfunction('7')">7</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" οnclick="myfunction('8')">8</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" οnclick="myfunction('9')">9</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" id="third" οnclick="myfunction('/')">/</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" id="third" οnclick="myfunction('(')">(</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary" style="width:90px;height:40px;" id="third" οnclick="myfunction('^')">^</button></div>
</div>
<div class="row">
<div class="col-md-1 col-md-offset-3" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" id="third" οnclick="myfunction('4')">4</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('5')">5</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('6')">6</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('*')">*</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;"οnclick="myfunction(')')">)</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('q')">清零</button></div>
</div>
<div class="row">
<div class="col-md-1 col-md-offset-3" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('1')">1</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('2')">2</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('3')">3</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('-')">-</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('=')" >=</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('sin')">sin</button></div>
</div>
<div class="row">
<span style="text-align:center">
<div class="col-md-3 col-md-offset-3" id="third"><button type="button" class="btn btn-primary"style="width:270px;height:40px;" id="third" οnclick="myfunction('0')">0</button></div></span>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('+')">+</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('.')">.</button></div>
<div class="col-md-1" id="third"><button type="button" class="btn btn-primary"style="width:90px;height:40px;" οnclick="myfunction('cos')">cos</button></div>
</div>
</div>
</div>
</body>
</html>