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<String> math = new ArrayList<String>();
List<String> flag = new ArrayList<String>();
List<Integer> mulDiv = new ArrayList<Integer>();
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.String());
sbMath.delete(0, sbMath.length());
flag.add(String.valueOf(temp));
if(temp == mul || temp == div){
mulDiv.add(flag.size()-1);
}
}
}
math.String());
while(math.size() != 1){
boolean needReIndex = false;
while(mulDiv.size() != 0){
int index = (0);
if(needReIndex){
index = index -1;
}
Map<String, List<String>> map = this.loopProcess(index, math, flag);
math = ("math");
flag = ("flag");
mulDiv = veList(Integer.class, mulDiv, 0);
needReIndex = true;
}
while(flag.size() != 0){
Map<String, List<String>> map = this.loopProcess(0, math, flag);
math = ("math");
flag = ("flag");
}
}
System.out.(0));
}
private Map<String, List<String>> loopProcess(int index, List<String> math, List<String> flag){
Map<String, List<String>> map = new HashMap<String, List<String>>();
char ch = (index).charAt(0);
String result = (index).trim(), (index+1).trim(), ch);
math = veList(String.class, math, index);
math = veList(String.class, math, index);
math.add(index, result);
flag = veList(String.class, flag, index);
map.put("math", math);
map.put("flag", flag);
return map;
}
private <T> List<T> removeList(Class<T> clazz, List<T> list, int index){
List<T> listTemp = new ArrayList<T>();
for (int i = 0; i < list.size(); i++) {
if(i != index){
listTemp.(i));
}
}
return listTemp;
}
private String getResult(String b, String e, char flag){
boolean isLong = false;
if(!b.contains(".") && !e.contains(".")){
isLong = true;
}
if(isLong){
java valueofif(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);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论