import org.apache.http.util.TextUtils;
import org.locationtech.jts.operation.buffer.BufferOp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
public class StackParser {
public static final StringcaculatorString ="9+(3-1)*3+10/2+5*8";
static StacksymbolStack =new Stack<>();
static StackdataStack =new Stack<>();
public static void main(String[] args)
{
mutilple();
int result =result();
System.out.println("result " + result);
}
private static void mutilple()
{
int length =caculatorString.length();
for (int i =0; i < length; i++)
{
int j = i;
String params ="";
boolean isNumber =true;
String character =null;
while (isNumber)
{
character = String.valueOf(caculatorString.charAt(j));
isNumber =isNumber(character);
if (isNumber)
{
params = params + character +"";
if (j < length -1)
{
j++;
}
else
{
i = j;
//读到最后一个了
if (!TextUtils.isEmpty(params))
{
dataStack.push(params);
if (symbolStack.size() >0)
{
int size =symbolStack.size();
for (int i1 = size-1; i1 >=0; i1--)
{
dataStack.push(symbolStack.get(i1));
}
}
}
break;
}
}
//读取到字符
else
{
//将已经读取出来的数字拼截出来
if (!TextUtils.isEmpty(params))
{
dataStack.push(params);
}
if (")".equals(character) &&symbolStack.contains("("))
{
int index =symbolStack.indexOf("(");
String symbol =symbolStack.get(index +1);
symbolStack.remove("(");
symbolStack.remove(symbol);
dataStack.push(symbol);
i = j;
continue;
}
//乘除
boolean characterChengchu = ("*".equals(character) ||"/".equals(character));
//加减
boolean characterJiaJian = ("+".equals(character) ||"-".equals(character));
if ((symbolStack.size() >0) && (characterChengchu || characterJiaJian))
{
String topSymbol =symbolStack.peek();
//乘除
boolean topChengchu ="*".equals(topSymbol) ||"/".equals(topSymbol);
//加减
boolean topJiaJian ="+".equals(topSymbol) ||"-".equals(topSymbol);
//优先级高,直接入栈 或者非运算符
if ((characterChengchu && topJiaJian) || (!topChengchu && !topJiaJian))
{
symbolStack.push(character);
}
//优先低需要将栈内比它优先级更高的或者相等的出栈
else
{
List removeString =new ArrayList<>();
//如果是乘除
if (characterChengchu)
{
Iterator iterator =symbolStack.iterator();
while (iterator.hasNext())
{
String sy = iterator.next();
//加减比乘除优先级要低所以可以保留
boolean syJiaJian ="+".equals(sy) ||"-".equals(sy);
if (!syJiaJian)
{
removeString.add(sy);
}
else
{
break;
}
}
for (int i1 = removeString.size() -1; i1 >=0; i1--)
{
String content = removeString.get(i1);
dataStack.push(content);
symbolStack.remove(content);
}
removeString.clear();
}
else
{
Iterator iterator =symbolStack.iterator();
while (iterator.hasNext())
{
String sy = iterator.next();
removeString.add(sy);
iterator.remove();
}
for (int j1 = removeString.size() -1; j1 >=0; j1--)
{
String content = removeString.get(j1);
dataStack.push(content);
}
removeString.clear();
}
symbolStack.push(character);
}
}
else
{
symbolStack.push(character);
}
i = j;
break;
}
}
}
System.out.println("params " +dataStack.toString() +" symbol " +symbolStack.toString());
}
public static boolean isNumber(String char1)
{
if ("0".equals(char1))return true;
try
{
Integer.parseInt(char1);
return true;
}catch (Exception e)
{
return false;
}
}
static int result()
{
int i =0;
List tmp =new ArrayList<>();
for (String character :dataStack)
{
if (isNumber(character))
{
tmp.add(Integer.valueOf(character));
}
else
{
int siz = tmp.size();
if (siz >=2)
{
List tempArray =new ArrayList<>();
int remain = siz -2;
for (int i1 =0; i1 < remain; i1++)
{
int value = tmp.get(i1);
tempArray.add(value);
}
int value =0;
int sencond = tmp.get(siz -1);
int first = tmp.get(siz -2);
if ("*".equals(character))
{
value = (first * sencond);
}
else if ("/".equals(character))
{
value = (first / sencond);
}
else if ("+".equals(character))
{
value = (first + sencond);
}
else if ("-".equals(character))
{
value = (first - sencond);
}
tempArray.add(value);
tmp = tempArray;
}
}
i++;
}
return tmp.get(0);
}
}