个人想法的优化:
Java获取字符串计算公式的结果_JackSparrow001的博客-CSDN博客
public class ArrayStackCopy {
//栈的容量Maximum capacity
private int stackMaxCapacity;
//栈的容器 -》 数组
private int []stack;
//记录栈顶的index
private int top = -1;
//初始化,栈的容器
public ArrayStackCopy(int stackMaxCapacity){
this.stackMaxCapacity = stackMaxCapacity;
this.stack = new int[stackMaxCapacity];
}
//判断栈是否满
public boolean isFull(){
return stackMaxCapacity == top+1;
}
//判断栈是否为空
public boolean isEmpty(){
return top == -1;
}
//压栈
public void push(int val){
//存入栈中,先判断该栈是否为空
if (isFull()){
throw new RuntimeException("栈已经满了!!!");
}
//记录栈顶的属性,先增加1
top++;
this.stack[top] = val;
}
//弹栈
public int pop(){
//先判断栈是否为空
if (isEmpty()){
throw new RuntimeException("栈已空!!!");
}
//取出栈顶的值,并保存到返回值的变量中
int value = this.stack[this.top];
this.top--;
return value;
}
//遍历栈
public void list(){
//先判断栈是否为空
if (isEmpty()){
throw new RuntimeException("栈是空的!!");
}
//遍历栈的容器 -- 数组
for (int i = 0; i < top; i++) {
System.out.println(stack[i]);
}
}
//获取栈的元素的个数
public int size(){
return this.top+1;
}
//判断是基本符号的方法 * / + -
public boolean isSymbol(char c ){
return c == '*' || c == '/' || c == '+' || c == '-';
}
//比较符号的优先级
public int priority(int val){
if (val == '*' || val == '/'){
return 1;
}
if (val == '+' || val == '-'){
return 0;
}
return -1;
}
//获取栈顶数据的方法
public int peek(){
return stack[top];
}
//获取栈容量的方法
public int getStackMaxCapacity(){
return this.stackMaxCapacity;
}
//计算两个数的运算结果
public int calculate(int num1,int num2,int opera){
int result = -1;
switch (opera){
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
}
return result;
}
//将栈进行反转
public ArrayStackCopy reversalStack(ArrayStackCopy stackCopy){
ArrayStackCopy arrayStackCopy = new ArrayStackCopy(stackCopy.getStackMaxCapacity());
if (stackCopy == null){
return null;
}
int size = stackCopy.size();
for (int i = 0; i < size; i++) {
arrayStackCopy.push(stackCopy.pop());
}
return arrayStackCopy;
}
}
public class TestStackCopy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("====================运算表达式=======================");
System.out.print("请输入整数的运算表达式(注:Exit 退出):");
String line = scanner.nextLine();
//匹配是否合适
if ("Exit".equals(line)){
System.out.println("退出程序。。。。");
break;
}
if (!TestStackCopy.isMatch(line)){
System.out.println("输入的运算表达式有误!!");
continue;
}
int rest = TestStackCopy.StrCalculate(line);
System.out.println(line+"="+rest+"\n");
}
}
public static boolean isMatch(String str){
//匹配运算表达式
String regex = "^\\d([-\\+\\*\\/]\\d+)+$";
boolean ismatch = Pattern.matches(regex, str);
return ismatch;
}
public static int StrCalculate(String s){
//给定运算的字符串
String str = s;
//创建存放数字的栈
ArrayStackCopy numStack = new ArrayStackCopy(str.length());
ArrayStackCopy symbolStack = new ArrayStackCopy(str.length());
String value = "";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
//是符号
if (symbolStack.isSymbol(c)){
//先判断symbolStack的容器是否为空
if (!symbolStack.isEmpty()){
//先判断栈顶的符号和当前字符串取出的符号比较优先级,如果栈顶的优先级大于当前字符的优先级
//取出栈顶的符号,并取出数字栈的栈顶两个数字进行运算,并将结果放回数字栈的栈顶
//将字符串取出的符号存入当前栈顶
if (symbolStack.priority(c) < symbolStack.priority(symbolStack.peek())||
(symbolStack.priority(c)== 1 && symbolStack.priority(symbolStack.peek()) == 1)){
int num1 = numStack.pop();
int num2 = numStack.pop();
int res = numStack.calculate(num1, num2, symbolStack.pop());
//将该结果入数字栈
numStack.push(res);
//符号栈顶
symbolStack.push(c);
}else {
symbolStack.push(c);
}
}else {
symbolStack.push(c);
}
//是数字
}else {
//用一个字符变量记录数字
value += c;
if (i == str.length()-1){
numStack.push(Integer.valueOf(value));
}else {
char c1 = str.substring(i + 1, i + 2).charAt(0);
if (symbolStack.isSymbol(c1)){
numStack.push(Integer.valueOf(value));
//清空缓存数字的字符串
value ="";
}
}
}
}
//可以判断是否symbolStack的栈顶元素是否为 * 或 / ,如果是,就取出进行运算 (下面比较的是字符的ascii码值)
if (symbolStack.peek() == '*' || symbolStack.peek() == '/'){
int num1 = numStack.pop();
int num2 = numStack.pop();
int rest = numStack.calculate(num1, num2, symbolStack.pop());
numStack.push(rest);
}
//将栈的元素进行反转
numStack = numStack.reversalStack(numStack);
symbolStack = symbolStack.reversalStack(symbolStack);
while (true){
//当数字栈为空时,停止遍历
if (symbolStack.isEmpty()){
break;
}
//取出数字
int num1 = numStack.pop();
int num2 = numStack.pop();
int symbol = symbolStack.pop();
//这里由于栈已经反转过了,所以下面的方法参数位置需要更改
int res = numStack.calculate(num2, num1, symbol);
//将结果存入栈中
numStack.push(res);
}
return numStack.pop();
}
}