파서 직접 만들면 능력자고 난 귀찮으니 라이브러리 쓰는 인간임. 구글링해보면 관련 자료가 많다.
문자열로 수학 표현식 평가하는 방법?
https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form
보니까 형님들이 다양한 라이브러리 쓰던데 나는 핫한 graalvm을 쓰기로 함.
입력받은 text를 eval 하면 끝
try (Context context = Context.create("js")) {
Value result = context.eval("js", text.getText());
text_1.setText(String.valueOf(result.asInt()));
} catch (Exception ex) {
ex.printStackTrace();
}
결과는 다음과 같음.
오늘의 뻘짓 끝. 케케케
진공관으로 하던걸 21세기 컴퓨터 소프트웨어로 재구현하다니 혁신적이네요