This time, in order to let us review the page Jump in the web, the boss gave us an unworthy task
A big copy of the world's code depends on how you copy it
First of all, the algorithm I think about is not a stack, but a simple one (actually, it's very complex, but I just need to know that someone else has done it well... The legendary CV method is good? Emmm, when you know there's such an algorithm but you can't write it by yourself but you can use it by yourself, it's equivalent to... You can...)
Needless to say, it's a collected regular tool class, which supports bracket operation
package pers.cal.util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * The bracketed version of calculator implemented by regular expression * @author Administrator * */ public class UtilArithmetic { /** * Evaluate expressions that may contain parentheses * * @param s * @return */ public static String calcu(String s) { s = s.replaceAll(" +", "");// Eliminate the space. The space can be one or more. Replace it when you find it // \(left bracket \ \) right bracket means there is no other bracket within the bracket, and then it is a subgroup e surrounded by brackets Pattern pa = Pattern.compile("\\(([^\\(\\)]*)\\)"); while (true) { Matcher ma = pa.matcher(s); if (ma.find() == false) break; // Calculate the parentheses first, then eliminate them s = s.replace(ma.group(), calcuNoParen(ma.group(1))); } return calcuNoParen(s); } /** * Calculate operations without parentheses * * @param s2 * @return */ public static String calcuNoParen(String s2) { if (s2.length() < 1) return s2; // Eliminate multiplication first Pattern pat = Pattern.compile("([0-9]+)\\*([0-9]+)");//0-9 one or more, multiply while (true) { Matcher mc = pat.matcher(s2); if (mc.find() == false) break; int res = Integer.parseInt(mc.group(1)) * Integer.parseInt(mc.group(2)); // Calculate the result, and remove the brackets. Note that replaceAll cannot be used here s2 = s2.replace(mc.group(), res + ""); } // Then eliminate all addition and subtraction from left to right pat = Pattern.compile("([0-9]+)([\\+\\-])([0-9]+)");// 0-9 one or more, add or subtract while (true) { Matcher mc = pat.matcher(s2); if (mc.find() == false) break; int res = 0; switch (mc.group(2)) { case "+": res = Integer.parseInt(mc.group(1)) + Integer.parseInt(mc.group(3)); break; case "-": res = Integer.parseInt(mc.group(1)) - Integer.parseInt(mc.group(3)); break; } // Calculate the result, and remove the brackets. Note that replaceAl cannot be used here s2 = s2.replace(mc.group(), res + ""); } return s2; } }
When calling, just use calcu()
Then the corresponding foreground jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Be based on Servlet+jsp Of Web Calculator project</title> <link href="./css/index.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> var cleartext = false;//Set identity value function getNum(num) { //alert(num); var result = document.getElementById("result"); if (cleartext) { result.value = ""; //cleartext = false; } result.value += num; } function getResult() { var result = document.getElementById("result"); //result.value=result.value+"="+eval(result.value); //next is the str we need result.value = result.value; //this result.value is the str we need //alert(result.value); //cleartext = true; / / change the ID after calculating the result } </script> </head> <body> <div> <span>Kim Sheng Chung ®</span> <form action="calculator.do" method="post" name="From" onclick="return getResult"> <div> <input type="text" name="result" id="result" size="33" readonly=readonly value="${ count}" > </div> <div> <button type="button" value="all-clear" onclick="document.getElementById('result').value=''">AC</button> <button type="button" value="9" onclick="getNum('(')">(</button> <button type="button" value="9" onclick="getNum(')')">)</button> <button type="button" value="+" onclick="getNum('+')">+</button> <button type="button" value="7" onclick="getNum(7)">7</button> <button type="button" value="8" onclick="getNum(8)">8</button> <button type="button" value="9" onclick="getNum(9)">9</button> <button type="button" value="-" onclick="getNum('-')">-</button> <button type="button" value="4" onclick="getNum(4)">4</button> <button type="button" value="5" onclick="getNum(5)">5</button> <button type="button" value="6" onclick="getNum(6)">6</button> <button type="button" value="*" onclick="getNum('*')">×</button> <button type="button" value="1" onclick="getNum(1)">1</button> <button type="button" value="2" onclick="getNum(2)">2</button> <button type="button" value="3" onclick="getNum(3)">3</button> <button type="button" value="/" onclick="getNum('/')">÷</button> <button type="button" value="0" onclick="getNum(0)">0</button> <button type="button" value="." onclick="getNum('.')">.</button> <button value="=" onclick="getResult('.')">=</button> </div> </form> </div> </body> </html>
His css
html { font-size: 62.5%; box-sizing: border-box; margin: 0; padding: 0; box-sizing: inherit } body { width: 100vw; min-height: 100vh; background-color: #B0C4DE; display: flex; justify-content: center; align-items: center; } .calculator { background: #1d1e22; padding:2.8rem .64rem .64rem; color: white; border-radius: .5rem; box-shadow: 0 .3rem 3rem .1rem rgba(0,0,0,0.6); position: relative; min-width: 40rem; } .calculator-display { font-size: 5rem; height: 80px; padding: 0 20px; background-color: #1d1e22; color: #fff; display: flex; align-items: center; justify-content: flex-end; } #result{ color:white; background-color:#003300; width:100%; height:50%; font-size:33%; text-align:right; } button { height: 60px; border-radius: 3px; border: 1px solid #c4c4c4; font-size: 2rem; background-color: #fff; } .calculator-keys { display: grid; grid-gap: 2rem; padding: 2rem 1.36rem; background-color: #fff; } .equal-sign { grid-row: 5 / span 1; grid-column: 3 / 5; height: 100%; } .copyRight{ text-align: center; display:block; color:black; font-weight:bolder; font-size: 33px; }
Look like this
El expression used
Related jar packages
xml configuration
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Calculation</display-name> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>pers.cal.servlet.CalculationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/calculator.do</url-pattern> </servlet-mapping> </web-app>
Corresponding Servlet
package pers.cal.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import pers.cal.util.UtilArithmetic; public class CalculationServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String result = req.getParameter("result"); // req.setAttribute("result", "admin"); String finalRes = result; result = finalRes+UtilArithmetic.calcu(result); HttpSession session = req.getSession(); session.setAttribute("count",result); System.out.println(result); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
Finally, the structure
Good night.
Have a good dream
おやすみ~