这是我的源代码:
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];
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("ignore for now");
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
example = line;
// for loop to print directions
Characterloop:
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;
}reader.close();
} 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) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
break Characterloop;
}
String multVariable = PostfixConverter.pop(mainStack[top]);
top--;
return multVariable;
}
// addition method
public static String addition(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String addVariable = PostfixConverter.pop(mainStack[top]);
top--;
return addVariable;
}
// subtraction method
public static String subtraction(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String subVariable = PostfixConverter.pop(mainStack[top]);
top--;
return subVariable;
}
// division method
public static String division(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String divVariable = PostfixConverter.pop(mainStack[top]);
top--;
return divVariable;
}
public static boolean empty() {
boolean check = false;
if (top < 0){
check = true;
}
else{
check = false;
}return check;
}
public static String pop(String j) {
if (top < 0) {
System.out.println("Stack is empty");
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;
System.out.println("Top:" + top + "||" + " Array: " + mainStack[top]);
top++;
}// end push
}
当给出无效字符串时,如何让此方法停止运行循环?我问的项目是下面的星号**问题**。比方说,我给了后缀字符串“AB + * AB +”和“AB +”。显然这是无效的,因为不能有两个只有两个操作数的运算符。如何跳过第一个语句并打印“无效参数”并继续下一个字符串(AB +)?我研究了break和continue,但我不知道如何在另一种方法中实现它。这是一个家庭作业,只是想指出正确的方向。
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
**break Characterloop;**
}
答案 0 :(得分:0)
这样的事情可以使用例外和try / catch来完成。但是这样做应该谨慎使用。抛出异常不应该用于&#34;返回&#34;给用户一些东西。但抛出异常有好处:
break
语句按预期工作,则没有编译时间检查验证该方法是否仅被调用,如果堆栈中的方法调用包含适当的标签让我们简单地解决问题代码:
<强>断裂强>
void a() {
label1: while (check()) {
b();
}
}
void b() {
c();
}
void c() {
if (check2()) {
break label1;
}
doSomething();
}
继续强>
void d() {
label2: while(check()) {
e();
}
doSomething();
}
void e() {
f();
}
void f() {
if (check2()) {
continue label2;
}
doSomething();
}
<强>断裂强>
class MyException1 extends Exception {
}
void a() {
try {
while (check()) {
b();
}
} catch (MyException1 ex) {
// we're already outside the loop; nothing to be done here
}
}
void b() throws MyException1 {
c();
}
void c() throws MyException1 {
if (check2()) {
throw new MyException1();
}
doSomething();
}
继续强>
class MyException2 extends Exception {
}
void d() {
while(check()) {
try {
e();
doSomething();
} catch (MyException2 ex) {
// nothing to do here; We're already at the end of the loop body
}
}
}
void e() throws MyException2 {
f();
}
void f() throws MyException2 {
if (check2()) {
throw new MyException2();
}
doSomething();
}