| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 542 人关注过本帖
标题:改变数学等式的程序错误
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:1 
改变数学等式的程序错误
为什么我的程序执行的时候说我array out of bound? (我的输入是A + B, 我希望输出是A B +, 大家帮忙看看哪里出错了

import java.util.EmptyStackException;
import java.util.Stack;
import *;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ITP
{
   public ITP()
    {
    }
   
    public static String infixToPostfix(String str)
    {
       int i=0, j=0;
        String[] input=str.split(" ");
        String a="", temp="";
        
       ArrayStack<String> S = new ArrayStack<String>();
        ArrayStack<String> A = new ArrayStack<String>();
        
        //StringTokenizer strTokenizer=new StringTokenizer(str, " ");
        
        /*while(strTokenizer.hasMoreTokens())
        {
           input[i]=strTokenizer.nextToken();
            i++;
        }*/
        
        while(j<input.length)
        {
           if(input[j]=="(")
               j++;
           else if(input[j]!="+" && input[j]!="-" && input[j]!="*" && input[j]!="/" && input[j]!=")")
           {
              S.push(input[j]);
                j++;
                if(input[j]=="*"||input[j]=="/")
               {
                  A.push(input[j]);
                  j++;
                   temp=input[j];
                   a=S.pop();
                   a=a+temp;
                   a=a+A.pop();
               }
                else if(j==input.length)
                {
                   a=a+S.pop();
                   a=a+A.pop();
                }
            }
            /*else if(input[j]=="*"||input[j]=="/")
            {
               A.push(input[j]);
               j++;
                temp=input[j];
                a=S.pop()
                a=a+temp;
                a=a+A.pop();
            }*/
            else if(input[j]=="+"||input[j]=="-")
            {
               A.push(input[j]);
                j++;
            }
            else if(input[j]==")")
            {
               if(S.size()>=2)
                {
                  temp=S.pop();
                   a=S.pop();
                  a=a+temp;
                  a=a+A.pop();
                }
                else if(S.size()<2 && !S.isEmpty())
                {
                   a=a+S.pop();
                   a=a+A.pop();
                }
                else if(S.isEmpty())
                {
                   i++;
                }
                j++;
            }
        }
        return a;
   }
}

        
import java.util.EmptyStackException;
import java.util.Stack;
import *;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ITPtest
{
   public static void main(String[] args)
    {
      String string, line;
        
        string="A + B";
        //String[] input=string.split(" ");
        
        ITP a=new ITP();
        
        line=a.infixToPostfix(string);
        System.out.print(line);
    }
}



[ 本帖最后由 suckdog 于 2010-11-17 15:10 编辑 ]
搜索更多相关主题的帖子: 数学 等式 程序错误 
2010-11-17 10:46
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
有人帮忙查看吗
2010-11-21 03:54
快速回复:改变数学等式的程序错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017944 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved