我正在开发一个应该接受数学表达式并使用堆栈和堆栈操作将所述表达式从中缀转换为后缀的程序,即 5 + 9 * 3 将变为 5 9 3 * + 。我相信这部分的大部分代码都是完整的;但是,由于某种原因,每次我尝试提供给我们的输入时,程序都会模仿我输入的内容。我目前完成的程序如下:
import java.util.Scanner;
public class StackTester {
public static void main(String[] args) {
boolean quit = false;
int input;
final String ADD="+", SUB="-", MUL="*", DIV="/";
do {
System.out.println("1. Convert infix to postfix");
System.out.println("2. Convert postfix to infix");
System.out.println("3. Exit.");
Scanner keyboard = new Scanner(System.in);
input = keyboard.nextInt();
switch(input) {
case 1:
ArrayStack<String> stack = new ArrayStack<String>();
System.out.println("Enter an infix expression: ");
Scanner Input = new Scanner(System.in);
String expression = Input.nextLine();
String[] storedExp = expression.split(" ");
for(int count=storedExp.length-1;count>=0;count--) {
stack.push(storedExp[count]);
}
//System.out.println(stack.toString());
ArrayStack<String> operands = new ArrayStack<String>();
String temp = "";
while(!stack.isEmpty()) {
if(stack.peek()==ADD||stack.peek()==SUB||stack.peek()==MUL||stack.peek()==DIV&&operands.isEmpty()) {
String store = stack.pop();
System.out.println(store);
operands.push(store);
}
if(stack.peek()==ADD||stack.peek()==SUB||stack.peek()==MUL||stack.peek()==DIV&&!operands.isEmpty()) {
System.out.println(stack.peek());
if(operands.peek()==MUL||operands.peek()==DIV&&stack.peek()==MUL||stack.peek()==DIV) {
String store = stack.pop();
operands.push(store);
}
if(operands.peek()==MUL||operands.peek()==DIV&&stack.peek()==ADD||stack.peek()==SUB) {
temp += operands.toString()+" ";
String store = stack.pop();
operands.push(store);
}
if(operands.peek()==ADD||operands.peek()==SUB&&stack.peek()==MUL||stack.peek()==DIV) {
String store = stack.pop();
operands.push(store);
}
if(operands.peek()==ADD||operands.peek()==SUB&&stack.peek()==ADD||stack.peek()==SUB) {
String store = stack.pop();
operands.push(store);
}
}
/*if(stack.peek()=="("||stack.pop()==")") {
stack.pop();
}
*/
else {
temp += stack.pop()+" ";
}
}
System.out.println(temp);
if(!operands.isEmpty()) {
temp += operands.toString()+" ";
}
System.out.println(temp);
break;
}
}
while(!quit);
}
}
对于 ArrayStack 类:
public class ArrayStack<T> implements StackADT<T>
{
private static final int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
private String test;
public ArrayStack() {
top = 0;
stack = (T[]) (new Object[DEFAULT_CAPACITY]);
}
public void push(T element) {
for(int count=0;count<stack.length;count++) {
if(stack[count]==null) {
stack[count] = element;
top = count;
break;
}
}
}
public T pop() {
T element = stack[top];
stack[top] = null;
if(top!=0) {
top--;
}
return element;
}
public T peek() {
return stack[top];
}
public boolean isEmpty() {
if(stack[0]==null)
return true;
else{
return false;
}
}
public int size() {
int length = 0;
for(int count=0;count<stack.length;count++) {
if(stack[count]!=null) {
length++;
}
else if(stack[count]==null) {
break;
}
}
return length;
}
public String toString() {
String array = "";
for(int count=0;count<stack.length;count++) {
if(stack[count]!=null)
array = array+stack[count]+" ";
}
return array;
}
/* public static void main(String[] args) {
boolean quit = false;
int input;
String expression;
do {
System.out.println("1. Convert infix to postfix");
System.out.println("2. Convert postfix to infix");
System.out.println("3. Exit.");
java.util.Scanner keyboard = new java.util.Scanner(System.in);
input = keyboard.nextInt();
switch(input) {
case 1:
System.out.println("Enter an infix expression");
expression = keyboard.next();
for(int count=0;count<expression.length();count++) {
Character e = expression.charAt(count);
push();
}
break;
}
}
while(!quit);
}
*/
}
就输出而言,该程序似乎只是按原样吐出我输入的任何内容。
任何想法我做错了什么?