import java.io.*;class StackX
{
private int maxSize;
private char[] stackArray;
private int top;

public StackX(int s)
{
maxSize=s;
stackArray=new char[maxSize];
top=-1;
}

public void push(char j)
{
stackArray[++top]=j;
}

public char pop()
{
return stackArray[top--];
}

public char peek()
{
return stackArray[top];
}

public boolean isEmpty()
{
return (top==-1);
}

public int size()
{
return top+1;
}

    public char peekN(int n)
    {
     return stackArray[n];
    }
    
public void displayStack(String s)
{
System.out.print(s);
System.out.print("Stack (botton-->top):");
for(int j=0;j<size();j++)
{
System.out.print(peekN(j));
System.out.print(" ");
}
System.out.println(" ");
}
}class InToPost
{
private StackX theStack;
private String input;
private String output=" ";

public InToPost(String in)
{
input=in;
int stackSize=input.length();
theStack=new StackX(stackSize);
}

public String doTrans()
{
for(int j=0;j<input.length();j++)
{
char ch=input.charAt(j);
theStack.displayStack("For"+ch+" ");
switch(ch)
{
case '+':
case '-':
   gotOper(ch,1);
   break;
case '*':
case '/':   
    gotOper(ch,2);
    break;
case '(':
    theStack.push(ch);        
    break;
case ')':
    gotParen(ch);   
    break;
default: 
     output=output+ch;
     break;
      
}
}

while (!theStack.isEmpty())
{
theStack.displayStack();
output=output+theStack.pop();
}
theStack.displayStack("End ");
return output;
}

public void gotOper(char opThis,int prec1)
{
while(!theStack.isEmpty())
{
char opTop=theStack.pop();
if(opTop=='(')
{
theStack.push(opTop);
break;
}
else
{
int prec2;

if(opTop=='+'||opTop=='-')
{
prec2=1;
}
else
{
prec2=2;
}
if(prec2<prec1)
{
theStack.push(opTop);
break;
}
else
{
output=output+opTop;
}
}
}
theStack.push(opThis);
}
public void gotParen(char ch)
{
while(!theStack.isEmpty())
{
char chx=theStack.pop();
if(chx=='(')
{
break;
}
else
{
output=output+chx;
}
}
}
}class InfixApp
{
public static void main(String args[])
{
String input,output;
while(true)
{
System.out.print("Enter infix: ");
System.out.flush();
input=getString();
if(input.equals(""))
{
break;
}
InToPost theTrans=new InToPost(input);
output=theTrans.doTrans();
System.out.println("Postfix is "+output+"\n");
}
}

public static String getString() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
}这是一个将中缀表达式转换成后缀表达式的过程,D:\JCreator Pro\MyProjects\InfixApp.java:107: displayStack(java.lang.String) in StackX cannot be applied to ()    theStack.displayStack(); 这里错了,但不知道怎么改,望各位指点下!

解决方案 »

  1.   

    displayStack()与displayStack(String s)是两个方法
    displayStack()这个方法没有定义
      

  2.   

    我刚刚接触到java
      学习中。
      

  3.   

    你定义的displayStack(String s)是要传参数的呀
    可是你的theStack.displayStack()没有带参数,当然编译通不过了
      

  4.   

    你就改成theStack.displayStack("")就行了
      

  5.   

    将theStack.displayStack()这里改成theStack.displayStack("").
    你的InfixApp类里的public static String getString() throws IOException{}方法是要抛异常的
    所以只要用到这个方法的地方都要放在try{},catch{}块里.
    还有InfixApp类里的String input,output这条语句没有给对象初始化,
    因此if(input.equals(""))这里的判断会出错,一个未初始化的对象是不可以进行比较的
    要写成String input = new String();String output = new String();
    这几个地方改了后就不会报错了.
      

  6.   

    你们的JSDK 1.4在那里下的啊
    我怎么下不了
    谁帮帮我啊!!!!