Java 后缀字符到字符串的转换
Java Postfix char to String conversion
使用 BlueJ 编写代码并使用 jUnit 对其进行测试。
正在尝试将我实验室 class 的 infixToPostfix class 从使用 char 转换为使用字符串。这将使它不再局限于 char 的 "ab+c-d*-" 的单个输入,而是能够读取 "a b + c - d * -"
它正在使用一个对我来说相当新的堆栈,我不知道我将如何处理它。到目前为止我的代码是:
public class InfixToPostfix
{
private Stack operators = new Stack();
/**
* Constructor for objects of class InfixToPostfix
*/
public InfixToPostfix()
{
}
/**
* toPostfix
*/
public String toPostfix(String infix)
{
String [] tokens = new String[100];
int i;
int length = infix.length();
String operator;
String output = "";
for (i = 0; i < length; i++)
{
if (isOperator(tokens[i]))
if (operators.empty())
// 2. If the stack is empty, push the incoming operator onto the stack.
operators.push(tokens[i] + " ");
else
{
if (operatorLessPrecedence(tokens[i]))
// 3. If the incoming symbol has equal or lower precedence than the
// symbol on the top of the stack, pop the stack and print the top
// operator. Then test the incoming operator against the new top of stack.
// Push the incoming symbol onto the stack.
{
do
{
output = output + operators.pop();
}
while (!operators.empty() && operatorLessPrecedence(tokens[i]));
operators.push(tokens[i] + " ");
}
else
// 4. If the incoming symbol has higher precedence than the top of the stack,
// push it on the stack.
operators.push(tokens[i]);
}
else
// 1. Print operands as they arrive.
output = output + tokens[i] + " ";
}
while (!operators.empty())
{
// 5. At the end of the expression, pop and print all operators on the stack.
operator = (String)operators.pop();
output = output + operator + " ";
}
return output;
}
/**
* isOperator
*/
public boolean isOperator(String c)
{
if( c.equals("/") ||
c.equals("'") ||
c.equals("+") ||
c.equals("-"))
return true;
else
return false;
}
/**
* operatorLessPrecedence
* Compare operator with top of stack
* Assume association left to right
*/
public boolean operatorLessPrecedence(String o)
{
int operatorPrecedence = precedence(o);
int tosPrecedence = precedence((String)operators.peek());
return (operatorPrecedence <= tosPrecedence);
}
/**
* precedence
*/
public int precedence(String o)
{
switch (o)
{
case "+": return 1;
case "-": return 1;
case "*": return 2;
case "/": return 2;
}
return 5;
}
}
因此,当我使用 assertEquals 在 jUnit 中进行测试时;
@Test
public void testAddSub()
{
InfixToPostfix test = new InfixToPostfix();
assertEquals("1 2 +", test.toPostfix("1 + 2"));
assertEquals("2 1 -", test.toPostfix("2 - 1"));
}
我目前得到一个异常方法,在我将 isOperator 方法从用于测试 char 的“==”更改为我认为正确的 .equals() 方法来测试字符串之前,我只会得到空输出..
我不想要直接的代码或我到底做错了什么,只是 "forceful" 向正确的方向轻推或我可以调查的东西。谢谢
一个对象数组持有一个对象的引用。初始化数组时,您只是为 100 个空位分配了内存。你必须在上面放一些真正的字符串对象,否则 NullPointerException
将是你的输出。
所以,在你的行中
String [] tokens = new String[100];
您有一个包含 100 个字符串的数组 references,其值为 null.
比较 String
个对象的正确方法是使用 equals
方法。
使用 ==
测试对象 references,equals
测试 String
值。所以,不要改变你的方法实现,你走在正确的道路上。
我不推荐使用 Stack 对象。正如您所说,Stack 对您来说是一个新事物并且您正在努力改进,我建议您在有空的情况下看一下这个讨论并得出您自己的结论。
Why should I use Deque over Stack?.
使用 BlueJ 编写代码并使用 jUnit 对其进行测试。
正在尝试将我实验室 class 的 infixToPostfix class 从使用 char 转换为使用字符串。这将使它不再局限于 char 的 "ab+c-d*-" 的单个输入,而是能够读取 "a b + c - d * -"
它正在使用一个对我来说相当新的堆栈,我不知道我将如何处理它。到目前为止我的代码是:
public class InfixToPostfix
{
private Stack operators = new Stack();
/**
* Constructor for objects of class InfixToPostfix
*/
public InfixToPostfix()
{
}
/**
* toPostfix
*/
public String toPostfix(String infix)
{
String [] tokens = new String[100];
int i;
int length = infix.length();
String operator;
String output = "";
for (i = 0; i < length; i++)
{
if (isOperator(tokens[i]))
if (operators.empty())
// 2. If the stack is empty, push the incoming operator onto the stack.
operators.push(tokens[i] + " ");
else
{
if (operatorLessPrecedence(tokens[i]))
// 3. If the incoming symbol has equal or lower precedence than the
// symbol on the top of the stack, pop the stack and print the top
// operator. Then test the incoming operator against the new top of stack.
// Push the incoming symbol onto the stack.
{
do
{
output = output + operators.pop();
}
while (!operators.empty() && operatorLessPrecedence(tokens[i]));
operators.push(tokens[i] + " ");
}
else
// 4. If the incoming symbol has higher precedence than the top of the stack,
// push it on the stack.
operators.push(tokens[i]);
}
else
// 1. Print operands as they arrive.
output = output + tokens[i] + " ";
}
while (!operators.empty())
{
// 5. At the end of the expression, pop and print all operators on the stack.
operator = (String)operators.pop();
output = output + operator + " ";
}
return output;
}
/**
* isOperator
*/
public boolean isOperator(String c)
{
if( c.equals("/") ||
c.equals("'") ||
c.equals("+") ||
c.equals("-"))
return true;
else
return false;
}
/**
* operatorLessPrecedence
* Compare operator with top of stack
* Assume association left to right
*/
public boolean operatorLessPrecedence(String o)
{
int operatorPrecedence = precedence(o);
int tosPrecedence = precedence((String)operators.peek());
return (operatorPrecedence <= tosPrecedence);
}
/**
* precedence
*/
public int precedence(String o)
{
switch (o)
{
case "+": return 1;
case "-": return 1;
case "*": return 2;
case "/": return 2;
}
return 5;
}
}
因此,当我使用 assertEquals 在 jUnit 中进行测试时;
@Test
public void testAddSub()
{
InfixToPostfix test = new InfixToPostfix();
assertEquals("1 2 +", test.toPostfix("1 + 2"));
assertEquals("2 1 -", test.toPostfix("2 - 1"));
}
我目前得到一个异常方法,在我将 isOperator 方法从用于测试 char 的“==”更改为我认为正确的 .equals() 方法来测试字符串之前,我只会得到空输出..
我不想要直接的代码或我到底做错了什么,只是 "forceful" 向正确的方向轻推或我可以调查的东西。谢谢
一个对象数组持有一个对象的引用。初始化数组时,您只是为 100 个空位分配了内存。你必须在上面放一些真正的字符串对象,否则 NullPointerException
将是你的输出。
所以,在你的行中
String [] tokens = new String[100];
您有一个包含 100 个字符串的数组 references,其值为 null.
比较 String
个对象的正确方法是使用 equals
方法。
使用 ==
测试对象 references,equals
测试 String
值。所以,不要改变你的方法实现,你走在正确的道路上。
我不推荐使用 Stack 对象。正如您所说,Stack 对您来说是一个新事物并且您正在努力改进,我建议您在有空的情况下看一下这个讨论并得出您自己的结论。 Why should I use Deque over Stack?.