2011-09-22 108 views
0

所以我必须编写一个程序,将从用户采取公式并计算它们。动态更新值?

Age, Height, fights, (age+height)*fights <- user defined 
    10, 5, 10, 150 <- I calculate this 
    1, 2, 1, 3 <- I calculate this 

但现在让我说我改变周围的价值,我想动态更新公式列是否有无论如何去做呢?我将每行存储到数组列表中,该列表是数组列表的数组列表。任何建议或指导将是真正有用的谢谢你:)

回答

0

我们可以修改此做一个简单的引擎

public interface Function<T> { 
    T operate(Map<String,T> values); 
} 

public class Calculation<T> { 

    private Map<String,T> values; 

    private Function<T> function; 

    public Calculation(Map<String,T> values, Function<T> function) { 
     this.values = new HashMap<String,T>(values); 
     this.function = function; 
    } 

    public T calculate() { 
     return function.operate(Collections.unmodifiableMap(values)); 
    } 

    // assume setters and getters are in place to manipulate the backing map and the functor object 

} 

有了这个地方,我们可以定义各种功能和计算

public static void main(String[] args) { 
    Function<Integer> original = new Function<Integer>() { 
     public Integer operate(Map<String,Integer> values) { 
      // these will throw exceptions if they don't exist, which is desired 
      // granted it's NullPointerException instead of IllegalStateException, but close enough for this example 
      int age = values.get("age").intValue(); 
      int height = values.get("height").intValue(); 
      int fights = values.get("fights").intValue(); 
      return (age+height)*fights; 
     } 
    }; 
    Map<String,Integer> map = new Map<String,Integer>(); 
    map.put("age",10); 
    map.put("height",100); 
    map.put("fights",25); 
    Calculation<Integer> calc = new Calculation<Integer>(map,original); 

    // later someone can replace either the values in the backing map 
    // or replace the function altogether to get a new function 
} 

当您试图使它不仅在问题出现用户定义,但用户输入。如在中,用户可以将该功能定义为您预先输入的一组功能之一。在这种情况下,你需要有一个解析器,原来是这样的:

value + (other + (something * that))/wazook 

到这一点:

EquationBuilder<Integer> eb = new EquationBuilder<Integer>(); 
eb.append(map.get("value")); 
eb.append(OPERATOR.PLUS); 
// etc 
return eb.evaluate(); 

但是,这可能超出你的任务的范围。或者,也许不是。

希望这比我以前的例子更接近您的要求。

+0

问题辉光编码器是公式是用户定义的,所以它可以是任何数量的大小和任何公式我是做这个没有类的部分... –

+0

@ashleysmith我修改了我的答案,以满足我对您的需求的重新评估。这种方法将如何为你工作? – corsiKa

0

你可能想看看使用其中一个表达式计算引擎而不是ArrayLists。您可以看到Expr4J或BIRT查找可能的解决方案。

0

您可能想要为这些数组编写事件侦听器。文档可以找到这里 http://download.oracle.com/javase/tutorial/uiswing/events/listdatalistener.html

更简单的解决方案是将写一个计算公式的简单方法和从触发列表中的值的变化方案的每一个部分调用它(但是这种解决方案可以杂乱的代码) 。如果这是一项家庭作业或者非常小的项目,你可以采用这种方法,否则我会建议编写一个监听器。

0

说实话,我会用更多的方式OO,请大家看看:

public class Parameter { 
    private String id; 
    private Number value; 

    public Parameter(String id, Number value) { 
     this.id = id; 
     this.value = value; 
    } 

    public String getId() { 
     return id; 
    } 

    public Number getValue() { 
     return value; 
    } 

    @Override 
    public int hashCode() { 
     return id.hashCode(); 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if(obj instanceof Parameter) { 
      Parameter parameter = (Parameter) obj; 
      return id.equals(parameter.getId()); 
     } 

     return super.equals(obj); 
    } 

} 

public class Equation { 
    private String formula; 
    private Set<Parameter> parameters; 

    public Equation(String formula, Parameter ... parameters) { 
     this.formula = formula; 
     this.parameters = new HashSet<Parameter>(); 
     for(Parameter parameter : parameters) { 
      this.parameters.add(parameter); 
     } 
     validatePresenceOfAllParameters(); 
    } 

    private void validatePresenceOfAllParameters() { 
     // here you parse formula and check if all parameters are present 
    } 

    public Number doTheMagic() { 
     Number result = null; 
     // here you do the all magic related to equation and its parameters 
     return result; 
    } 

    // public void setParameter(Parameter parameter) { 
    // parameters.add(parameter); 
    // validatePresenceOfAllParameters(); 
    // } 

} 

我也会让ParameterEquation对象不变,如果有改变的必要,我会再拍方程对象,但如果这是要求,您可以在Equation中编写方法来添加/设置Parameter's。 Set会处理它们,如果他们有相同的ID。