2014-11-04 30 views
0

我已经写了一些基本的代码中使用二个部分,计算数学表达式,但我难倒就如何进行计算上比这更如1/2 + 1/2 * 1/2如何进行方程多组分

我为两个部分代码如下。有什么建议么?

import java.util.Scanner; 

public class Evaluate { 
    public static void main(String args[]){ 

     Scanner in = new Scanner(System.in); 

     System.out.println("Enter your equation, please seperate fractions and operators with a space: "); 

     String[] equation = in.nextLine().split(" "); 

     String a = equation[0]; 
     String a2 = equation[2]; 
     String a4 = equation[1]; 

     String[] a1 = a.split("/"); 
     String[] a3 = a2.split("/"); 

     int b = Integer.parseInt(a1[0]); 
     int c = Integer.parseInt(a1[1]); 

     int b1 = Integer.parseInt(a3[0]); 
     int c1 = Integer.parseInt(a3[1]); 

     Fraction first = new Fraction(b,c); 
     Fraction second = new Fraction(b1,c1); 

     if(a4.equals("*")){ 
      Fraction multi = first.multiply(second); 
       System.out.println(multi); 
     } 
     else if(a4.equals("+")){ 
      Fraction add = first.add(second); 
      System.out.println(add); 
     } 
     else if(a4.equals("-")){ 
      Fraction sub = first.subtract(second); 
      System.out.println(sub); 
     } 
     else if(a4.equals("/")){ 
      Fraction div = first.divideBy(second); 
      System.out.println(div); 
     } 



    } 
} 
+0

如果你想超过2个输入,第一步是重新编写代码,以便您能接受“任何数量的输入”。简单地从2扩展到3会让代码变得越来越难以维护。聚集在列表中输入,然后解析列表,一旦用户完成输入数据。 – 2014-11-04 22:37:25

+0

感谢您的回复迈克。对不起,但我不明白。你能给我一个线索让我开始? – Gangadi 2014-11-04 22:51:12

+0

要非常小心。你需要确保你使用的浮点值,当你划分,或者你会遇到整数除法问题(截去小数部分)。 – Makoto 2014-11-05 00:20:56

回答

1

用空格分割的字符串是否足够,并应与多个运营商合作:

String[] equation = in.nextLine.split(" "); 

现在,你有,你要遍历这一点,创建两个的ArrayList,一个操作员,一个用于您的形式x/y的分数:

String[] operators = new String[equation.length/2]; 
String[] fractions = new String[operators.length+1]; 
for (int i = 0; i < equation.length; i+=2) 
{ 
    fractions[i] = equation[i]; 
    operators[i] = equation[i+1]; 
} 

现在你有2周的ArrayList你需要的所有东西,只是遍历这些:

Fraction[] newFractions = new Fraction[fractions.length]; 
for (int i = 0; i < fractions.length; i++) 
    String[] parts = fractions[i].split("/"); 
    Fraction[i] = new Fraction(parts[0],parts[1]); 

然后越过运营商ArrayList和做的那些:

for (int i = 0; i < operators.length; i++) 
{ 
    if (operators[i] == "*") 
     //Multiply your fractions 
     //Something like this, I haven't used the Fraction class before, nor heard of it 
     //Shouldn't throw out of bounds because the size should be 1 bigger 
     newFractions[i+1] = newFractions[i].multiply(newFractions[i+1]); 
    else if (operators[i] == "+") 
     //Add your fractions 
    else if (operators[i] == "-") 
     //Subtract your fractions 
    else if (operators[i] == "/") 
     //Divide your fractions 
} 
+0

由于这是一个很好的答案,但遗憾的是我不能使用ArrayList ... – Gangadi 2014-11-04 23:17:12

+0

@Gangadi编辑使用数组代替的ArrayList – Shadow 2014-11-05 01:02:15

-1

您可以使用javascript enginejava评估缀表达式。对于进口

import javax.script.ScriptEngineManager; 
import javax.script.ScriptEngine; 

,然后执行以下操作:

ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine engine = manager.getEngineByName("JavaScript"); 
String equation = "1/2 + 1/2 * 1/2"; 
System.out.println(engine.eval(equation)); 
+0

感谢约翰尼拉但遗憾的是不能使用任何库或脚本 – Gangadi 2014-11-04 23:18:23