我的代碼將不完整的運算式組合與右括號“)”組合成一個完整的運算式組合,左括號“(”的位置正確。如果右括號和左括號仍然不相等,則使第一個運算式到最后一個 a優先級,如果仍然不相等,則對第一個索引做一個左括號。最多三個運算式(運算式=數字運算子數字)。
樣本輸入:4 3 ) * 4 - 2 ) * 6 - 6 ) ) )
樣本輸出: ((4 3 ) * ((4 - 2 ) * (6 - 6 ) ) )
當僅放置 1-2 個右括號時,我的代碼有效。如果超過兩個,則程式掛起。
源代碼:
import java.util.Scanner;
import java.lang.String;
public class brackets { //Declaration of Variables
static String Str, Strr="";
static Integer ope=0;
static int temp=0,lent,pr=0,prl=0,prr=0; //Temp = Opening Brackets placed, lent = string length, pr=Closing Brackets, prl = number of opening brackets made from last index, prr=TBA
static Scanner scan = new Scanner(System.in);
public static void main(String[]args){
System.out.println("Enter the combined experessions: ");
Str = scan.nextLine();
lent = Str.length(); //Setting the full expression to a string (Str)
for(int i=0; i<lent;i ){ //Finding how many Closing Brackets There are
if(Str.charAt(i)==')'){
pr ;
}
}
Popping(lent); //Method
System.out.print(Strr); //Printing Final Ouput
scan.close();
}
public static void Popping(int lent){
for(int j =lent-1; j>-1;j--){ //I set j to lent because I want to search from rightmost to leftmost
if(Str.charAt(j)==')') //I need to find a closing bracket first before the program could start to input an opening bracket
prr ;
if(prr>0){
if(Str.charAt(j)==' '||Str.charAt(j)=='-'||Str.charAt(j)=='*'||Str.charAt(j)=='/'||(j<=lent-2&&Character.isDigit(Str.charAt(j 1))&&Str.charAt(j)==')')){ //Finding an operator or finding a closing bracket which has a digit next to it
ope ;
}
if(ope==2){ //finding two operators mean that I can now put an opening bracket
Strr = '(' Strr;
temp ;
ope=0;
}
}
Strr = Str.charAt(j) Strr;
if(prr>0){
if(j==0&&temp!=pr){ //If J==0 and Closing brackets still is not equal to Opening Brackets, I'll set opening bracket to the 0 index
Strr = '(' Strr;
temp ;
}
}
}
while(temp!=pr) { // If still not equal, I'll set an opening bracket to the second opening bracket there is, making the last two expression a priority
for(int j =lent-1; j>-1;j--){
if(Str.charAt(j)=='(')
prl ;
if(prl>1&&Str.charAt(j)=='('){
Strr= Strr.substring(0,j-1) '(' Strr.substring(j,lent-1);
temp ;
}
}
}
}
}
uj5u.com熱心網友回復:
我相信問題出在你的最后一個while回圈上。
問題是您正在運行Str(您的原始字串),尋找左括號。然而,你的原始字串4 3 ) * 4 - 2 ) * 6 - 6 ) ) )中不包含任何左括號,所以Str.charAt(j)=='('從來都不是true和temp從來沒有得到增量相匹配pr。因此條件temp!=pr總是true,所以while回圈繼續執行。
您可能希望Str將此回圈中所有出現的 更改為Strr,這是您一直在插入左括號的字串。
然后,您將遇到以下行的問題:
Strr= Strr.substring(0,j-1) '(' Strr.substring(j,lent-1);
如果j > 0,這條線將在位置排除字符j-1在Strr。如果j == 0這會導致StringIndexOutOfBoundsException拋出 a 。該.substring方法的第二個引數不是要包含在子字串中的最后一個字符的索引,而是之后下一個字符的索引。Strr從開始到但不包括位置中的字符的子串j是Strr.substring(0,j)代替Strr.substring(0,j-1)。
另外,為簡單起見,如果您希望子字串運行到??字串的末尾,則可以省略第二個引數。在下面的行中,我對您的第二次呼叫進行了修改.substring:
Strr= Strr.substring(0,j) '(' Strr.substring(j);
在對您的類進行這些更改后,我能夠在您的示例輸入上運行它4 3 ) * 4 - 2 ) * 6 - 6 ) ) )并從中獲取輸出((4 3 ) *(( 4 - 2 ) *( 6 - 6 ) ) )。
但是,我隨后在 上運行了您的代碼1 5 ) / 4 3 ) * 4 - 2 ) * 6 - 6 ) ) ),它再次掛起。
這里的問題是,當您的代碼到達while回圈的頂部時,它將插入四個左括號,剩下兩個仍然需要關閉。但是,運行for回圈會向 增加三個左括號str,占temp7,當pr是 6 時。7 不等于 6,因此while回圈體再次運行,插入更多括號,依此類推,直到字串變得太大而無法放入記憶體并且程式崩潰。
最簡單的解決方法是將條件添加temp<pr到行中
if(prl>1&&Strr.charAt(j)=='('){
為你帶來
if(prl>1&&Strr.charAt(j)=='('&&temp<pr){
這樣你就不會插入太多的左括號。
最后,為了可讀性,我強烈建議您為變數提供更具描述性的名稱。例如,temp對于一個計算到目前為止插入的左括號數量的變數來說,這不是一個好名字:更好的名字應該是openParenCount或者insertedOpenParensCount. 同樣,pr最好作為closeParenCount. Str可以重命名為originalString和 Strr到modifiedString。
uj5u.com熱心網友回復:
我不確定您的代碼在 while 回圈中究竟做了什么,但是我可以確認一件事,當輸入超過 3 個關閉的“)”括號時,您的代碼進入 while 回圈
while(temp!=pr) { // If still not equal, I'll set an opening bracket to the second opening bracket there is, making the last two expression a priority
for(int j =lent-1; j>-1;j--){
if(Str.charAt(j)=='(')
prl ;
if(prl>1&&Str.charAt(j)=='('){
Strr= Strr.substring(0,j-1) '(' Strr.substring(j,lent-1);
temp ;
}
}
}
在這一點上,您while檢查如果temp!=pr呼叫此條件是否始終為真,從而創建一個無限回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403637.html
標籤:
下一篇:Python中的字串操作:
