2016-02-24 87 views
0

以下是我正在參加的課程。這個任務的目的是獲取後綴表達式並將它們轉換爲彙編語言指令。第一次運行後,我無法讓循​​環打印出正確的說明。有人可以向我解釋我做錯了什麼,因爲我現在已經調試了一段時間,我被卡住了?此外,由於我對Java更新,如何使我的程序更高效或任何您想指出要我學習的解釋的任何意見將不勝感激。我相信有更多的方法可以做到這一點,但請記住我是新人,還有一些我還沒有學到的東西。循環和效率Java程序

什麼是對的.txt的輸入文件:

AB + C- //環1個

ABC + - //環2

ABC + DEF - + $ //循環3

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 


public class PostfixConverter{ 

static int top = 0; 
static String [] mainStack = new String [100]; 

public static void main(String args[]) throws Exception{ 
String string = null; 
String asterisk = "*"; 
String divisor = "/"; 
String plus = "+"; 
String minus = "-"; 
int temp =0; 
int directionCounter = 0; 
String load = "LD "; 
String multiply = "ML "; 
String add = "AD "; 
String div = "DV "; 
String subtract = "SB "; 
String store = "ST TEMP"; 
String tempString = "TEMP"; 
String [] directions = new String [100]; 
String example = "AB+C-"; 
PostfixConverter s = new PostfixConverter(); 

try{ 
//file reader code 
FileReader file = new FileReader  ("/Users/ChristopherSchubert/Desktop/PostfixMachineLangInput(1).txt"); 
BufferedReader reader = new BufferedReader(file); 


String text = ""; 
String line = reader.readLine(); 
while (line!= null) 
{ 
    text += line; 
    line = reader.readLine(); 
    example = text; 

    //for loop to print directions 
    for (int i=0; i<example.length(); i++) { 


    //get letter entered by user 1 by 1 
    char letter = example.charAt(i); 

    //convert char to string 
    String convertedChar = java.lang.String.valueOf(letter); 

    //finds operands in order or priority 
    //multiply character 
    if (convertedChar.equals(asterisk)){ 
     String outcome; 
     String multiplyReturn = PostfixConverter.multiply(string); 
     String loadmulReturn = PostfixConverter.multiply(string); 
     directions[directionCounter] = load + loadmulReturn; 
     directionCounter++; 
     directions[directionCounter] = multiply + multiplyReturn; 
     directionCounter++; 
     temp++; 
     outcome = tempString + java.lang.String.valueOf(temp); 
     directions[directionCounter] = store +  java.lang.String.valueOf(temp); 
     directionCounter++; 
     s.push(outcome); 
     } 


     //division character 
     else if (convertedChar.equals(divisor)){ 
      String outcome; 
      String divisionReturn = PostfixConverter.addition(string); 
      String loaddivReturn = PostfixConverter.addition(string); 
      directions[directionCounter] = load + loaddivReturn; 
      directionCounter++; 
      directions[directionCounter] = div + divisionReturn; 
      directionCounter++; 
      temp++; 
      outcome = tempString + java.lang.String.valueOf(temp); 
      directions[directionCounter] = store + java.lang.String.valueOf(temp); 
      directionCounter++; 
      s.push(outcome); 
     } 

     //addition character 
     else if (convertedChar.equals(plus)){ 
      String outcome; 
      String additionReturn = PostfixConverter.addition(string); 
      String loadAddReturn = PostfixConverter.addition(string); 
      directions[directionCounter] = load + loadAddReturn; 
      directionCounter++; 
      directions[directionCounter] = add + additionReturn; 
      directionCounter++; 
      temp++; 
      outcome = tempString + java.lang.String.valueOf(temp); 
      directions[directionCounter] = store + java.lang.String.valueOf(temp); 
      directionCounter++; 
      s.push(outcome); 
     } 

     //subtraction character 
     else if (convertedChar.equals(minus)){ 
      String outcome; 
      String subtractionReturn = PostfixConverter.addition(string); 
      String loadsubReturn = PostfixConverter.addition(string); 
      directions[directionCounter] = load + loadsubReturn; 
      directionCounter++; 
      directions[directionCounter] = subtract + subtractionReturn; 
      directionCounter++; 
      temp++; 
      outcome = tempString + java.lang.String.valueOf(temp); 
      directions[directionCounter] = store + java.lang.String.valueOf(temp); 
      directionCounter++; 
      s.push(outcome); 
     } 

     //letter character 
     else { 
      s.push(convertedChar); 
     } 

    } 

    //print out the instructions 
    System.out.println("Assembly Directions are as follows: "); 
    int printDirections = 0; 
    for (int i=0; i< directionCounter; i++){ 
     System.out.println(directions[printDirections]); 
     printDirections++; 
    } 
    printDirections=0; 
    directionCounter=0; 
    System.out.println("This is the end of the directions."); 
    System.out.println(""); 
    directionCounter = 0; 
    temp = 0; 
    top = 0; 

} 
} 
catch (FileNotFoundException exception) 
{ 
    System.out.println("The file was not found."); 
    } 
} 


//multiply method 
public static String multiply(String a){ 
    String multVariable = PostfixConverter.pop(mainStack[top]); 
    top--; 
    return multVariable; 
} 

//addition method 
public static String addition(String a){ 

    String addVariable = PostfixConverter.pop(mainStack[top]); 
    top--; 
    return addVariable; 
} 

//subtraction method 
public static String subtraction(String a){ 
    String subVariable = PostfixConverter.pop(mainStack[top]); 
    top--; 
    return subVariable; 
} 

//division method 
public static String division(String a){ 
    String divVariable = PostfixConverter.pop(mainStack[top]); 
    top--; 
    return divVariable; 
    } 

public static boolean empty(){ 
    if (top == -1) 
    return true; 
    else 
    return false; 

} 

public static String pop(String j){ 
    if (empty()){ 
    System.out.println("Stack Underflow"); 
    System.exit(1); 
    } 
    return mainStack[top - 1]; 
} 

public void push (String x){ 
    if (top == 99){ 
    System.out.println("Stack Overflow"); 
    System.exit(1); 
    }else 
    mainStack[top] = x; 
    top++; 
}//end push 

} 

這裏是正被打印出來:

循環1:

大會路線如下:

LD A

AD乙

ST TEMP1

LD TEMP1

SBÇ

ST TEMP2

這是方向的結束。 //看起來是正確

循環2:

大會路線如下:

LD A //從AD乙上述

ST TEMP1

複製LD TEMP1

SB C

ST TEMP2

LD乙//其中第二循環實際開始

廣告C

ST TEMP3

LD A

SB TEMP3

ST TEMP4

這是方向的結束。

+0

輸入是三行;每一行應該被獨立處理?輸出究竟有什麼問題?你究竟期待什麼? –

+0

@IanMc輸入的每行應該運行程序。我期待有3個輸入和3個單獨的輸出。更清楚的是,上述兩個輸出是前兩個輸入。該程序有什麼問題是,如果看起來輸出沒有被正確地壓入陣列。第二個輸入的輸出看起來是從第一個輸入獲取輸出,並以第一個輸入作爲第一對耦合指令啓動打印陣列。請看我在哪裏說「第二次循環實際開始的位置」。這應該是第二個輸出的第一條指令。 – Cfs0004

回答

1

我稍微改變了讀取文件的方式,並將調整後的代碼包含在輸出中。讓我知道如果這造成了預期的結果:

String line = ""; 

while ((line = reader.readLine()) != null) 
{ 
    example = line; 
    // Continue to process ... 

編輯:這裏是演示了什麼,我指的是在評論(通知「)processOperand(」引進,以及如何使一個更加模塊化的方法代碼更可讀)。它只是的(即,每個操作調用相同的代碼塊傳遞的唯一參數)尋找公共代碼,並使其「可用」用於各種箱子

public class PostfixConverter { 

    static int top = 0; 
    static String[] mainStack = new String[100]; 

    final static String asterisk = "*"; 
    final static String divisor = "/"; 
    final static String plus = "+"; 
    final static String minus = "-"; 
    final static String store = "ST TEMP"; 

    static int temp = 0; 
    static int directionCounter = 0; 
    static String[] directions = new String[100]; 
    static PostfixConverter s = new PostfixConverter(); 
    static String tempString = "TEMP"; 

    public static void main(String args[]) throws Exception { 

     String string = null; 

     String load = "LD "; 
     String multiply = "ML "; 
     String add = "AD "; 
     String div = "DV "; 
     String subtract = "SB "; 

     String example = "AB+C-"; 

     try { 
      // file reader code 
      FileReader file = new FileReader("...."); 
      BufferedReader reader = new BufferedReader(file); 

      String line = ""; 

      while ((line = reader.readLine()) != null) { 
       example = line; 

       // for loop to print directions 
       for (int i = 0; i < example.length(); i++) { 

        // get letter entered by user 1 by 1 
        char letter = example.charAt(i); 

        // convert char to string 
        String convertedChar = java.lang.String.valueOf(letter); 

        // finds operands in order or priority 
        // multiply character 
        if (convertedChar.equals(asterisk)) { 

         processOperand(PostfixConverter.multiply(string), PostfixConverter.multiply(string), load, 
           multiply); 
        } 

        // division character 
        else if (convertedChar.equals(divisor)) { 
         processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, div); 
        } 

        // addition character 
        else if (convertedChar.equals(plus)) { 
         processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, add); 
        } 

        // subtraction character 
        else if (convertedChar.equals(minus)) { 
         processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, 
           subtract); 

        } 
        // letter character 
        else { 
         s.push(convertedChar); 
        } 

       } 

       // print out the instructions 
       System.out.println("Assembly Directions are as follows: "); 
       int printDirections = 0; 
       for (int i = 0; i < directionCounter; i++) { 
        System.out.println(directions[printDirections]); 
        printDirections++; 
       } 
       printDirections = 0; 
       directionCounter = 0; 
       System.out.println("This is the end of the directions."); 
       System.out.println(""); 
       directionCounter = 0; 
       temp = 0; 
       top = 0; 

      } 
     } catch (FileNotFoundException exception) { 
      System.out.println("The file was not found."); 
     } 
    } 

    private static void processOperand(String postFileConverterOutput, String postFileConverterOutput2, 
      String instruction1, String instruction2) { 
     String outcome; 
     String opReturn1 = postFileConverterOutput; 
     String opReturn2 = postFileConverterOutput2; 
     directions[directionCounter] = instruction1 + opReturn2; 
     directionCounter++; 
     directions[directionCounter] = instruction2 + opReturn1; 
     directionCounter++; 
     temp++; 
     outcome = tempString + java.lang.String.valueOf(temp); 
     directions[directionCounter] = store + java.lang.String.valueOf(temp); 
     directionCounter++; 
     s.push(outcome); 
    } 

    // multiply method 
    public static String multiply(String a) { 
     String multVariable = PostfixConverter.pop(mainStack[top]); 
     top--; 
     return multVariable; 
    } 

    // addition method 
    public static String addition(String a) { 

     String addVariable = PostfixConverter.pop(mainStack[top]); 
     top--; 
     return addVariable; 
    } 

    // subtraction method 
    public static String subtraction(String a) { 
     String subVariable = PostfixConverter.pop(mainStack[top]); 
     top--; 
     return subVariable; 
    } 

    // division method 
    public static String division(String a) { 
     String divVariable = PostfixConverter.pop(mainStack[top]); 
     top--; 
     return divVariable; 
    } 

    public static boolean empty() { 
     if (top == -1) 
      return true; 
     else 
      return false; 

    } 

    public static String pop(String j) { 
     if (empty()) { 
      System.out.println("Stack Underflow"); 
      System.exit(1); 
     } 
     return mainStack[top - 1]; 
    } 

    public void push(String x) { 
     if (top == 99) { 
      System.out.println("Stack Overflow"); 
      System.exit(1); 
     } else 
      mainStack[top] = x; 
     top++; 
    }// end push 

} 

