「java解析表达式」java解析表达式语法优先级

博主:adminadmin 2022-11-24 19:27:05 83

今天给各位分享java解析表达式的知识,其中也会对java解析表达式语法优先级进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA 文本表达式解析成数学公式,计算出结果

正则表达分解字符串

然后运算

给你个例子。以前写的,现在忙没空给你写你这个了,public class CalStr { private String src;

/**

* constructor

*

* @param srcthe string(expression) to calculate

*/

public CalStr(String src) {

this.src = src;

}

/**

* calculate to get the result

*

* @return(double)result

*/

public double getResult() {

String postfix = getPostfix();

Stack stk = new Stack();

// System.out.println(postfix);

String parts[] = postfix.split( " + ");

double result = 0;

for (int i = 0; i parts.length; i++) {

char tmp = parts[i].charAt(0);

if (!isOperator(tmp)) {

stk.push(parts[i]);

} else {

double a = Double.parseDouble((String) stk.pop());

double b = Double.parseDouble((String) stk.pop());

// b is followed by a in the orignal expression

result = calculate(b, a, tmp);

stk.push(String.valueOf(result));

}

}

return result;

}

/**

* test if the character is an operator,such +,-,*,/

*

* @param opthe character to test

* @returntrue if op is an operator otherwise false

*/

private boolean isOperator(char op) {

return (op == '+ ' || op == '- ' || op == '* ' || op == '/ ');

}

/**

* calculate an expression such (a op b)

*

* @param anumber 1

* @param bnumber 2

* @param opthe operator

* @return(double)(a op b)

*/

public double calculate(double a, double b, char op) {

switch (op) {

case '+ ':

return a + b;

case '- ':

return a - b;

case '* ':

return a * b;

case '/ ':

return a / b;

}

return -1;

}

/**

* convert the suffix to postfix

*

* @returnthe postfix as a string

*/

private String getPostfix() {

Stack stk = new Stack();

String postfix = new String();

char op;

int i = 0;

while (i src.length()) {

if (Character.isDigit(src.charAt(i)) || src.charAt(i) == '. ') {

postfix += " ";

do {

postfix += src.charAt(i++);

} while ((i src.length())

(Character.isDigit(src.charAt(i))));

postfix += " ";

}

else {

switch (op = src.charAt(i++)) {

case '( ':

stk.push( "( ");

break;

case ') ':

while (stk.peek() != "( ") {

String tmp = (String) stk.pop();

postfix += tmp;

if (tmp.length() == 1 isOperator(tmp.charAt(0)))

postfix += " ";

}

stk.pop();

postfix += " ";

break;

case '+ ':

case '- ':

while ((!stk.empty()) (stk.peek() != "( ")) {

postfix += stk.pop() + " ";

}

stk.push(String.valueOf(new Character(op)));

break;

case '* ':

case '/ ':

while ((!stk.empty())

((stk.peek() == "* ") || (stk.peek() == "/ "))) {

postfix += stk.pop() + " ";

}

stk.push(String.valueOf(new Character(op)));

break;

}

}

}

ListIterator it = stk.listIterator(stk.size());

while (it.hasPrevious())

postfix += it.previous() + " ";

return postfix.trim().replaceAll( " +\\. ", ". ");

}

/**

* main function

*

* @param args

*/

public static void main(String args[]) {

System.out.println(new CalStr( "((1.5+6.000)*9+9.36)*(8+9-8*8+8*7) ")

.getResult());

}

}

java正则表达式解析Mysql数据库错误日志

System.out.println( new ReadSingleLineNumber().test2());

}

MySqlLog test2(){

String log = "2018-03-21T13:46:01.185376Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).";

String[] head = log.substring(0, log.indexOf(']') + 1).split(" ");

return new MySqlLog(head[0].trim(), Integer.parseInt(head[1].trim()), head[2].trim().substring(head[2].trim().indexOf('[') + 1, head[2].trim().indexOf(']')), log.substring(log.indexOf(']') + 2).trim());

}

class MySqlLog{

String time;

int index;

String level;

String msg;

public MySqlLog(String time, int index, String level, String msg) {

this.time = time;

java解析字符串 算术表达式求值

Java 有一个jar包 叫 groovy

groovy可以实现动态执行 String格式的算数表达式

public static void main(String[] args) throws Exception{

    String str1 = "1 + 2 * 3"; //表达式1  固定表达式

    GroovyShell groovyShell = new GroovyShell();

    Object value = groovyShell.evaluate(str1);

    System.out.println(value);

    String str2 = "A + B * C"; //表达式2 动态表达式

    Binding binding = new Binding();

    binding.setVariable("A",1); //表达式中 所有的A替换为1

    binding.setVariable("B",2);//表达式中 所有的B替换为2

    binding.setVariable("C",3);//表达式中 所有的C替换为3

    GroovyShell groovyShell2 = new GroovyShell(binding);

    Object value2 = groovyShell2.evaluate(str2); //str2 实际表达式为 1 + 2 * 3

    System.out.println(value2);

}

java解析表达式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java解析表达式语法优先级、java解析表达式的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。