`
sinokaka
  • 浏览: 319673 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

华为的java题

    博客分类:
  • java
阅读更多
      前两天在JR上面发现了这么一片文章《华为JAVA比武大赛试题》,稍微有些兴趣,就做了做。
题目如下:红色的是我修改的地方,因为按照上面写的,根本出不来想要的结果。自己做了一下,没有在要求的时间内做出来:)比较笨了,随便写了写,只能符合这个题目要求,但是写的比较草,也比较傻,这个应该做一个语法分析器,赫赫,有空看看了,附录是我写的答案了:)
要求:
/////////////////////input.txt样例////////////////////////////////
1+3*7*2=
4+2/*asjdff*/*5-8=
5%2+5-2/*4=6/*i**ifdg***/*84o*/*1-2=
………………………………………        //更多表达式省略
/////////////////////////////////////////////////////////////////

/////////////////////output.txt样例////////////////////////////////
1+3*7*2=43
4+2/*asjdff*/*5-8=
5%2+5-2/*4=6/*i**ifdg***/*84o*/*1-2=??
………………………………………        //更多表达式省略
/////////////////////////////////////////////////////////////
1.要求用JAVA实现。
2.若在命令行参数中指明了 input.txt 和 output.txt,请计算出input.txt中每一行表达式的值,并写入到output.txt文件中
3.2.若在命令行参数中只指明了 input.txt,则新建一个output.txt,计算出input.txt中每一行表达式的值,并写入到output.txt文件中
3.若在命令行参数中没有指明 input.txt 和 output.txt,则输入一个类似4+2/*asjdff*/*5-8这样的表达式,计算1000次这个表达式的值,在屏幕中输出结计算结果和所用时间。
4.要求最多在180分钟内完成。
 
public class HuaWei {

    public static void main(String[] arg) throws Exception {
        String[] strs = new String[]{"1+3*7*2=","4+2/*asjdff*/*5-8=","5%2+5-2/*4=6/*i**ifdg***/*84o*/*1-2="};
        HuaWei h = new HuaWei();
        int result = 0;
        for (int i = 0; i < strs.length; i++) {
            try {
                List a = h.division(strs[i]);
                result = h.analyse(0, "+", a);
            } catch (Exception e) {
                System.out.println("??");
                return;
            }
            System.out.println(result);
        }
    }
    public static String doComput(String str) {
        HuaWei h = new HuaWei();
        try {
            List a = h.division(str);
            int result = h.analyse(0, "+", a);
            return str + result;
        } catch (Exception e) {
            return str + "??";
        }
       
    }
   
    /**
     * 分析并计算结果
     * @param v
     * @param oper
     * @param strs
     * @return
     * @throws Exception
     */
    private int analyse(int v, String oper, List strs) throws Exception {
        String value = (String) strs.get(0);
        if (isOperator(value)) {
            throw new Exception("???");
        }
        if (strs.size() == 1) {
            return computerReslut(v, oper, Integer.parseInt(value));
        }
        if (strs.size() < 3) {
            throw new Exception("???");
        }
        String nextOper = (String) strs.get(1);
        if (!isOperator(nextOper)) {
            throw new Exception("???");
        }
        if (isAdvanceOperator(oper)) {
            v = computerReslut(v, oper, Integer.parseInt(value));
            int temp = analyse(v, nextOper, strs.subList(2, strs.size()));
            return temp;
        } else {
            int temp = computerReslut(v, oper, analyse(Integer.parseInt(value),
                    nextOper, strs.subList(2, strs.size())));
            return temp;
        }
    }
   
    /**
     * 是否是高级操作符
     * @param str
     * @return
     */
    private boolean isAdvanceOperator(String str) {
        if (str.equals("*") || str.equals("/")) {
            return true;
        }
        return false;
    }
   
    /**
     * 是否是操作符
     * @param str
     * @return
     */
    private boolean isOperator(String str) {
        if (str.equals("+") || str.equals("-") || str.equals("*")
                || str.equals("/")) {
            return true;
        }
        return false;
    }
   
    /**
     * 分割字符串,分割成一个个Token
     * @param str   待分割的字符串
     * @return
     * @throws Exception
     */
    private List division(String str) throws Exception {
        char[] allChars = str.toCharArray();
        List allTokens = new ArrayList();
        StringBuffer token = new StringBuffer();
        for (int i = 0; i < allChars.length; i++) {
            // 取出其中的数字
            if (isNumber(allChars[i])) {
                for (; i < allChars.length; i++) {
                    if (isNumber(allChars[i])) {
                        token.append(allChars[i]);
                    } else {
                        break;
                    }
                }
                allTokens.add(token.toString());
                token.delete(0, token.length());
            }
           
            // 取出其中的操作符
            if (isOperator(allChars[i])) {
                allTokens.add(String.valueOf(allChars[i]));
                continue;
            }
           
            // 如果是=号,表示结束
            if (allChars[i] == '=') {
                break;
            }
           
            // 特殊对待
            if (allChars[i] == '/') {
                // 如果不是注释的话,那么就加入操作符
                if (allChars[i + 1] != '*') {
                    allTokens.add(String.valueOf(allChars[i]));
                } else {
                    boolean isCommentOkEnd = false; // 注释是否正常结束
                    for (i = i + 2; i < allChars.length; i++) {
                        if (allChars[i] == '/' && allChars[i-1] == '*') {
                            isCommentOkEnd = true;
                            break;
                        }
                    }
                    // 非正常结束,抛出异常
                    if (!isCommentOkEnd) {
                        throw new Exception("??");
                    }
                }
            } else {
               
                // 特殊字符抛出异常
                throw new Exception("??");
            }
        }
        return allTokens;
    }
   
    /**
     * 是否是数字
     * @param c
     * @return
     */
    private boolean isNumber(char c) {
        if (c <= '9' && c >= '0') {
            return true;
        }
        return false;
    }
   
    /**
     * 是否是操作符。[/]单独对待
     * @param c
     * @return
     */
    private boolean isOperator(char c) {
        if (c == '+' || c == '*' || c == '-') {
            return true;
        }
        return false;
    }
   
    /**
     * 计算结果
     * @param a     value1
     * @param oper  操作符
     * @param b     value2
     * @return      计算的结果
     */
    private static int computerReslut(int a, String oper, int b) {
        if (oper.equals("+")) {
            return a + b;
        }
        if (oper.equals("*")) {
            return a * b;
        }
        if (oper.equals("/")) {
            return a / b;
        }
        if (oper.equals("-")) {
            return a - b;
        }
        return 0;
    }
}
 
测试用例
public class HuaWeiTest extends TestCase {
    public void testDoComput1() {
        assertEquals("1+3*7*2=43", HuaWei.doComput("1+3*7*2="));
    }
    public void testDoComput2() {
        assertEquals("4+2/*asjdff*/*5-8=6", HuaWei
                .doComput("4+2/*asjdff*/*5-8="));
    }
    public void testDoComput3() {
        assertEquals("5%2+5-2/*4=6/*i**ifdg***/*84o*/*1-2=??", HuaWei
                .doComput("5%2+5-2/*4=6/*i**ifdg***/*84o*/*1-2="));
    }
}
分享到:
评论
2 楼 richman 2007-08-15  
主要是减法那里出了问题!

1 楼 richman 2007-08-15  
我也是刚把这道题目做出来了,想看看其他人怎么弄的,进来看了下,首先你的代码我运行了一下,发现有问题!
表达式等于“2+2*3-1+2”你的结果是5,我反复测试了,确信不是我的错误。还有这个递归,真的让人受不了!那个地方让人看起来真的很不爽!~

相关推荐

Global site tag (gtag.js) - Google Analytics