您的位置:首页 > 其它

输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2

2017-07-12 23:28 926 查看
public class PracticeUtil {  

  

    public static void main(String[] args) {  

        String s = "3+8×2/9-2 ";  

        int result = getMyRet(s);  

        System.out.println("最后结果:" + result);  

    }  
4000

  

    public static int getMyRet(String s1) {  

        int len = s1.length();  

        List<String> list = new ArrayList<String>();  

        for (int i = 0; i < len; i++)  

            list.add(s1.charAt(i) + "");  

        System.out.println("list--->" + list);  

        for (int j = 0; j < list.size(); j++) {  

            if (list.get(j).equals("×")) {  

                int ji = Integer.parseInt(list.get(j - 1))  

                        * Integer.parseInt(list.get(j + 1));  

                list.add(j - 1, ji + "");// 把ji插入到原来x的前一位,原来的后移。从8开始往后移  

                list.remove(j);// 删除8;remove是删除当前位置后后面的前移;故x到了j这个下标位置。  

                list.remove(j);// 删除x  

                list.remove(j);// 删除9  

                System.out.println("list--x后->" + list);// list--x后->[3, +, 16,  

                                                        // /, 9, -, 2, ]  

                j--;// 相当于这次循环木有跳动下一个下标,因为马上要对ji参与运算,而不是跳过  

            } else if (list.get(j).equals("/")) {  

                int shang = Integer.parseInt(list.get(j - 1))  

                        / Integer.parseInt(list.get(j + 1));  

                list.add(j - 1, shang + "");  

                list.remove(j);  

                list.remove(j);  

                list.remove(j);  

                System.out.println("list--/后->" + list);// list--/后->[3, +, 1,  

                                                        // -, 2, ]  

                j--;  

            }  

        }  

        for (int k = 0; k < list.size(); k++) {// 这个时候是新的size  

            if (list.get(k).equals("+")) {  

                int he = Integer.parseInt(list.get(k - 1))  

                        + Integer.parseInt(list.get(k + 1));  

                list.add(k - 1, he + "");  

                list.remove(k);  

                list.remove(k);  

                list.remove(k);  

                System.out.println("list--+后->" + list); // list--+后->[4, -, 2,  

                                                            // ]  

                k--;  

            }  

            if (list.get(k).equals("-")) {  

                int cha = Integer.parseInt(list.get(k - 1))  

                        - Integer.parseInt(list.get(k + 1));  

                list.add(k - 1, cha + "");  

                list.remove(k);  

                list.remove(k);  

                list.remove(k);  

                System.out.println("list--  -后->" + list); // list-- -后->[2, ]  

                                                            // k--;  

            }  

        }  

        int sum = Integer.parseInt(list.get(0));  

        return sum;  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