我正在研究一个应该采用数学表达式的程序,并使用堆栈和堆栈操作将所述表达式从中缀转换为后缀,即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);
}
*/
}
就输出而言,该程序似乎只是吐出我完全按原样提供的东西。
任何想法我做错了什么?