java 使用堆栈(LinkedList)将中缀转换为前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26715686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting infix to prefix using a Stack (LinkedList)
提问by Noxob
I am trying to write a method that converts infix to prefix and to do that i want to read a sting reverse and use a stack. When i execute this code i am getting an exception atcharacter = expression.charAt(limit);
how can i fix that code?
我正在尝试编写一种将中缀转换为前缀的方法,为此我想读取一个反向并使用堆栈。当我执行此代码时,我收到一个异常,character = expression.charAt(limit);
我该如何修复该代码?
My input was1+3and the error i got was:
我的输入是1+3 ,我得到的错误是:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(Unknown Source) at PrefixTranslator.translate(PrefixTranslator.java:27) at PrefixTranslatorTest.main(PrefixTranslatorTest.java:11)
线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:3 at java.lang.String.charAt(Unknown Source) at PrefixTranslator.translate(PrefixTranslator.java:27) at PrefixTranslatorTest.main(PrefixTranslatorTest.java) :11)
PrefixTranslator Class:
PrefixTranslator 类:
public class PrefixTranslator
{
static private String expression;
private MyStack<Character> stack = new MyStack<Character>();
//Constructor
public PrefixTranslator(String infixExpression)
{
expression = infixExpression;
}//End of constructor
public String translate()
{
//Declare Method Variables
String input = "";
String output = "";
char character = ' ';
char nextCharacter = ' ';
for(int limit = expression.length(); limit > 0 ; limit--)
{
character = expression.charAt(limit);
if(isOperator(character))
{
output = output + character + " ";
}
else if(character == '(')
{
stack.push(character);
}
else if(character == ')')
{
while(!stack.top().equals('('))
output = output + stack.pop() + " ";
stack.pop();
}
else
{
if(Character.isDigit(character) && (limit + 1) < limit && Character.isDigit(expression.charAt(limit+1)))
{
stack.push(character);
stack.push(expression.charAt(limit+1));
}
else if(Character.isDigit(character))
{
stack.push(character);
}
else
{
output = output + character;
}
}
}//End of for
while(!stack.isEmpty())
{
output = output + stack.pop() + " ";
}
return output;
}//End of translate method
//Check priority on characters
public static int precedence(char operator)
{
if(operator == '+' || operator =='-')
return 1;
else if(operator == '*' || operator == '/')
return 2;
else
return 0;
}//End of priority method
public boolean isOperator(char element)
{
if(element == '*' || element == '-' || element == '/' || element == '+')
return true;
else
return false;
}//End of isOperator method
}//End of class
PrefixTranslatorTest Class:
PrefixTranslatorTest 类:
import java.util.Scanner;
public class PrefixTranslatorTest{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the expression that you want to convert to prefix.");
String expression = input.next();
PrefixTranslator translator = new PrefixTranslator(expression);
System.out.println(translator.translate());
}
}
回答by cassiomolin
The first index of Java arrays is 0
and the method length()
returns the number of elements of array. If you need the last element's index, use
length() - 1
.
Java 数组的第一个索引是0
,该方法length()
返回数组的元素数。如果您需要最后一个元素的索引,请使用
length() - 1
.
So instead of
所以代替
for (int limit = expression.length(); limit > 0; limit--)
use
利用
for (int limit = expression.length() - 1; limit >= 0; limit--)
回答by ?smet Alkan
For the given input 1+3
expression.length()
returns 3
and you can refer to indexes 0-2
of that string. So, your loop shouldn't be:
对于给定的输入1+3
expression.length()
返回3
,您可以参考0-2
该字符串的索引。所以,你的循环不应该是:
for(int limit = expression.length(); limit > 0 ; limit--)
And it should be
它应该是
for(int limit = expression.length() - 1; limit >= 0 ; limit--)