
如果我這樣寫的話它會報錯
uj5u.com熱心網友回復:
package stack;
/**
* Created with IntelliJ IDEA.
*
* @author: heiye
* @Date: 2020/3/23
* @Time: 22:13
* Description: No Description
*/
public class OperationStackDemo {
public static void main(String[] args) {
//中綴運算式
String expression = "3*7+1-25+4/2";
//定義數堆疊和符號堆疊
OperStack numStack = new OperStack(10);
OperStack operStack = new OperStack(10);
//定義運算需要的變數
int index = 0;
char ch;
int res;
String keepNum = "";
while (true) {
//遍歷獲取字串中每一個字符
ch = expression.substring(index, ++index).charAt(0);
//判斷取的字符是數字還是運算子
if (operStack.isOper(ch)) {
//如果是運算子,則需要判斷當前符號堆疊中是否存在運算子
if (!operStack.isEmpty()) {
//如果存在運算子則需要判斷當前運算子是否小于堆疊中的運算子
int oper = operStack.peek();
if (operStack.operFlag(ch) <= operStack.operFlag(oper)) {
//如果小于則需要彈出堆疊中運算子進行運算
operation(operStack, numStack);
//將當前運算子加入到符號堆疊中
operStack.push(ch);
} else {
//如果大于或等于則直接存入即可
operStack.push(ch);
}
} else {
//如果為空則直接插入
operStack.push(ch);
}
} else {
//如果是數字則直接存入到數堆疊中
//實作多位數字運算
keepNum += ch;
if (index >= expression.length()) {
numStack.push(Integer.parseInt(keepNum));
} else {
if (operStack.isOper(expression.charAt(index))) {
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
//判斷是否截取完畢
if (index >= expression.length()) {
break;
}
}
//分別彈出數堆疊和符號堆疊進行計算
while (true) {
//如果符號堆疊為空,則數堆疊中最后的數字就是結果
if (!operStack.isEmpty()) {
//計算
operation(operStack, numStack);
} else {
//如果為空則獲取結果
res = numStack.prop();
break;
}
}
System.out.println("最后的計算結果:" + res);
}
public static void operation(OperStack operStack, OperStack numStack) {
int oper = operStack.prop();
int num1 = numStack.prop();
int num2 = numStack.prop();
//將運算結果再次存入到堆疊中
int res = operStack.operation(num1, num2, oper);
numStack.push(res);
}
}
class OperStack {
private int maxSize;
private int[] stack;
private int top;
public OperStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
top = -1;
}
/**
* 判斷堆疊是否空
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
* 堆疊滿
*
* @return
*/
public boolean isFull() {
return top == this.maxSize - 1;
}
/**
* 添加資料到堆疊
*
* @param data
*/
public void push(int data) {
//添加之前判斷是否滿
if (isFull()) {
System.out.println("堆疊已滿!");
return;
}
top++;
stack[top] = data;
}
/**
* 彈堆疊
*
* @return
*/
public int prop() {
//彈之前判斷是否空
if (isEmpty()) {
System.out.println("堆疊為空!");
return 0;
}
int value = stack[top];
top--;
return value;
}
/**
* 查看堆疊
*/
public void show() {
if (isEmpty()) {
System.out.println("堆疊為空!");
return;
}
int i = top;
while (i > -1) {
System.out.println("堆疊資料:" + stack[i]);
i--;
}
}
/**
* 判斷是否是符號
*
* @param ch
*/
public boolean isOper(char ch) {
return ch == '*' || ch == '/' || ch == '+' || ch == '-';
}
/**
* 運算方法
*
* @param num1
* @param num2
* @param oper
* @return
*/
public int operation(int num1, int num2, int oper) {
int res = 0;
switch (oper) {
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
default:
System.out.println("暫不支持該運算子~");
break;
}
return res;
}
/**
* 判斷符號優先級
*
* @param ch
* @return
*/
public int operFlag(int ch) {
char[] chars = {'+', '-', '*', '/'};
int ZERO = 0, ONE = 1, TWO = 2, THREE = 3;
if (ch == chars[TWO] || ch == chars[THREE]) {
return 1;
} else if (ch == chars[ZERO] || ch == chars[ONE]) {
return 0;
} else {
return -1;
}
}
/**
* 獲取堆疊頂的第一個元素
*
* @return
*/
public int peek() {
return stack[top];
}
}
剛好之前有寫過一個中綴運算式的計算器小案例
uj5u.com熱心網友回復:
將字串替換成鍵盤輸入即可轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/135816.html
標籤:Eclipse
