千锤百炼软工9,11,12
又干了三天关于栈的运算,经过上次大彻大悟后,我又努力的按照自己的思路编写栈的运算,写出来后发现怎么也运行不出来,而且
我反复观看代码发现逻辑上并没有错误,最终经过长达两小时的不断测试,我发现了一个致命的错误,那就是在for循环里面的if else的多重嵌套
语句,有时候不小心少打上的‘}’不会报错,但是会导致逻辑上的错误,这个小问题苦恼了我两个小时,希望以后注意,下面附上奋斗了将近二十小时的代码:
package GOGOGO;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class Stack1 {
public static double getResult(String n,double num1,double num2)
{
double result = 0;
switch (n) {
case "1":
result = num1+num2;
break;
case "2":
result = num1-num2;
break;
case "5":
result = num1*num2;
break;
case "6":
result = num1/num2;
break;
default:
break;
}
return result;
}
public static void main(String[] args) {
String str1 = "333+(6-99)*741";
String str2 = "";
String str3 = "";
Stack<String> stack1 = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
ArrayList<String> list = new ArrayList<String>();
int i=0;
int j=0;
int k=0;
int g=0;
double num1;
double num2;
int num3;
int num4;
double num5;
double num6;
for(char a:str1.toCharArray()) {
if(Character.isDigit(a)) {
g=0;
}
else {
if(k==0) {
str2 = str1.substring(j,i).trim();
j = str1.lastIndexOf(a);
str3 = str1.substring(j,j+1).trim();
list.add(str2);
list.add(str3);
k++;
g++;
}
else {
if(g>=1) {
j = str1.lastIndexOf(a);
str3 = str1.substring(j,j+1).trim();
list.add(str3);
}
else {
str2 = str1.substring(j+1,i).trim();
j = str1.lastIndexOf(a);
str3 = str1.substring(j,j+1).trim();
list.add(str2);
list.add(str3);
}
g++;
}
}
i++;
}
list.add(str1.substring(j+1));
System.out.println(list);
stack1.push("0");
for(String a2:list) {
char ch[] = a2.toCharArray();
if(Character.isDigit(ch[0])) {
stack2.push(a2);
}
else {
if(a2.contentEquals("+")) {
stack1.push("1");
}
if(a2.contentEquals("-")) {
stack1.push("2");
}
if(a2.contentEquals("*")) {
stack1.push("5");
}
if(a2.contentEquals("/")) {
stack1.push("6");
}
if(a2.contentEquals("(")){
stack1.push("8");
continue;
}
if(a2.contentEquals(")")){
num1 = Double.parseDouble(stack2.pop());
num2 = Double.parseDouble(stack2.pop());
double result = getResult(stack1.pop(),num2,num1);
stack2.push(result+"");
stack1.pop();
continue;
}
String str = stack1.pop();
String strr = stack1.pop();
num3 = Integer.parseInt(str);
num4 = Integer.parseInt(strr);
if(num3-num4<=-3.0&&num3!=8&&num4!=8){
num5 = Double.parseDouble(stack2.pop());
num6 = Double.parseDouble(stack2.pop());
double result = getResult(strr,num6,num5);
stack2.push(result+"");
stack1.push(str);
}
else {
stack1.push(strr);
stack1.push(str);
}
}
}
stack1.push("0");
do {
String str = stack1.pop();
String strr = stack1.pop();
num3 = Integer.parseInt(str);
num4 = Integer.parseInt(strr);
if(num3-num4<=-3.0){
num5 = Double.parseDouble(stack2.pop());
num6 = Double.parseDouble(stack2.pop());
double result = getResult(strr,num6,num5);
stack2.push(result+"");
}
if(num3-num4>=-2&&num3-num4<=2&&num4==0) {
num5 = Double.parseDouble(stack2.pop());
num6 = Double.parseDouble(stack2.pop());
double result = getResult(str,num6,num5);
stack2.push(result+"");
break;
}
if(num3-num4>=-2&&num3-num4<=2) {
num5 = Double.parseDouble(stack2.pop());
num6 = Double.parseDouble(stack2.pop());
double result = getResult(strr,num6,num5);
stack2.push(result+"");
stack1.push(str);
}
}while(stack1.peek()!="0");
System.out.print(stack2.peek());
}
}
虽然非常的痛苦并且感觉我的进度非常的慢,我几乎用了我所有的时间用来思考这个栈运算,但是我并不是很后悔,希望我再接再厉。