Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

計算機作業-陳昱廷 #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions calculator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
body{
background-color: darkkhaki
}

#all{
margin: auto;
width: 30%;

}
#cback{
width: 150px;
margin: 0 auto;
background-color: #ffffff;
border-radius: 10px;
padding: 20px;
text-align: center;
}
55 changes: 55 additions & 0 deletions calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/calculator.css" >
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="js/calculator.js"></script>
<title>我的計算機</title>
</head>
<body>
<div id=all>
<h2>mycalculator</h2>
<h4>with jquery</h4>
<form>
<div id=cback>
<input type="text" id="showResBox" readonly/>
<br/>
<input type="button" value="7" onClick="getNum('7')">
<input type="button" value="8" onClick="getNum('8')">
<input type="button" value="9" onClick="getNum('9')">
<input type="button" value="+" onClick="cal('+')">
<br/>

<input type="button" value="4" onClick="getNum('4')">
<input type="button" value="5" onClick="getNum('5')">
<input type="button" value="6" onClick="getNum('6')">
<input type="button" value="-" onClick="minus('-')">
<br/>

<input type="button" value="3" onClick="getNum('3')">
<input type="button" value="2" onClick="getNum('2')">
<input type="button" value="1" onClick="getNum('1')">
<input type="button" value="×" onClick="cal('*')">
<br/>

<input type="button" value="(" >
<input type="button" value="0" onClick="getNum('0')">
<input type="button" value=")" >
<input type="button" value="÷" onClick="cal('/')">
<br/>

<input type="button" value="C" onClick="clearRes()">
<input type="button" value="." onClick="cal('.')">
<input type="button" value="←" onClick="del()">
<input type="button" value="=" onClick="equal()">


</div>
</form>
</div>

</body>
</html>
48 changes: 48 additions & 0 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

function getNum(num){
$("#showResBox").val(function(i,val){
return val + num
})
}
function clearRes(){
document.getElementById("showResBox").value = "";
}

function cal(calSymbol){
var boxRes=$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);

if($("#showResBox").val()==""){
$("#showResBox").val("");
}else if(lastSym=="+"||lastSym=="-"||lastSym=="*"||lastSym=="/"||lastSym=="."){
$("#showResBox").val("");}
else{$("#showResBox").val(function(i,val){
return val + calSymbol
});
}
}

function minus(minuss){
var boxRes =$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);
if(lastSym=="+"||lastSym=="-"||lastSym=="*"||lastSym=="/"||lastSym=="."){
$("#showResBox").val("");
}else{$("#showResBox").val(function(i,val){
return val + minuss
});
}
}

function equal(){
var boxRes =$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);
document.getElementById("showResBox").value = eval(document.getElementById("showResBox").value);

}

function del(){
$("#showResBox").val(function(i,val){
return val.substr(0,val.length-1)
});
}

17 changes: 17 additions & 0 deletions css/calculator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
body{
background-color: darkkhaki
}

#all{
margin: auto;
width: 30%;

}
#cback{
width: 150px;
margin: 0 auto;
background-color: #ffffff;
border-radius: 10px;
padding: 20px;
text-align: center;
}
48 changes: 48 additions & 0 deletions js/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

function getNum(num){
$("#showResBox").val(function(i,val){
return val + num
})
}
function clearRes(){
document.getElementById("showResBox").value = "";
}

function cal(calSymbol){
var boxRes=$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);

if($("#showResBox").val()==""){
$("#showResBox").val("");
}else if(lastSym=="+"||lastSym=="-"||lastSym=="*"||lastSym=="/"||lastSym=="."){
$("#showResBox").val("");}
else{$("#showResBox").val(function(i,val){
return val + calSymbol
});
}
}

function minus(minuss){
var boxRes =$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);
if(lastSym=="+"||lastSym=="-"||lastSym=="*"||lastSym=="/"||lastSym=="."){
$("#showResBox").val("");
}else{$("#showResBox").val(function(i,val){
return val + minuss
});
}
}

function equal(){
var boxRes =$("#showResBox").val();
var lastSym=boxRes.substr(boxRes.length-1);
document.getElementById("showResBox").value = eval(document.getElementById("showResBox").value);

}

function del(){
$("#showResBox").val(function(i,val){
return val.substr(0,val.length-1)
});
}