华为的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="));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值