全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
<style>
*:focus {
outline: none;
}
</style>
<body>
<div class="h-screen bg-gray-100 py-6 flex justify-center items-center w-full">
<div class="w-full min-h-min md:w-1/2 md:h-1/2 m-9 relative">
<div class="absolute inset-0 bg-gradient-to-r from-green-300 to-blue-300 shadow-lg transform md:-skew-y-6 sm:skew-y-0 sm:-rotate-6 sm:rounded-3xl rounded-md"></div>
<div class="p-8 relative min-h-min z-10 bg-white rounded-xl">
<div class="uppercase text-black text-3xl md:text-base">INPUT expression</div>
<div class="mt-5">
<input type="text" class="px-2 ring-2 py-3 md:py-0 rounded-md w-full md:w-1/2" id="inputExpress" />
<input type="button" value="S t a r t" class="uppercase mt-6 md:mt-0 md:ml-3 px-3 md:py-1 py-2 w-full md:w-min rounded-md cursor-pointer bg-blue-400 text-white" id="inputInput" />
</div>
<div class="">
<div class="uppercase text-black text-3xl md:text-base mt-6">out result</div>
<textarea class="resize-none border ring-2 m-2 w-full h-36 rounded-md p-2 box-border" id="textareaInput"></textarea>
</div>
</div>
</div>
</div>
</body>
<script>
let printf = () => {
let inputText = document.getElementById('textareaInput')
printf= {
log: (str) => {
inputText.value += str + '\n'
},
clear: () => {
inputText.value = ''
inputText.value = ''
}
}
}
function getResult(strExpression) {
let isValid = true,
nowI = 0
E()
return isValid
function E() {
printf.log('E=>TG')
T()
G()
}
function T() {
printf.log('T=>FS')
F()
S()
}
function G() {
printf.log('G=>ε')
if (strExpression[nowI] == '+') {
nowI++
printf.log('G >+TG')
T()
G()
} else if (strExpression[nowI] == '-') {
nowI++
printf.log('G >-TG')
T()
G()
}
}
function F() {
if (strExpression[nowI] == '(') {
nowI++
E()
if (strExpression[nowI] == ')') {
nowI++
printf.log('F=>(E)')
} else flag = false
} else if (strExpression[nowI] == 'i') {
printf.log('F=>i')
nowI++
} else isValid = false
}
function S() {
printf.log('S=>ε')
if (strExpression[nowI] == '*') {
nowI++
F()
S()
} else if (strExpression[nowI] == '/') {
nowI++
F()
S()
}
}
}
window.onload = () => {
let inputExpress = document.getElementById('inputExpress'),
inputInput = document.getElementById('inputInput')
printf()
inputInput.onclick = () => {
printf.clear()
printf.log(getResult(inputExpress.value) ? 'Accepted' : 'error')
}
}
</script>
</html>
效果: