当前位置: 技术问答>java相关
各位大虾!!请帮忙啊!救小弟一把!我编的一个中缀转后缀的程序,便已通过可是运行却出错,请指教!!
来源: 互联网 发布时间:2015-05-21
本文导语: 运行错误是: please input the inorder expression 1+2 the postorder expression is Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:507) ...
运行错误是:
please input the inorder expression
1+2
the postorder expression is
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: -1
at java.lang.String.charAt(String.java:507)
at stack1.main(stack1.java:18)
源码如下:
import java.io.*;
public class stack1{
public static void main(String args[]){
stackarray operator=new stackarray();
String inorder=new String();
int inposition=0;
int operator1=0;
System.out.println("please input the inorder expression");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{
inorder=input.readLine();}
catch(IOException e){
System.out.println(e);}
System.out.println("the postorder expression is");
for(inposition=-1;;inposition++){
while (true){
if(operator.isoperator(inorder.charAt(inposition))){
if((operator.top==-1)||(char)inorder.charAt(inposition)=='(')
operator.push(inorder.charAt(inposition));
else
{ if((char)inorder.charAt(inposition)==')')
{if(operator.astack[operator.top]!=40)
{operator1=operator.pop();
System.out.print((char)operator1);
}
}
else
{if(operator.priority(inorder.charAt(inposition))
=inorder.length())
break;
}
while(operator.top!=-1)
{
operator1=operator.pop();
System.out.print((char)operator1);
}
}
}
}
class stackarray{
int maxsize=20;
int[] astack=new int[maxsize];
int top=-1;
public void push(int value){
int i;
if(top>=maxsize)
System.out.println("the stack is full");
else{
top++;
astack[top]=value;
}
}
public int pop(){
int temp;
int i;
if(top
please input the inorder expression
1+2
the postorder expression is
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: -1
at java.lang.String.charAt(String.java:507)
at stack1.main(stack1.java:18)
源码如下:
import java.io.*;
public class stack1{
public static void main(String args[]){
stackarray operator=new stackarray();
String inorder=new String();
int inposition=0;
int operator1=0;
System.out.println("please input the inorder expression");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{
inorder=input.readLine();}
catch(IOException e){
System.out.println(e);}
System.out.println("the postorder expression is");
for(inposition=-1;;inposition++){
while (true){
if(operator.isoperator(inorder.charAt(inposition))){
if((operator.top==-1)||(char)inorder.charAt(inposition)=='(')
operator.push(inorder.charAt(inposition));
else
{ if((char)inorder.charAt(inposition)==')')
{if(operator.astack[operator.top]!=40)
{operator1=operator.pop();
System.out.print((char)operator1);
}
}
else
{if(operator.priority(inorder.charAt(inposition))
=inorder.length())
break;
}
while(operator.top!=-1)
{
operator1=operator.pop();
System.out.print((char)operator1);
}
}
}
}
class stackarray{
int maxsize=20;
int[] astack=new int[maxsize];
int top=-1;
public void push(int value){
int i;
if(top>=maxsize)
System.out.println("the stack is full");
else{
top++;
astack[top]=value;
}
}
public int pop(){
int temp;
int i;
if(top