演算式の値を求める
12016 ワード
HaspMap方法:
転載元:http://www.eoeandroid.com/thread-317031-1-1.html
転載元:http://www.eoeandroid.com/thread-317031-1-1.html
public double StringToDouble(String s1) {
double d = 0;
//
String[] a = s1.split("[+-]");
//
List<Map<String, Object>> list;
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
char cc[] = s1.toCharArray();
for (int i = 0; i < cc.length; i++) {
Map<String, Object> map2 = new HashMap<String, Object>();
if (cc[i] == '+') {
map2.put("P", "add");
list2.add(map2);
}
if (cc[i] == '-') {
map2.put("P", "sub");
list2.add(map2);
}
}
/*
* Toast.makeText(CalcolatorActivity.this, list2.size() + "s1/x",
* Toast.LENGTH_LONG).show();
*/
int x = 0;
// a
for (int i = 0; i < a.length; i++) {
//
String[] b = a[i].split("[*/]");
/*
* Toast.makeText(CalcolatorActivity.this, b.length + "b",
* Toast.LENGTH_LONG).show();
*/
// a
char c[] = a[i].toCharArray();
// a he
list = new ArrayList<Map<String, Object>>();
// map, 。
for (int j = 0; j < c.length; j++) {
Map<String, Object> map = new HashMap<String, Object>();
if (c[j] == '/') {
map.put("M", "div");
list.add(map);
}
if (c[j] == '*') {
map.put("M", "mul");
list.add(map);
}
}
// Toast.makeText(CalcolatorActivity.this,
// list.size() + ":list", Toast.LENGTH_LONG).show();
// map a , a 。
int w = 0;
double dd = 1;
if (list.size() == 0) {
// dd=Double.parseDouble(a[i]);
dd = Double.parseDouble(1 + "");
} else {
for (int k = 0; k < b.length - 1; k++) {
if (w == 0) {
double d1 = Double.parseDouble(b[k]);
double d2 = Double.parseDouble(b[k + 1]);
String m = list.get(k).get("M").toString();
if (m.equals("mul")) {
dd = d1 * d2;
} else if (m.equals("div")) {
dd = d1 / d2;
}
w = 1;
} else {
double d3 = Double.parseDouble(b[k + 1]);
String m = list.get(k).get("M").toString();
if (m.equals("mul")) {
dd = dd * d3;
} else if (m.equals("div")) {
dd = dd / d3;
}
}
/*
* Toast.makeText(CalcolatorActivity.this, "bbbbbbbbs",
* Toast.LENGTH_LONG).show();
*/
}
}
if (x == 0) {
d = dd;
x = 1;
} else {
String n = list2.get(i - 1).get("P").toString();
if (n.equals("add")) {
d = d + dd;
} else if (n.equals("sub")) {
d = d - dd;
}
}
}
/*
* Toast.makeText(CalcolatorActivity.this, d + "_ ",
* Toast.LENGTH_LONG).show();
*/
return d;
}
第二種類:public class ArithHelper {
//
private static final int DEF_DIV_SCALE = 16;
//
private ArithHelper() {
}
/**
* 。
*
* @param v1
*
* @param v2
*
* @return
*/
public static double add(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
public static double add(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.add(b2).doubleValue();
}
/**
* 。
*
* @param v1
*
* @param v2
*
* @return
*/
public static double sub(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
public static double sub(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.subtract(b2).doubleValue();
}
/**
* 。
*
* @param v1
*
* @param v2
*
* @return
*/
public static double mul(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
public static double mul(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.multiply(b2).doubleValue();
}
/**
* ( ) , , 10 , 。
*
* @param v1
*
* @param v2
*
* @return
*/
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
public static double div(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.divide(b2, DEF_DIV_SCALE, java.math.BigDecimal.ROUND_HALF_UP)
.doubleValue();
}
/**
* ( ) 。 , scale , 。
*
* @param v1
*
* @param v2
*
* @param scale
* 。
* @return
*/
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 。
*
* @param v
*
* @param scale
*
* @return
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b = new java.math.BigDecimal(Double.toString(v));
java.math.BigDecimal one = new java.math.BigDecimal(1);
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double round(String v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b = new java.math.BigDecimal(v);
java.math.BigDecimal one = new java.math.BigDecimal(1);
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
package com.example.democalculatortest;
import java.util.Collections;
import java.util.Stack;
/**
* ClassName :XXXXXXXXXX
* Comments :XXXXXXXXXX
*
*/
public class Calculator {
private Stack<String> postfixStack = new Stack<String>();//
private Stack<Character> opStack = new Stack<Character>();//
private int [] operatPriority = new int[] {0,3,2,1,-1,1,0,2};// ASCII -40
// public static void main(String[] args) {
// System.out.println(5+12*(3+5)/7.0);
// Calculator cal = new Calculator();
// String s = 5+12*(3+5)/7;
// double result = cal.calculate(s);
// System.out.println(result);
// }
/**
*
* @param expression :5+12*(3+5)/7
* @return
*/
public double calculate(String expression) {
Stack<String> resultStack = new Stack<String>();
prepare(expression);
Collections.reverse(postfixStack);//
String firstValue ,secondValue,currentValue;// ,
while(!postfixStack.isEmpty()) {
currentValue = postfixStack.pop();
if(!isOperator(currentValue.charAt(0))) {//
resultStack.push(currentValue);
} else {//
secondValue = resultStack.pop();
firstValue = resultStack.pop();
String tempResult = calculate(firstValue, secondValue, currentValue.charAt(0));
resultStack.push(tempResult);
}
}
return Double.valueOf(resultStack.pop());
}
/**
*
* @param expression
*/
private void prepare(String expression) {
opStack.push(',');// ,
char[] arr = expression.toCharArray();
int currentIndex = 0;//
int count = 0;//
char currentOp ,peekOp;//
for(int i=0;i<arr.length;i++) {
currentOp = arr[i];
if(isOperator(currentOp)) {//
if(count > 0) {
postfixStack.push(new String(arr,currentIndex,count));//
}
peekOp = opStack.peek();
if(currentOp == ')') {//
while(opStack.peek() != '(') {
postfixStack.push(String.valueOf(opStack.pop()));
}
opStack.pop();
} else {
while(currentOp != '(' && peekOp != ',' && compare(currentOp,peekOp) ) {
postfixStack.push(String.valueOf(opStack.pop()));
peekOp = opStack.peek();
}
opStack.push(currentOp);
}
count = 0;
currentIndex = i+1;
} else {
count++;
}
}
if(count > 1 || (count == 1 && !isOperator(arr[currentIndex]))) {//
postfixStack.push(new String(arr,currentIndex,count));
}
while(opStack.peek() != ',') {
postfixStack.push(String.valueOf( opStack.pop()));//
}
}
/**
*
* @param c
* @return
*/
private boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' ||c == ')';
}
/**
* ASCII -40
* @param cur
* @param peek
* @return
*/
public boolean compare(char cur,char peek) {// peek cur, true, peek
boolean result = false;
if(operatPriority[(peek)-40] >= operatPriority[(cur) - 40]) {
result = true;
}
return result;
}
/**
*
* @param firstValue
* @param secondValue
* @param currentOp
* @return
*/
private String calculate(String firstValue,String secondValue,char currentOp) {
String result =" " ;
switch(currentOp) {
case '+':
result = String.valueOf(ArithHelper.add(firstValue, secondValue));
break;
case '-':
result = String.valueOf(ArithHelper.sub(firstValue, secondValue));
break;
case '*':
result = String.valueOf(ArithHelper.mul(firstValue, secondValue));
break;
case '/':
result = String.valueOf(ArithHelper.div(firstValue, secondValue));
break;
}
return result;
}
}