字符串运算java简单实现,没有优化,没有大批量的测试。

import java.util.ArrayList;
import java.util.List;

public class MathString {
	
	public static void main(String[] args){
		String press = "12*12.22-7*(23.45-(-2))+(12-34)";
		press = press.replaceAll("[\\[\\{]", "(").replaceAll("[\\]\\}]", ")");
		System.out.println(parseString(press));
	}
	
	public static String parseString(String press){
		int lasc = press.lastIndexOf("(");
		if(lasc==-1){
			return String.valueOf(addString(press));
		}
		
		String firstKHString = press.substring(0,lasc);
		String lastFKHString = press.substring(lasc+1);
		//System.out.println("firstKHString---"+firstKHString);
		//System.out.println("lastFKHString---"+lastFKHString);

		int firc = lastFKHString.indexOf(")");
		lastFKHString = press.substring(firc+lasc+2);
		//System.out.println("lastFKHString---"+lastFKHString);
		String currentPress = press.substring(lasc+1,firc+lasc+1);
		//System.out.println(currentPress);

		return parseString(firstKHString+addString(currentPress)+lastFKHString);
	
	}
	
	public static double addString(String press){
		//System.out.println(press);
		press = repAddString(press);
		press = repString(press);
		//System.out.println(press);
		String[] numbers = press.split("[\\+]");
		double number = 0;
		
		List<String> fhs = getFHAdd(press);
		for(int i=0 ; i<numbers.length; i++ ){
			String curNum = numbers[i];
			//System.out.println(curNum);
			if(curNum.equals("")){
				curNum="0";
			}
			if(fhs.get(i).equals("+")){
				number = number + mathString(curNum);
			}else if(fhs.get(i).equals("-")){
				number = number - mathString(curNum);
			}
		}
		return number;
	}
	
	public static double mathString(String press){
	  /*int firstCH = press.indexOf("*");
		int firstC = press.indexOf("/");
		if(firstCH == -1 && firstC == -1){
			return Double.parseDouble(press);
		}
		int firstindex = firstC < firstCH ? firstC : firstCH; 
	  */		
		press = repAddString(press);
		String[] numbers = press.split("[\\*\\/]");
		List<String> fhs = getFH(press);
		double result = 1;
		for(int i=0 ; i<numbers.length; i++){
			if(fhs.get(i).equals("*")){
				result = result * Double.parseDouble(numbers[i]);
			}else if(fhs.get(i).equals("/")){
				if(Double.parseDouble(numbers[i])==0){
					throw new ArithmeticException("表达式中被除数有空值!");
				}
				result = result / Double.parseDouble(numbers[i]);
			}
		}		
		return result;
	}
	
	
	
	public static String repAddString(String press){
		return press.replace("++", "+").replace("--", "+").replace("+-", "-").replace("-+", "-");
	}
	
	public static String repString(String press){
		StringBuffer sb = new StringBuffer();
		char ch = 0;
		for(char c:press.toCharArray()){
			if(c=='-'&&'/'!=ch&&'*'!=ch){
				sb.append("+");
			}
			ch = c;
			sb.append(c);
		}
		return sb.toString();
	}
	public static List<String> getFH(String press){
		char[] chars = press.toCharArray();
		List<String> fhs = new ArrayList<String>();
		fhs.add("*");
		for(char c : chars){
			if(c=='*' || c=='/'){
				fhs.add(String.valueOf(c));
			}
		}
		return fhs;
	}
	
	public static List<String> getFHAdd(String press){
		char[] chars = press.toCharArray();
		List<String> fhs = new ArrayList<String>();
		fhs.add("+");
		for(char c : chars){
			if(c=='+'){
				fhs.add(String.valueOf(c));
			}
		}
		return fhs;
	}
	
}

你可能感兴趣的:(字符串运算,java字符串运算,字符串四则运算)