143 lines
4.2 KiB
Java
143 lines
4.2 KiB
Java
package stack;
|
|
|
|
import java.util.Scanner;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class CalculatorByStack {
|
|
|
|
private final static int ADD = 1;
|
|
|
|
private final static int SUB = 2;
|
|
|
|
private final static int MUL = 3;
|
|
|
|
private final static int DIV = 4;
|
|
|
|
private ArrayStack<Integer> numStack = new ArrayStack(10);
|
|
|
|
private ArrayStack<Integer> operStack = new ArrayStack(10);
|
|
|
|
public void calculate(String expression) {
|
|
int length = expression.length();
|
|
String numStr = "";
|
|
for (int i = 0; i<length;i++){
|
|
String str = expression.substring(i,i+1);
|
|
String operRegex = "[+\\-*/]";
|
|
Pattern operPattern = Pattern.compile(operRegex);
|
|
if (operPattern.matcher(str).matches()) {
|
|
if("+".equals(str)){
|
|
if(operStack.isEmpty()){
|
|
operStack.push(ADD);
|
|
continue;
|
|
}
|
|
cal();
|
|
operStack.push(ADD);
|
|
}
|
|
if("-".equals(str)){
|
|
if(operStack.isEmpty()){
|
|
operStack.push(SUB);
|
|
continue;
|
|
}
|
|
cal();
|
|
operStack.push(SUB);
|
|
}
|
|
if("*".equals(str)){
|
|
if(operStack.isEmpty() || operStack.top() < 3){
|
|
operStack.push(MUL);
|
|
continue;
|
|
}
|
|
cal();
|
|
operStack.push(MUL);
|
|
}
|
|
if("/".equals(str)){
|
|
if(operStack.isEmpty() || operStack.top() < 3){
|
|
operStack.push(DIV);
|
|
continue;
|
|
}
|
|
cal();
|
|
operStack.push(DIV);
|
|
}
|
|
}else {
|
|
numStr += str;
|
|
if (i == expression.length()-1){
|
|
numStack.push(Integer.parseInt(numStr));
|
|
continue;
|
|
}
|
|
if (operPattern.matcher(expression.substring(i+1,i+2)).matches()){
|
|
numStack.push(Integer.parseInt(numStr));
|
|
numStr = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
// 开始进行计算
|
|
while (true) {
|
|
if(numStack.isEmpty()){
|
|
System.out.println("当前无计算公式");
|
|
}
|
|
if(numStack.getTop() == 0){
|
|
System.out.println("计算结果为:" + numStack.pop());
|
|
break;
|
|
}
|
|
cal();
|
|
}
|
|
|
|
}
|
|
|
|
public void cal(){
|
|
int oper = operStack.pop();
|
|
if(oper == ADD){
|
|
int num1 = numStack.pop();
|
|
int num2 = numStack.pop();
|
|
int result = num1 + num2;
|
|
numStack.push(result);
|
|
}
|
|
if(oper == SUB){
|
|
int num1 = numStack.pop();
|
|
int num2 = numStack.pop();
|
|
int result = num2 - num1;
|
|
numStack.push(result);
|
|
}
|
|
if(oper == MUL){
|
|
int num1 = numStack.pop();
|
|
int num2 = numStack.pop();
|
|
int result = num2 * num1;
|
|
numStack.push(result);
|
|
}
|
|
if(oper == DIV){
|
|
int num1 = numStack.pop();
|
|
int num2 = numStack.pop();
|
|
int result = num2/num1;
|
|
numStack.push(result);
|
|
}
|
|
}
|
|
|
|
private void show(){
|
|
System.out.println("数栈数据:");
|
|
numStack.show();
|
|
System.out.println("符号栈数据:");
|
|
operStack.show();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
CalculatorByStack calcStack = new CalculatorByStack();
|
|
Scanner scan = new Scanner(System.in);
|
|
while (true) {
|
|
System.out.println("c计算, s查看, exit退出:");
|
|
String handle = scan.next();
|
|
if ("exit".equals(handle)){
|
|
break;
|
|
}
|
|
if ("c".equals(handle)){
|
|
System.out.println("请输入计算表达式:");
|
|
String expression = scan.next();
|
|
calcStack.calculate(expression);
|
|
}
|
|
if ("s".equals(handle)){
|
|
calcStack.show();
|
|
}
|
|
}
|
|
scan.close();
|
|
}
|
|
}
|