Topic: 如何解析字符串中的数学表达式

  Print this page

1.如何解析字符串中的数学表达式 Copy to clipboard
Posted by: k_k_grass
Posted on: 2004-09-26 21:15

欲取“会计科目11101+1102-(1103-1104)的科目余额” 的 “11101+1102-(1103-1104)”

现通过遍历该字符串中的每个字符是否为数字或操作符,但是否有其他好的方法呢?

2.Re:如何解析字符串中的数学表达式 [Re: k_k_grass] Copy to clipboard
Posted by: why
Posted on: 2004-09-27 06:54

you may want to use Regular Expression

3.Re:如何解析字符串中的数学表达式 [Re: k_k_grass] Copy to clipboard
Posted by: battlekiller
Posted on: 2004-09-28 20:50

使用substring()怎么样?
汉字占二字节
其他的只占一字节
用str.substring(8,Sb.lastIndexOf(")"));
不就行了
也许我这种方法很菜,希望你能给我些指教
谢谢

4.Re:如何解析字符串中的数学表达式 [Re: k_k_grass] Copy to clipboard
Posted by: aleel_008
Posted on: 2004-09-30 00:26

public class GetNumber
{
  public static void main(String[] args){
  System.out.println(getNumber());
  }
  private static String getNumber(){
    String str=new String("会计科目11101+1102-(1103-1104)的科目余额");
    byte[] bytes=str.getBytes();
    StringBuffer buffer=new StringBuffer("");
    String temp;
    try{
   for(int i=0;i<str.length();i++){
    temp=str.substring(i,i+1);
    if(temp.getBytes().length==1){
      buffer.append(temp);
    }
   }
    }
    catch(Exception e){
    e.printStackTrace();
    }
   return new String(buffer);
  }
}

5.Re:如何解析字符串中的数学表达式:使用开源和商业软件 [Re: aleel_008] Copy to clipboard
Posted by: youlq
Posted on: 2004-09-30 09:05

开源软件:
UJAC - Useful JAVA Application Components
Expression Interpreter
http://ujac.sourceforge.net/expression-interpreter.html

${1 + 2 + 3}
Gets the result of the calculation (6 in this case - I guess).

商业软件:
JFormula
http://www.japisoft.com/formula/
Formula is a Java library for evaluating mathematical expressions
Main features :

Decimal, string, boolean operators (or, and, not, xor...)

Unicode

Boolean expression support : (A<B)&&(B>C), (A or B) and not ( C equals D )
String expression support : "abc" != "cba"

IF THEN ELSE expression

Short expression format : 2x+3y
Variable : A=(cos(PI + x )*2) + [y-x]^2
Multiple lines expression : A = 1 B = A + 1 ...
Functions with decimal, boolean or string arguments

Evaluation tree produced by a pluggable parsing system
Evaluation optimization for symbol value changes

Standard library with 24 mathematical functions
Delegate for resolving unknown functions or symbols
Extend or add a new library dynamically
Share multiple formula context

Override any functions from the current library by your one
Support for multithreaded computing

Many samples (library extension, graphes) for API interesting parts
JDK 1.1 compliant (tested on JDK1.1.8 and JDK1.4.2)

6.Re:如何解析字符串中的数学表达式 [Re: aleel_008] Copy to clipboard
Posted by: think
Posted on: 2004-09-30 09:15

这儿有一个Java Mathematical Expression Parser - JEP

http://www.singularsys.com/jep/.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923