2025-01-13 用栈实现计算器
This commit is contained in:
parent
ca2ce0d684
commit
6c51073f51
134
src/dataANDcalc/java/stack/CalculatorByStack.java
Normal file
134
src/dataANDcalc/java/stack/CalculatorByStack.java
Normal file
@ -0,0 +1,134 @@
|
||||
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 numStack = new ArrayStack(10);
|
||||
|
||||
private ArrayStack operStack = new ArrayStack(10);
|
||||
|
||||
public void calculate(String expression) {
|
||||
int length = expression.length();
|
||||
for (int i = 0; i<length;i++){
|
||||
String str = expression.substring(i,i+1);
|
||||
String operRegex = "[+\\-*/]";
|
||||
Pattern operPattern = Pattern.compile(operRegex);
|
||||
if (operPattern.matcher(String.valueOf(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 {
|
||||
int num = Integer.parseInt(str);
|
||||
numStack.push(num);
|
||||
}
|
||||
}
|
||||
|
||||
// 开始进行计算
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user