以下是我正在參加的課程。這個任務的目的是獲取後綴表達式並將它們轉換爲彙編語言指令。第一次運行後,我無法讓循環打印出正確的說明。有人可以向我解釋我做錯了什麼,因爲我現在已經調試了一段時間,我被卡住了?此外,由於我對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
這是方向的結束。
輸入是三行;每一行應該被獨立處理?輸出究竟有什麼問題?你究竟期待什麼? –
@IanMc輸入的每行應該運行程序。我期待有3個輸入和3個單獨的輸出。更清楚的是,上述兩個輸出是前兩個輸入。該程序有什麼問題是,如果看起來輸出沒有被正確地壓入陣列。第二個輸入的輸出看起來是從第一個輸入獲取輸出,並以第一個輸入作爲第一對耦合指令啓動打印陣列。請看我在哪裏說「第二次循環實際開始的位置」。這應該是第二個輸出的第一條指令。 – Cfs0004