簡體   English   中英

循環和效率Java程序

[英]Loops and efficiency Java Program

以下是我正在上課的項目。 分配的目的是獲取后綴表達式並將其轉換為匯編語言指令。 第一次運行后,我無法使循環打印出正確的說明。 有人可以向我解釋我做錯了什么嗎,因為我已經調試了一段時間,但仍卡住了? 另外,由於我是java的新手,所以對如何使我的程序更高效的任何注釋或您想指出的對我學習的任何解釋都將不勝感激。 我敢肯定還有更多方法可以做到這一點,但是請記住我是新手,有些事情我還沒學到。

.txt輸入文件上的內容:

AB + C- //循環1

ABC +-//循環2

AB-C + 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

公元

溫度1

LD溫度1

SB C

聖溫度2

這是指示的結尾。 //看起來是正確的

循環2:

組裝方向如下:

LD A //從上面復制

公元

溫度1

LD溫度1

SB C

聖溫度2

LD B //第二個循環實際開始的地方

公元C

聖溫度3

LD A

SB TEMP3

ST TEMP4

這是指示的結尾。

我稍稍更改了文件的讀取方式,並包含了經過調整的代碼得到的輸出。 讓我知道這是否會產生預期的結果:

 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM