大家。我目前在使用Java进行后缀表达式的评估时遇到了问题,我认为一切都没问题,除了输出不是很正确。但是没关系,请让我发布所有代码,以便所有人都可以查看它们。顺便提一下,请注意,你们所有人只需要专注于TestPalindrome类就足够了,因为除了我刚刚指定的类之外,我从不更改其他类的代码。
StackInterface定义了ArrayStack类可用的所有方法。
//There is no need to check this.
public interface StackInterface<T> {
/** Task: Adds a new entry to the top of the stack.
* @param newEntry an object to be added to the stack */
public void push(T newEntry);
/** Task: Removes and returns the stack誷 top entry.
* @return either the object at the top of the stack or, if the
* stack is empty before the operation, null */
public T pop();
/** Task: Retrieves the stack誷 top entry.
* @return either the object at the top of the stack or null if
* the stack is empty */
public T peek();
/** Task: Detects whether the stack is empty.
* @return true if the stack is empty */
public boolean isEmpty();
/** Task: Removes all entries from the stack */
public void clear();
} // end StackInterface
ArrayStack类没什么特别的。
//There is no need to check this.
public class ArrayStack<T> implements StackInterface<T> {
private T[] stack; // array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;
public ArrayStack() {
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity) {
stack = (T[]) new Object[initialCapacity];
topIndex = -1;
} // end constructor
public void push(T newEntry) {
topIndex++;
if (topIndex >= stack.length) // if array is full,
doubleArray(); // expand array
stack[topIndex] = newEntry;
} // end push
public T peek() {
T top = null;
if (!isEmpty())
top = stack[topIndex];
return top;
} // end peek
public T pop() {
T top = null;
if (!isEmpty()) {
top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
} // end if
return top;
} // end pop
public boolean isEmpty() {
return topIndex < 0;
} // end isEmpty
public void clear() {
} // end clear
/** Task: Doubles the size of the array of stack entries.
* Refer to Segment 5.18 */
private void doubleArray() {
T[] oldStack = stack; // get reference to array of stack entries
int oldSize = oldStack.length; // get max size of original array
stack = (T[]) new Object[2 * oldSize]; // double size of array
// copy entries from old array to new, bigger array
System.arraycopy(oldStack, 0, stack, 0, oldSize);
} // end doubleArray
} // end ArrayStack
下面的类是TestPalindrome类。(类名可能听起来很奇怪,因为我所做的练习都在同一个类中,我不会在课堂上发布不相关的代码。)
import java.util.Scanner;
public class TestPalindrome {
public TestPalindrome() {
}
public static void main(String[] args) {
//P3Q2
StackInterface<Character> myStack = new ArrayStack<Character>();
Scanner scanner = new Scanner(System.in);
int result;
char resultInChar;
System.out.print("Please enter a postfix expresion : ");
String postfix = scanner.nextLine();
for(int i = 0; i < postfix.length(); i++)
{
char postfixChar = postfix.charAt(i);
if(Character.isDigit(postfixChar)) //If postfixChar is a digit, then it will be pushed into the stack.
{
myStack.push(postfixChar);
}
/*(For else statement) First operand will be popped as right operand and second
operand will be popped as left operand if postfixChar is operator such as + .
The calculation of both operands will be carried out based on the operator given.
After this the result of calculation will be pushed back into the stack and the
same things will happen again.*/
else
{
int firstOperand = Character.getNumericValue(myStack.pop()); //To get numeric value of the first character stored.
System.out.println("\nThe right operand : " + firstOperand);
int secondOperand = Character.getNumericValue(myStack.pop()); //To get numeric value of the second character stored.
System.out.println("The left operand : " + secondOperand);
switch(postfixChar)
{
case '+':
result = secondOperand + firstOperand;
System.out.println("The result is " + result);
resultInChar = (char)result; //Convert the result of calculation back to character data type so that it can be pushed into the stack.
System.out.println("Strange output : " + resultInChar); //Here is where the strange output occurred.
myStack.push(resultInChar);
break;
case '-':
result = secondOperand - firstOperand;
System.out.println("The result is " + result);
resultInChar = (char)result;
myStack.push(resultInChar);
break;
case '/':
result = secondOperand / firstOperand;
System.out.println("The result is " + result);
resultInChar = (char)result;
myStack.push(resultInChar);
break;
case '*':
result = secondOperand * firstOperand;
System.out.println("The result is " + result);
resultInChar = (char)result;
myStack.push(resultInChar);
break;
}
}
}
System.out.println("\nThe answer of " + postfix + " is " + Character.getNumericValue(myStack.pop())); //To get the final answer in the form of numeric value
}
}
以下是图片的附件,以显示该程序的所有输出。
请解释错误的部分因为我真的无法弄清楚为什么会发生这种情况,因为1 + 1 = 2并且应该显示2的50码的ASCII代码而不是奇怪的方形符号。谢谢因为花费宝贵的时间来看看我的问题。
答案 0 :(得分:2)
你说:
请解释错误的部分因为我真的无法弄清楚为什么会这样 将发生1 + 1 = 2和2的ASCII码50 显示而不是奇怪的方形符号。
是的,1 + 1 = 2.但是如果你将它转换为char,它将使用ASCII值2,而不是50.要做到这一点,你应该做类似的事情:
resultCharIn = (char) ('0' + result);
换句话说:'0' != 0
。
然而,这似乎是一种错误的方法,因为:如果结果大于9会怎么样。你需要两个字符。也许你应该考虑不同的设计?