A method of writing an analytic expression of one's own and obtaining the result (the last is expressed as a double, in which the middle result is computed by the BigDecimal, the temporary whim is written, no comment)

Source: Internet
Author: User
Tags addall

Package calcutor;

Import Java.math.BigDecimal;
Import java.util.ArrayList;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

public class calcutor{

public static BigDecimal GetResult (arraylist<string> list) {
if (list.size () = = 3) {

if ("+". Equals (List.get (2))) {
return new BigDecimal (List.get (0)). Add (New BigDecimal (List.get (1)));
}else if ("-". Equals (List.get (2)) {
return new BigDecimal (List.get (0)). Subtract (New BigDecimal (List.get (1));

}else if ("*". Equals (List.get (2)) {
return new BigDecimal (List.get (0)). Multiply (New BigDecimal (List.get (1));
}else if ("/". Equals (List.get (2))) {
return new BigDecimal (List.get (0)). Divide (New BigDecimal (List.get (1)), 1000,bigdecimal.round_up);

}else if ("^". Equals (List.get (2)) {
return new BigDecimal (Integer.parseint (list.get (0)) ^integer.parseint (List.get (1)));
}
}

Arraylist<string> List1 = new arraylist<string> ();

for (int i = 0;i<list.size (); i++) {

if (Priorityenum.getenumlist (). Contains (List.get (i))) {
List1.addall (list.sublist (0, i-2));
String s = "";
if ("+". Equals (List.get (i))) {

s = new BigDecimal (List.get (I-2)). Add (New BigDecimal (List.get (i-1)). ToString ();


}else if ("-". Equals (List.get (i)) {
s = new BigDecimal (List.get (i-2)). Subtract (new BigDecimal (List.get)). ToString ();

}else if ("*". Equals (List.get (i)) {
s = new BigDecimal (List.get (i-2)). Multiply (new BigDecimal (List.get)). ToString ();
}else if ("/". Equals (List.get (i))) {
s = new BigDecimal (List.get (i-2)). Divide (new BigDecimal (List.get), i-1). toString ();

}else if ("^". Equals (List.get (i)) {
s = "" + New BigDecimal (Integer.parseint (list.get (0)) ^integer.parseint (List.get (1));

}
List1.add (s);
List1.addall (List.sublist (i+1, List.size ()));
list = null;
Return GetResult (List1);
}
}
return Bigdecimal.zero;
}

public static arraylist<string> Readformula (String formula) {

Arraylist<string> List1 = new arraylist<string> ();
arraylist<string> list2 = new arraylist<string> ();
String s = "";

for (int i = 0; i< formula.length (); i++) {
if (!isc (Formula.charat (i))) {
List1.add (s);
List1.add ("" +formula.charat (i));
s = "";
} else {
S + + Formula.charat (i);
}
}

if (s!= null && s.length ()!= 0) {List1.add (s);}

for (String String:list1) {
if (string!= null && string.length ()!= 0) {list2.add (string);
}
List2.trimtosize ();
return list2;
}

public static Boolean isnumber (String target) {
if (target = null | | target.length () = = 0) {
return false;
}

Pattern pattern =
Pattern.compile ("[0-9]+\\.") [0-9]* ");
Matcher Matcher = Pattern.matcher (target);
return matcher.matches ();
}

public static Boolean IsC (char c) {

String target = "" +C;

if (target = null | | target.length () = = 0) {
return false;
}

Pattern pattern =
Pattern.compile ("[0-9]?\\.?");
Matcher Matcher = Pattern.matcher (target);
return matcher.matches ();
}

public static arraylist<string> sort (arraylist<string> list) {

  ArrayList<String> list1 = new arraylist<string> ();
  ArrayList<String> list2 = new arraylist<string> ();
  list1.add ("#");
  list.add ("#");
  for (String str:list) {
   if (isnumber (str)) {
     List2.add (str);
   } else{
    if (")". Equals (str)) {
      while (List1.size () > 0) {
      if () (. Equals (List1.get (List1.size ()-1)) {
       list1.remove (List1.size ()-1);
       break;
      }
      list2.add (List1.get (List1.size ()-1));
      list1.remove (List1.size ()-1);
     }

} else{
if (PRIORITYENUM.GETICP (str) > Priorityenum.getisp (List1.get (List1.size ()-1)) {
List1.add (str);
}else{

while (List1.size () > 0) {
if (PRIORITYENUM.GETICP (str) < Priorityenum.getisp (List1.get (List1.size ()-1)) {
if (! ")". Equals (List1.get (List1.size ()-1)) &&! " (". Equals (List1.get (List1.size ()-1)) &&!" # ". Equals (List1.get (List1.size ()-1))" {
List2.add (List1.get (List1.size ()-1));
}
List1.remove (List1.size ()-1);
} else {
Break
}
}
if (PRIORITYENUM.GETICP (str) > Priorityenum.getisp (List1.get (List1.size ()-1)) {
List1.add (str);
}
}
}
}

}

List1 = null;
List2.trimtosize ();
return list2;
}


}

Package calcutor;

Import java.util.ArrayList;

public enum Priorityenum {


A ("#", 0, 0)

, Left ("(", 8,1)

, right (")", 1,8)
, Divide ("/", 4,5)
, Multi ("*", 4,5)
, Mul ("%", 4,5)
, head ("^", 6,7)
, Plus ("+", 2, 3)
, Minus ("-", 2, 3);


Private final String param;


private final int ICP;


private final int ISP;


Private Priorityenum (String param, int ICP, int ISP) {
This.param = param;
THIS.ICP = ICP;
This.isp = ISP;
}


@Override
Public String toString () {
return this.param;
}


Public String GetParam () {
return this.param;
}


public static int GETICP (String param) {
For (Priorityenum v:values ()) {
if (V.getparam (). Equals (param)) {
return V.ICP;
}
}
return 0;
}
public static int getisp (String param) {
For (Priorityenum v:values ()) {
if (V.getparam (). Equals (param)) {
return v.isp;
}
}
return 0;
}
public static arraylist<string> Getenumlist () {
arraylist<string> list = new arraylist<string> ();
For (Priorityenum v:values ()) {
List.add (V.getparam ());
}
List.trimtosize ();
return list;
}
}

Package calcutor;

Import java.util.ArrayList;
Import Java.util.Scanner;
public class Run {
public static void Main (string[] args) {

Scanner sc = new Scanner (system.in);
String s = sc.nextline ();
String s = "11+12* (13-14) -15/16* (12+1.5* (3+5.2))";
String s = "12.6 + 13.5*6";
S =s. Replace ("", "");
arraylist<string> list = Calcutor.readformula (s);
Arraylist<string> List1 = calcutor.sort (list);
for (String String:list1) {
System.out.print (string+ "");
}
System.out.println ();
System.out.println (Calcutor.getresult (List1). Doublevalue ());
Double A = 1*13*15* (5+9) * (12.0/9+9* (3+5));
System.out.println (12^14);
System.out.println (a);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.