文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
我是人狠话不多,谦卑尚学呆萌呆萌的小萌新.
主程序模块
package operation;
import java.util.Scanner;
import java.util.Stack;
/**
* @author WangJiaHui
* @description: about operation
* @ClassName Test
* @date 2022/1/19 18:39
*/
public class Test {
public static void main(String[] args) {
//用户输入
Scanner sc = new Scanner(System.in);
//初步处理剔除用户意外输入的空格,方便逆波兰.
String str = sc.nextLine().replace(" ","");
//调用函数
int i = Dispose.DisposeString(str);
System.out.println(i);
}
}
调用Dispose类,用来进行字符串分割
package operation;
import java.util.ArrayList;
/**
* @author WangJiaHui
* @description: a RPN expression
* @ClassName RPN
* @date 2022/1/19 18:47
*/
public class Dispose {
//9+(3-1)*3+10/2
public static int DisposeString(String str) {
//进一步处理方便后续操作
char newC;
//申明字符数组
ArrayList<String> list = new ArrayList<>();
int i=0;
do{
//9+(3-1)*3+10/2
newC = str.charAt(i);
//如果当前字符是符号
if (newC <= '0' || newC >= '9' ) {
//将当前符号加入集合
list.add("" + newC);
i++;
} else {
//开始拼串
//用来拼串接收多位数字
String s = "";
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
s += str.charAt(i++);
}
list.add(s);
}
}while (i < str.length());
System.out.println(list);
return RPN.afterPression(list);
}
}
调用RPN,进行逆波兰处理
package operation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
/**
* @author WangJiaHui
* @description: a RPN
* @ClassName RPN
* @date 2022/1/20 17:14
*/
public class RPN {
//[9, +, (, 3, -, 1, ), *, 3, +, 10, /, 2]
public static int afterPression(ArrayList list) {
//创建栈,用来逆波兰
Stack<String> stack = new Stack<>();
//用来获得逆波兰表达式后的字符串
ArrayList list1 = new ArrayList();
//创建迭代器 逆波兰处理
Iterator<String> itr = list.iterator();
//逆波兰处理
while (itr.hasNext()) {
String ls = itr.next();
// + ( -
if (!stack.isEmpty()) {
switch (ls) {
case "+":
case "-":
if(stack.peek().equals("*") || stack.peek().equals("/")){
while (!stack.isEmpty()) {
list1.add(stack.pop());
}
}
stack.push(ls);
break;
case "*":
case "/":stack.push(ls);
break;
case "(":
stack.push(ls);
break;
case ")":
String c1 = ls;
// + ( -
while (!c1.equals("(")) {
c1 = stack.pop();
list1.add(c1);
}
break;
default:
list1.add(ls);
}
} else if (ls.equals("+") || ls.equals("-") || ls.equals("*") || ls.equals("(")) {
stack.push(ls);
} else list1.add(ls);
}
while (!stack.isEmpty()) {
list1.add(stack.pop());
}
list1.remove("(");
System.out.println(list1);
return Compute.MathCompute(list1);
}
}
调用Compute,进行逆波兰计算
package operation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
/**
* @author WangJiaHui
* @description: Compute
* @ClassName Compute
* @date 2022/1/20 18:57
*/
public class Compute {
//[9, 3, 1, -, 3, *, +, 10, 2, /, +]
public static int MathCompute(ArrayList list){
Iterator<String> itr = list.iterator();
//保留符号之前的数字
Stack<Integer> stack = new Stack<>();
//获取计算结果
int res;
int num1,num2;
while (itr.hasNext()){
String s = itr.next();
switch (s){
//20
case "+": res = stack.pop()+stack.pop();stack.push(res);break;
case "-":
num1=stack.pop();num2=stack.pop();
//9 2 3
res = num2-num1;stack.push(res);break;
case "*":
//9 6
res = stack.pop()*stack.pop();stack.push(res);break;
case "/":
//15 5
num1=stack.pop();num2=stack.pop();
try {
res = num2/num1;
stack.push(res);
}catch (Exception e){
System.out.println("除数不能为零!");
System.exit(-1);
}
break;
default:stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
}
程序运行结果
1.正常处理
2.异常处理