使用的类
- 计算器类
- 线性栈类(使用线性表存储数据)
- 线性表类
- 主类
运行结果:
图解:
计算器类
package Class.Stack.SeqStack;
import Class.LinearList.LinearList;
public class Calculator {
public int priority(int ch) {
if (ch == '+' || ch == '-') {
return 0;
} else if (ch == '*' || ch == '/') {
return 1;
} else {
throw new RuntimeException("优先级判断发生错误!");
}
}
public boolean isOpera(int ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')';
}
public int getResult(int before, int behind, int opera) {
switch (opera) {
case '+':
return before + behind;
case '-':
return before - behind;
case '*':
return before * behind;
case '/':
return before / behind;
default:
throw new RuntimeException("运算符计算发生错误!");
}
}
public int calculate(String expression) {
char ch;
int index = 0;
int before;
int behind;
int opera;
int result;
int finalResult;
SeqStack numStack = new SeqStack(new LinearList(expression.length()));
SeqStack operaStack = new SeqStack(new LinearList(expression.length()));
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
if (this.isOpera(ch)) {
if (operaStack.isEmpty() || ch == '(') {
operaStack.push(ch);
} else {
if (ch == ')') {
while (operaStack.peek() != '(') {
behind = numStack.pop();
before = numStack.pop();
opera = operaStack.pop();
result = this.getResult(before, behind, opera);
numStack.push(result);
}
operaStack.pop();
} else if (operaStack.peek() == '(') {
operaStack.push(ch);
} else if (this.priority(ch) <= this.priority(operaStack.peek())) {
behind = numStack.pop();
before = numStack.pop();
opera = operaStack.pop();
result = this.getResult(before, behind, opera);
numStack.push(result);
operaStack.push(ch);
} else {
operaStack.push(ch);
}
}
} else {
numStack.push(ch - 48);
}
index++;
if (index == expression.length()) {
break;
}
}
while (true) {
behind = numStack.pop();
before = numStack.pop();
opera = operaStack.pop();
result = this.getResult(before, behind, opera);
numStack.push(result);
if (numStack.length() == 1) {
break;
}
}
return numStack.peek();
}
}
顺序栈类
package Class.Stack.SeqStack;
import Class.LinearList.LinearList;
import Class.Stack.Stack;
public class SeqStack implements Stack {
private LinearList linearList;
public SeqStack(LinearList linearList) {
this.linearList = linearList;
}
@Override
public boolean isEmpty() {
return linearList.isEmpty();
}
@Override
public void push(int num) {
linearList.tailInsert(num);
}
@Override
public int pop() {
return linearList.tailPop();
}
@Override
public int peek() {
return linearList.getInt(linearList.length - 1);
}
public int length() {
return linearList.size();
}
}
package Class.Stack;
public interface Stack {
public abstract boolean isEmpty();
public abstract void push(int num);
public abstract int pop();
public abstract int peek();
}
线性表:
package Class.LinearList;
public class LinearList {
public int length = 0;
private int[] arr;
private static final int MIN_CAPACITY = 3;
public LinearList(int length) {
if (length >= MIN_CAPACITY) {
arr = new int[length];
} else {
System.out.println("要求线性表最小长度为3!");
}
}
public LinearList() {
this(MIN_CAPACITY);
}
public LinearList(int []arr) {
if(arr.length <= MIN_CAPACITY) {
throw new RuntimeException("队列长度过小!");
}
this.arr = arr;
length = arr.length;
}
public int getCapacity() {
return arr.length;
}
public boolean isEmpty() {
return length == 0;
}
public boolean isFull() {
return length == arr.length;
}
public int size() {
return length;
}
public int keySearch(int key) {
if (isEmpty()) {
throw new RuntimeException("线性表为空!");
}
for (int i = 0; i < length; i++) {
if (arr[i] == key) {
return i;
}
}
System.out.println("线性表中没有该元素");
return 0;
}
public void keyRemove(int key) {
if (isEmpty()) {
System.out.println("线性表为空!");
return;
}
for (int i = 0; i < length; i++) {
if (arr[i] == key) {
arr[i] = 0;
length--;
return;
}
}
System.out.println("线性表中没有该元素");
}
public void insert(int value, int n) {
if (isFull()) {
System.out.println("线性表已满");
return;
}
if (n >= 1 && n <= length + 1) {
for (int i = length; i >= n; i--) {
arr[i] = arr[i - 1];
}
arr[n - 1] = value;
} else if (n > length + 1) {
System.out.println("插入位置过大,自动插入到尾部");
this.insert(value, length + 1);
} else if (n < 1) {
System.out.println("插入位置过小,自动插入到头部");
this.insert(value, 1);
}
length++;
}
public void tailInsert(int value) {
this.insert(value, length + 1);
}
public int remove(int n) {
if (isEmpty()) {
throw new RuntimeException("线性表为空!");
}
if (n >= 1 && n <= length) {
int temp = arr[n - 1];
for (int i = n - 1; i <= length - 2; i++) {
arr[i] = arr[i + 1];
}
arr[length - 1] = 0;
length--;
return temp;
} else {
throw new RuntimeException("删除位置有误");
}
}
public int tailPop() {
int tail = remove(length);
return tail;
}
public void setValue(int value, int n) {
if (isEmpty()) {
System.out.println("当前线性表为空");
}
if (n > length || n < 1) {
System.out.println("访问越界");
return;
} else {
arr[n - 1] = value;
}
}
public void showInfo() {
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println("");
}
public int getInt(int index) {
return arr[index];
}
}