創建的輸出的例子:

Assembly Directions are as follows: 
LD A 
AD B 
ST TEMP1 
LD TEMP1 
SB C 
ST TEMP2 
This is the end of the directions. 

Assembly Directions are as follows: 
LD B 
AD C 
ST TEMP1 
LD A 
SB TEMP1 
ST TEMP2 
This is the end of the directions. 

Assembly Directions are as follows: 
LD A 
SB B 
ST TEMP1 
LD TEMP1 
AD C 
ST TEMP2 
LD E 
SB F 
ST TEMP3 
LD D 
AD TEMP3 
ST TEMP4 
This is the end of the directions. 
+0

現在可以工作。非常感謝你。至於學習更多,你有什麼不同的代碼?你會建議添加/減少任何代碼。感謝您的幫助。 – Cfs0004

+0

我鼓勵你尋找重複的代碼塊(有幾個)並將它們重構成方法。算術代碼塊共享很多共同點;您可以大大簡化代碼,從而使其更易於閱讀和使用此方法進行修改。合理?如果你有興趣,我可以發佈例子。 (你也可以請標記答案是有用的,如果它幫助你......歡呼)。 –

+0

我標記爲有用。再次感謝您的幫助。是的,如果你不介意發佈一個非常感謝的例子。我發佈了這個問題進行調試,但更重要的是學習如何提高我的代碼,這將是一個很大的幫助。 – Cfs0004