2025-01-14 1.优化栈类(支持多种类型) 2.逆波兰表达式计算
This commit is contained in:
parent
bba8bce10f
commit
30cfd63a65
@ -2,19 +2,20 @@ package stack;
|
|||||||
|
|
||||||
import queue.ArrayQueue;
|
import queue.ArrayQueue;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ArrayStack {
|
public class ArrayStack<E> {
|
||||||
|
|
||||||
private int top = -1;
|
private int top = -1;
|
||||||
|
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
|
|
||||||
private int[] stack;
|
private Object[] stack;
|
||||||
|
|
||||||
public ArrayStack(int maxSize) {
|
public ArrayStack(int maxSize) {
|
||||||
this.maxSize = maxSize;
|
this.maxSize = maxSize;
|
||||||
stack = new int[maxSize];
|
stack = new Object[maxSize];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTop() {
|
public int getTop() {
|
||||||
@ -33,11 +34,11 @@ public class ArrayStack {
|
|||||||
this.maxSize = maxSize;
|
this.maxSize = maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getStack() {
|
public Object[] getStack() {
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStack(int[] stack) {
|
public void setStack(Object[] stack) {
|
||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,20 +59,20 @@ public class ArrayStack {
|
|||||||
stack[top] = data;
|
stack[top] = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int pop(){
|
public E pop(){
|
||||||
if(isEmpty()){
|
if(isEmpty()){
|
||||||
throw new RuntimeException("栈空了");
|
throw new RuntimeException("栈空了");
|
||||||
}
|
}
|
||||||
int value = stack[top];
|
E value = (E) stack[top];
|
||||||
top --;
|
top --;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int top(){
|
public E top(){
|
||||||
if(isEmpty()){
|
if(isEmpty()){
|
||||||
throw new RuntimeException("栈空了");
|
throw new RuntimeException("栈空了");
|
||||||
}
|
}
|
||||||
int value = stack[top];
|
E value = (E) stack[top];
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ public class ArrayStack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ArrayStack arrayStack = new ArrayStack(5);
|
ArrayStack<Integer> arrayStack = new ArrayStack(5);
|
||||||
Scanner scan = new Scanner(System.in);
|
Scanner scan = new Scanner(System.in);
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.println("a存数, g取数, s查看, exit退出:");
|
System.out.println("a存数, g取数, s查看, exit退出:");
|
||||||
|
@ -13,9 +13,9 @@ public class CalculatorByStack {
|
|||||||
|
|
||||||
private final static int DIV = 4;
|
private final static int DIV = 4;
|
||||||
|
|
||||||
private ArrayStack numStack = new ArrayStack(10);
|
private ArrayStack<Integer> numStack = new ArrayStack(10);
|
||||||
|
|
||||||
private ArrayStack operStack = new ArrayStack(10);
|
private ArrayStack<Integer> operStack = new ArrayStack(10);
|
||||||
|
|
||||||
public void calculate(String expression) {
|
public void calculate(String expression) {
|
||||||
int length = expression.length();
|
int length = expression.length();
|
||||||
|
83
src/dataANDcalc/java/stack/ReversePoland.java
Normal file
83
src/dataANDcalc/java/stack/ReversePoland.java
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package stack;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Stack;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ReversePoland {
|
||||||
|
|
||||||
|
private ArrayStack<Integer> numStack = new ArrayStack(10);
|
||||||
|
|
||||||
|
private List<String> expressList = new ArrayList<>();
|
||||||
|
|
||||||
|
public int calculate(String expression){
|
||||||
|
translateExpress(expression);
|
||||||
|
int length = expressList.size();
|
||||||
|
String operRegex = "[+\\-*/]";
|
||||||
|
Pattern operPattern = Pattern.compile(operRegex);
|
||||||
|
for (int i = 0; i<length;i++){
|
||||||
|
String str = expressList.get(i);
|
||||||
|
if (operPattern.matcher(str).matches()) {
|
||||||
|
if("+".equals(str)){
|
||||||
|
int num1 = numStack.pop();
|
||||||
|
int num2 = numStack.pop();
|
||||||
|
int result = num1 + num2;
|
||||||
|
numStack.push(result);
|
||||||
|
}
|
||||||
|
if("-".equals(str)){
|
||||||
|
int num1 = numStack.pop();
|
||||||
|
int num2 = numStack.pop();
|
||||||
|
int result = num2 - num1;
|
||||||
|
numStack.push(result);
|
||||||
|
}
|
||||||
|
if("*".equals(str)){
|
||||||
|
int num1 = numStack.pop();
|
||||||
|
int num2 = numStack.pop();
|
||||||
|
int result = num2 * num1;
|
||||||
|
numStack.push(result);
|
||||||
|
}
|
||||||
|
if("/".equals(str)){
|
||||||
|
int num1 = numStack.pop();
|
||||||
|
int num2 = numStack.pop();
|
||||||
|
int result = num2/num1;
|
||||||
|
numStack.push(result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
numStack.push(Integer.parseInt(expressList.get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return numStack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void translateExpress(String expression){
|
||||||
|
String[] strArr = expression.split(";");
|
||||||
|
for (String str : strArr){
|
||||||
|
expressList.add(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ReversePoland reversePoland = new ReversePoland();
|
||||||
|
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();
|
||||||
|
int calculate = reversePoland.calculate(expression);
|
||||||
|
System.out.println("计算结果为:" + calculate);
|
||||||
|
}
|
||||||
|
if ("s".equals(handle)){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scan.close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user