和同事大哥一起研究出来的方法,记录一下
并没有使用逆波兰方法,将中缀表达式转后缀表达式,再用栈计算。
这里使用的是不同运算符号的优先级来将表达式来拆分,于人计算的过程相似
如:((2+3)*4)-5
1.将表达式存入list a中,循环寻找是否含有“(”,如果没有进行按*/ +-的运算规则将list 循 环查找符号,将list(i-1)*/ +-list(i+1)
将式子递归通过循环来找寻“()”
然后中不再有“()”括号的最小式子存入一个list b中
“(2+3)”//2级
2.将最小式子中按*/ +-的运算规则将list循环查找符号,将list(i-1)*/ +-list(i+1)将计算结果替换到list b中 ,直至乜有符号
将结果替换到list a 中
3. 现在list 的表达式为 “(5*4)-5” 再次递归寻找含有括号的
```
import java.text.DecimalFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(
System.in);
String str = sc.nextLine();
char[] chars = str.toCharArray();
/*1 :先将字符串变成数字和运算符*/
List list = new ArrayList();
Stack stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
if (chars[i]>='0' && chars[i]<='9'){
int num = chars[i]-'0';
int j = 0;
while ((i+1)='0' && chars[i+1]<='9' ){
num = num*10+chars[i+1]-'0';
i++;
}
stack.push(num);
list.add(num+"");
}else{
stack.push(chars[i]);
list.add(chars[i]+"");
}
}
double doublenum = getlsitlevel2(list);
System.out.println(doublenum);
}
/*0级*/
public static double getlsitlevel0( List list ) {
double datevalue1 = 0 ;
for (int i = 0; i < list.size(); i++) {
if( ("+").equals(list.get(i)) ){
datevalue1 = datevalue1+ Double.parseDouble((String) list.get(i+1));
i++;
}else if(("-").equals(list.get(i))){
datevalue1 = datevalue1- Double.parseDouble((String) list.get(i+1));
i++;
}else{
datevalue1 = Double.parseDouble( (String)list.get(i) );
}
}
return datevalue1;
}
/*1级*/
public static double getlsitlevel1( List list ) {
for (int i = 0; i < list.size(); i++) {
if( ("*").equals(list.get(i))){
list.set(i-1,Double.parseDouble((String) list.get(i-1)) * Double.parseDouble((String) list.get(i+1))+"" ) ;
list.remove(i);
list.remove(i);
}else if( ("/").equals(list.get(i))){
list.set(i-1,Double.parseDouble((String) list.get(i-1)) / Double.parseDouble((String) list.get(i+1) )+"" ) ;
list.remove(i);
list.remove(i);
}
}
return getlsitlevel0(list);
}
/*2级*/
public static double getlsitlevel2( List list ) {
if(islevel0(list)){
return getlsitlevel1(list);
}
for (int i = 0; i < list.size(); i++) {
int yi = i;
if( list.get(i).equals("(") ){
List newlist = new ArrayList<>();
i++;
int leftnum = 1;
while(i
if( list.get(i).equals("(")){
leftnum ++;
newlist.add( list.get(i));
}else if( list.get(i).equals(")")) {
--leftnum;
if(leftnum==0){
break;
}else{
newlist.add( list.get(i));
}
}else{
newlist.add( list.get(i));
}
i++;
}
double doublen = 0;
if(islevel0(newlist)){
doublen = getlsitlevel1(newlist);
}else{
doublen = getlsitlevel2(newlist);
}
list.set(yi,doublen+"");
for (int j = yi+1; j
list.remove(yi+1);
}
i=yi+1;
}
}
return getDoubleString(getlsitlevel2(list));
}
public static double getDoubleString(double number) {
double numberStr = 0;
if (((int) number * 1000) == (int) (number * 1000)) {
//如果是一个整数
numberStr = (int) number;
}else{
return number;
}
return numberStr ;
}
public static boolean islevel0( List list ) {
for (int i = 0; i < list.size(); i++) {
if( list.get(i).equals("(")||list.get(i).equals(")") ){
return false;
}
}
return true;
}
```