java 对一个字符串进行加减乘除的运算

记录一个小程序,里面涉及到的JAVA知识点有:字符串扫描,list删除元素的方法,泛型的使用,JAVA中的/要注意的事项。有兴趣的可以看看

package com.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Calculate {
	
	public char add = '+';
	
	public char del = '-';
	
	public char mul = '*';
	
	public char div = '/';

	public void calculate(String s){				
		StringBuffer sbMath = new StringBuffer();
		List math = new ArrayList();		
		List flag = new ArrayList();
		List mulDiv = new ArrayList();
		for (int i = 0; i < s.length(); i++) {
			char temp = s.charAt(i);			
			if(temp!= add && temp!= del && temp!=mul && temp!=div){
				sbMath.append(String.valueOf(temp));
			}else{	
				if(sbMath.length()==0 && temp==del){
					sbMath.append("0");
				}
				math.add(sbMath.toString());
				sbMath.delete(0, sbMath.length());
				flag.add(String.valueOf(temp));
				if(temp == mul || temp == div){   
					mulDiv.add(flag.size()-1);
				}
			}			
		}
		math.add(sbMath.toString());
		while(math.size() != 1){	
			boolean needReIndex = false;
			while(mulDiv.size() != 0){
				int index = mulDiv.get(0);
				if(needReIndex){
					index = index -1;
				}
				Map> map = this.loopProcess(index, math, flag);
				math = map.get("math");
				flag = map.get("flag");
				mulDiv = this.removeList(Integer.class, mulDiv, 0);
				needReIndex = true;
			}
			while(flag.size() != 0){				
				Map> map = this.loopProcess(0, math, flag);
				math = map.get("math");
				flag = map.get("flag");
			}			
		}
		System.out.println(math.get(0));
	}
	
	private Map> loopProcess(int index, List math, List flag){
		Map> map = new HashMap>();
		char ch = flag.get(index).charAt(0);
		String result = this.getResult(math.get(index).trim(), math.get(index+1).trim(), ch);
		math = this.removeList(String.class, math, index);
		math = this.removeList(String.class, math, index);
		math.add(index, result);
		flag = this.removeList(String.class, flag, index);		
		map.put("math", math);
		map.put("flag", flag);
		return map;
	}
	
	private  List removeList(Class clazz, List list, int index){
		List listTemp = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			if(i != index){
				listTemp.add(list.get(i));
			}
		}
		return listTemp;
	}	
	
	private String getResult(String b, String e, char flag){
		boolean isLong = false;
		if(!b.contains(".") && !e.contains(".")){
			isLong = true;
		}
		if(isLong){
			if(flag == add){
				return String.valueOf(Long.valueOf(b)+Long.valueOf(e));
			}else if(flag == del){
				return String.valueOf(Long.valueOf(b)-Long.valueOf(e));
			}else if(flag == mul){
				return String.valueOf(Long.valueOf(b)*Long.valueOf(e));
			}else if(flag == div){
				return String.valueOf((double)Long.valueOf(b)/Long.valueOf(e));
			}else{
				throw new RuntimeException("error: "+ b + flag + e);
			}
		}else{
			if(flag == add){
				return String.valueOf(Double.valueOf(b)+Double.valueOf(e));
			}else if(flag == del){
				return String.valueOf(Double.valueOf(b)-Double.valueOf(e));
			}else if(flag == mul){
				return String.valueOf(Double.valueOf(b)*Double.valueOf(e));
			}else if(flag == div){
				return String.valueOf((double)Double.valueOf(b)/Double.valueOf(e));
			}else{
				throw new RuntimeException("error: "+ b + flag + e);
			}
		}
		
	}	
	
	public static void main(String[] args) {
		Calculate t = new Calculate();
		String s = "1+2*3-10/3";
		t.calculate(s);
	}

}

 

转载于:https://www.cnblogs.com/zhangfei/p/4609831.html

你可能感兴趣的:(java 对一个字符串进行加减乘除的运算)