import java.io.*;
import java.util.*;
public class hou
{
public static void main(String[] args)
{
final int MAX=100;
InputStream input=System.in;
InputStreamReader reader=new InputStreamReader(input);
BufferedReader buf=new BufferedReader(reader); char str[]=new char[MAX];
char exp[]=new char[MAX];
char stack[]=new char[MAX];
String temp=null,line;
char ch;
int flag=1;
int i=0,j=0,t=0,top=0;
while(true)
{     System.out.println();
    System.out.println("============将中缀表达式转换为后缀表达式=============");
    System.out.print("中缀表达式:");
    try{
    temp=buf.readLine();
    }catch(Exception e){
     e.printStackTrace();
    }     if(temp.equals("exit"))
    {
       System.out.println("退出!");
        break;
    }
    break;
     }
line=temp.replaceAll(" ","");                     //去除空格        if(input!=null){                                 //没有输入
        try{
           input.close();
            }catch(Exception e){
              e.printStackTrace();
             }        str=line.toCharArray();
                for(int m=line.length()-1;m>=0;m--)
        {
         ch=str[i];
         if((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))//字符为数字或者字母
         {
                   exp[t]=ch;
                   t++;
                }            else if(ch=='(')                        //若为左括号则压栈
             {
                   top++;
                   stack[top]=ch;
                }
            else if(ch==')')                     //为右括号时
             {
                  while(stack[top]!='(')
                  {
                  exp[t]=stack[top];
                  top--;
                  t++;
                  }
                  top--;
                }
            else if(ch=='+'||ch=='-')
             {
                  while(top!=0&&stack[top]!='(')
                 {
                     exp[t]=stack[top];
                     top--;
                     t++;
                     }
                  top++;
                  stack[top]=ch;
               }
           else if(ch=='*'||ch=='/')
            {
           while(stack[top]=='*'||stack[top]=='/')
            {
            exp[t]=stack[top];
            top--;
            t++;
           }
           top++;
           stack[top]=ch;
           }
           else {
           System.out.println("第 "+(i+1)+" 个字母开始出错!");
           flag=0;
           break;
              }
            i++;            ch=str[i];
            }
         if(flag!=0){
         while(top!=0){
         exp[t]=stack[top];
         t++;
         top--;
         }
         System.out.print("逆波兰式输出:");
         for(j=0;j<t;j++)
         System.out.print(exp[j]);
         System.out.println();     }
   }
        }     }程序编译可以通过,但是运行时,如输入2+3之类就提示错误,越界了
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at hou.main(hou.java:111)

解决方案 »

  1.   


    import java.io.*;
    import java.util.*;
    public class hou
    {
    public static void main(String[] args)
    {
    final int MAX=100;
    InputStream input=System.in;
    InputStreamReader reader=new InputStreamReader(input);
    BufferedReader buf=new BufferedReader(reader); char str[]=new char[MAX];
    char exp[]=new char[MAX];
    char stack[]=new char[MAX];
    String temp=null,line;
    char ch;
    int flag=1;
    int i=0,j=0,t=0,top=0;
    while(true)
    {
    System.out.println();
    System.out.println("============将中缀表达式转换为后缀表达式=============");
    System.out.print("中缀表达式:");
    try{
    temp=buf.readLine();
    }catch(Exception e){
    e.printStackTrace();
    } if(temp.equals("exit"))
    {
    System.out.println("退出!");
    break;
    }
    break;
    }
    line=temp.replaceAll(" ",""); //去除空格   if(input!=null){ //没有输入
      try{
    input.close();
      }catch(Exception e){
    e.printStackTrace();
      }   str=line.toCharArray();
      for(int m=line.length()-1;m>=0;m--)
      {
      ch=str[i];
      if((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))//字符为数字或者字母
    {
      exp[t]=ch;
      t++;
    }else if(ch=='(') //若为左括号则压栈
    {
      top++;
      stack[top]=ch;
    } else if(ch==')') //为右括号时
    {
      while(stack[top]!='(')
      {
      exp[t]=stack[top];
      top--;
      t++;
      }
      top--;
    } else if(ch=='+'||ch=='-') 
    {
      while(top!=0&&stack[top]!='(')
      {
      exp[t]=stack[top];
      top--;
      t++;
      }
      top++;
      stack[top]=ch;
    }
    else if ((ch=='*') || (ch=='/') )
    {
      while ( (stack[top] == '*') || (stack[top] == '/'))
      {
      exp[t]=stack[top];
      top--;
      t++;
      }
      top++;
      stack[top]=ch;
    }else {
      System.out.println("第 "+(i+1)+" 个字母开始出错!");
      flag=0;
      break;
    }
    i++; ch=str[i];
    }
      if(flag!=0){
      while ((top!=0 ) && (t < exp.length) ){
    exp[t]=stack[top];
    t++;
    top--;
      }
      System.out.print("逆波兰式输出:");
      for(j=0;j<t;j++)
    System.out.print(exp[j]);
      System.out.println();   }
    }
      }  }
      

  2.   

              if(flag!=0){
         
             while ((top!=0 ) && (t < exp.length) ){
                    exp[t]=stack[top];
                    t++;
                    top--;
                  }
                  System.out.print("逆波兰式输出:");
                  for(j=0;j<t;j++)
                    System.out.print(exp[j]);
                  System.out.println();          }
            }
          }  }