用于复杂数学表达式的高级标记器 [英] Advanced tokenizer for a complex math expression
问题描述
我想标记一个由整数、浮点数、运算符、函数、变量和括号组成的字符串.下面的例子应该能说明问题的本质:
I would like to tokenize a string that consists of integers,floats, operators, functions, variables and parentheses. The following example should brighten the essence of problem:
当前状态:
String infix = 4*x+5.2024*(Log(x,y)^z)-300.12
期望状态:
String tokBuf[0]=4
String tokBuf[1]=*
String tokBuf[2]=x
String tokBuf[3]=+
String tokBuf[4]=5.2024
String tokBuf[5]=*
String tokBuf[6]=(
String tokBuf[7]=Log
String tokBuf[8]=(
String tokBuf[9]=x
String tokBuf[10]=,
String tokBuf[11]=y
String tokBuf[12]=)
String tokBuf[13]=^
String tokBuf[14]=z
String tokBuf[15]=)
String tokBuf[16]=-
String tokBuf[17]=300.12
将不胜感激所有提示和解决方案.
All tips and solutions would be appreciated.
推荐答案
使用 Java 流标记器.界面有点奇怪,但习惯了:
Use the Java stream tokenizer. The interface is a bit strange but one gets used to it:
http://docs.oracle.com/javase/7/docs/api/java/io/StreamTokenizer.html
解析为请求的字符串列表的示例代码(您可能希望直接使用分词器或至少使用对象列表,以便您可以将数字直接存储为 Double):
Example code that parses to the requested String list (you probably want to use the tokenizer directly or at least use an Object list so you can store numbers directly as Double):
public static List<String> tokenize(String s) throws IOException {
StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
tokenizer.ordinaryChar('-'); // Don't parse minus as part of numbers.
tokenizer.ordinaryChar('/'); // Don't treat slash as a comment start.
List<String> tokBuf = new ArrayList<String>();
while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch(tokenizer.ttype) {
case StreamTokenizer.TT_NUMBER:
tokBuf.add(String.valueOf(tokenizer.nval));
break;
case StreamTokenizer.TT_WORD:
tokBuf.add(tokenizer.sval);
break;
default: // operator
tokBuf.add(String.valueOf((char) tokenizer.ttype));
}
}
return tokBuf;
}
试运行:
System.out.println(tokenize("4*x+5.2024*(Log(x,y)^z)-300.12"));
[4.0, *, x, +, 5.2024, *, (, Log, (, x, ,, y, ), ^, z, ), -, 300.12]
这篇关于用于复杂数学表达式的高级标记器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!