Last active
July 9, 2021 01:38
-
-
Save black-black-cat/60711f72315f4a2734d3359cdd2a7f8e to your computer and use it in GitHub Desktop.
arith 四则运算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
<h1>Hello world!</h1> | |
<input id="in" type="text"> | |
<div id="res"></div> | |
<script src="https://raw.githack.com/marlun78/arith/master/arith.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let input = document.querySelector('#in') | |
let result = document.querySelector('#res') | |
function breakStr(arr) { | |
let v = '' | |
let arr2 = [] | |
for (let i = 0; i < arr.length; i++) { | |
let char = arr[i] | |
if (char == '*' || char == '/' || char == '+' || char == '-') { | |
arr2.push(v) | |
v = '' | |
arr2.push(char) | |
} else { | |
v += char | |
} | |
if (i === arr.length - 1) { | |
arr2.push(v) | |
v = '' | |
} | |
} | |
return arr2 | |
} | |
input.addEventListener('blur', () => { | |
let arr = input.value.replace(' ', '').split('') | |
let memo = arr[0] | |
let arr2 = breakStr(arr) | |
console.log(arr2) | |
let res = arith(arr2[0]) | |
for (let i = 1; i < arr2.length; i+=2) { | |
let v = arr2[i] | |
let x = arr2[i+1] | |
if (v == '+') { | |
res = res.add(x) | |
} | |
if (v == '-') { | |
res = res.subtract(x) | |
} | |
if (v == '*') { | |
res = res.multiply(x) | |
} | |
if (v == '/') { | |
res = res.divide(x) | |
} | |
} | |
result.innerText = res.value() | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment