package Stack.PolandNotation03;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class demo03 {
public static void main(String[] args) {
String expression = "1+((2+3)*4)-5";
List<String> list = transferList(expression);
System.out.println(list);
List<String> finalList = convertFinal(list);
System.out.println(finalList);
int res2 = calculate(finalList);
System.out.println("计算的结果是" + res2);
String suffixExpression = "3 4 + 5 * 6 -";
List<String> rpnList = getList(suffixExpression);
System.out.println("rpnList=" + rpnList);
int res = calculate(rpnList);
System.out.println("计算的结果是" + res);
}
public static List<String> transferList(String s) {
ArrayList<String> list = new ArrayList<>();
int index = 0;
String str;
char c;
do {
if ((c=s.charAt(index))<48||(c=s.charAt(index))>57){
list.add(""+c);
index++;
} else {
str="";
while (index<s.length()&& (c=s.charAt(index))>=48&&(c=s.charAt(index))<=57){
str+=c;
index++;
}
list.add(str);
}
} while (index < s.length()) ;
return list;
}
public static List<String> convertFinal(List<String> list){
Stack<String> s1 = new Stack<>();
List<String> s2 = new ArrayList<>();
for (String s : list) {
if (s.matches("\\d+")){
s2.add(s);
} else if (s.equals("(")){
s1.push(s);
} else if (s.equals(")")){
while (!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();
} else {
while (s1.size()!=0&&compare(s)<=compare(s1.peek())){
s2.add(s1.pop());
}
s1.push(s);
}
}
while (s1.size()!=0){
s2.add(s1.pop());
}
return s2;
}
public static int compare(String s){
if ("*".equals(s)||"/".equals(s)){
return 1;
}
else if ("+".equals(s)||"-".equals(s)){
return 0;
} else
return -1;
}
public static List<String> getList(String suffixExpression) {
String[] s = suffixExpression.split(" ");
List<String> list = new ArrayList<>();
for (String ele : s) {
list.add(ele);
}
return list;
}
public static int cal(int num1, int num2, String oper) {
int res = 0;
switch (oper) {
case "+":
res = num1 + num2;
break;
case "-":
res = num1 - num2;
break;
case "*":
res = num1 * num2;
break;
case "/":
res = num1 / num2;
break;
}
return res;
}
public static int calculate(List<String> ls) {
Stack<String> stack = new Stack<>();
for (String item : ls) {
if (item.matches("\\d+")) {
stack.push(item);
} else {
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = cal(num1, num2, item);
stack.push(res + "");
}
}
return Integer.parseInt(stack.pop());
}
}
package Recursion;
public class RecursionTest {
public static void main(String[] args) {
int[][] map = new int[8][7];
for (int i = 0; i < 7; i++) {
map[0][i]=1;
map[7][i]=1;
}
for (int i = 0; i < 8; i++) {
map[i][0]=1;
map[i][6]=1;
}
map[3][1]=1;
map[3][2]=1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
setWay(map,1,1);
show(map);
}
public static boolean setWay(int[][] map,int i,int j){
if (map[6][5]==2){
return true;
}else {
if (map[i][j]==0){
map[i][j]=2;
if (setWay(map,i+1,j)){
return true;
} else if (setWay(map,i,j+1)){
return true;
} else if (setWay(map,i-1,j)){
return true;
} else if (setWay(map,i,j-1)){
return true;
} else {
map[i][j]=3;
return false;
}
}
else {
return false;
}
}
}
public static void show(int[][] map){
System.out.println("小球走过的地图:");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
}