Itisblank 2018-01-30 20:30 采纳率: 0%
浏览 976
已结题

整数四则运算, 无括号, 求验证所写程序是否正确

package test;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // 非负整数 小于10000
        // 2+3*4-2+5+3*2-100
        String line;
        String right;
        String left;
        String middle;
        String sign1;
        String sign2;

        line = in.nextLine();
        int lineLength = line.length();

        if (lineLength > 0) {
            do {

                // 获取最右边的加减号
                int addIndex = line.lastIndexOf("+");
                int subIndex = line.lastIndexOf("-");
                int mulIndex = line.lastIndexOf("*");
                int divIndex = line.lastIndexOf("/");

                // 加减乘除 不存在,
                // 或者只有最前面的负号, 则终止循环, 输出结果
                if (addIndex == -1 && mulIndex == -1 && divIndex == -1) { // end start
                    if (subIndex == -1) {
                        break;
                    } else if (subIndex != -1) {
                        int subSecondIndex = line.lastIndexOf("-", subIndex - 1);
                        if (subIndex == 0 && subSecondIndex == -1) {
                            break;
                        }
                    }
                } // end end

                if (addIndex == -1 && subIndex == -1) { // 加减 无
                    if (divIndex == -1 && mulIndex == -1) { // 加减 乘除 无
                        System.out.println(line);
                    } else { // 乘除 只有
                        line = getMulDivResult(line);
                    }
                } else { // 加减 有
                    // 以最右边的加减号为断点, 开始截取字符串
                    int asIndex = getMaxIndex(addIndex, subIndex);

                    if (divIndex == -1 && mulIndex == -1) { // 加减 只有
                        line = getAddSubResult(line);
                    } else { // 加减乘除 都有
                        // 从最后一个加减号截取来的字符串
                        String str2 = line.substring(asIndex + 1);

                        middle = String.valueOf(line.charAt(asIndex));
                        left = line.substring(0, asIndex);
                        int mdIndex = getMaxIndex(mulIndex, divIndex);

                        right = str2;
                        if (mdIndex > asIndex) { // 在最后一个加减号的后面还存在乘除
                            right = getMulDivResult(str2);
                            line = left + middle + right;
                        } else if (mdIndex < asIndex) { // 在最后一个加减号的后面, 只是单个数字
                            // 查找最后一个加减号之前, 即紧挨着的加减号, 截取其中的乘除字符串

                            int add2Index = line.lastIndexOf("+", asIndex - 1);
                            int sub2Index = line.lastIndexOf("-", asIndex - 1);
                            // 在最后一个加减号, 左边全是乘除, 右边随意
                            if (add2Index == -1 && sub2Index == -1) {
                                left = line.substring(0, asIndex);
                                left = getMulDivResult(left);
                                right = line.substring(asIndex + 1);
                                middle = String.valueOf(line.charAt(asIndex));
                                line = left + middle + right;
                            } else { // 有加减 有乘除
                                int leftIndex = getMaxIndex(line.lastIndexOf("+", mdIndex - 1),
                                        line.lastIndexOf("-", mdIndex - 1));
                                int rightIndex = getMinIndex(line.indexOf("+", mdIndex + 1),
                                        line.indexOf("-", mdIndex + 1));
                                if (leftIndex != -1) {
                                    sign1 = String.valueOf(line.charAt(leftIndex));
                                    left = line.substring(0, leftIndex);
                                } else {
                                    sign1 = "";
                                    left = "";
                                }
                                if (rightIndex != -1) {
                                    sign2 = String.valueOf(line.charAt(rightIndex));
                                } else {
                                    sign2 = "";
                                }

                                middle = line.substring(leftIndex + 1, rightIndex);
                                middle = getMulDivResult(middle);
                                right = line.substring(rightIndex + 1);

                                line = left + sign1 + middle + sign2 + right;

                            }

                        }

                    }
                }
            } while (line.indexOf("+") != -1 || line.indexOf("-") != -1 || line.indexOf("*") != -1
                    || line.indexOf("/") != -1);
            System.out.println(line);
        } // end if

        in.close();

        // 提示:一种可能的做法是,将整个式子作为字符串读入,
        // 然后找出其中最后一个+或-,在此位置将其截成两段,
        // 分别计算后再做+或-,以此类推。
        // 另,用Integer.parseInt(s)可以从一个字符串得到整数。
    }

    /**
     * 单纯乘除计算
     * 
     * @param line
     * @return
     */
    public static String getMulDivResult(String line) {
        String retStr = null;

        String leftStr = null;
        String endStr = null;

        do {
            if (endStr != null) {
                line = leftStr + endStr;
            }
            int index = getMinIndex(line.indexOf("*"), line.indexOf("/"));
            String sign = String.valueOf(line.charAt(index));
            leftStr = line.substring(0, index);
            int index2 = getMinIndex(line.indexOf("*", index + 1), line.indexOf("/", index + 1));
            String rightStr = null;
            if (index2 != -1) {
                rightStr = line.substring(index + 1, index2);
                endStr = line.substring(index2);
            } else if (index2 == -1) {
                rightStr = line.substring(index + 1);
                endStr = line.substring(index + rightStr.length() + 1);
            }

            switch (sign) {
            case "*":
                leftStr = String.valueOf(Integer.parseInt(leftStr) * Integer.parseInt(rightStr));
                break;
            case "/":
                leftStr = String.valueOf(Integer.parseInt(leftStr) / Integer.parseInt(rightStr));
                break;
            }
            retStr = leftStr + endStr;

        } while (endStr.length() > 1);

        return retStr;
    }

    /**
     * 单纯加减计算
     * 
     * @param line
     *            10-4+5
     * @return
     */
    public static String getAddSubResult(String line) {
        String retStr = null;

        String leftStr = null;
        String endStr = null;

        do {
            if (endStr != null) {
                line = leftStr + endStr;
            }
            int index = getMinIndex(line.indexOf("+"), line.indexOf("-"));
            if (index == 0) {
                index = getMinIndex(line.indexOf("+", index + 1), line.indexOf("-", index + 1));
            }
            String sign = String.valueOf(line.charAt(index));
            leftStr = line.substring(0, index);

            int index2 = getMinIndex(line.indexOf("+", index + 1), line.indexOf("-", index + 1));
            String rightStr = null;
            if (index2 != -1) {
                rightStr = line.substring(index + 1, index2);
                endStr = line.substring(index2);
            } else if (index2 == -1) {
                rightStr = line.substring(index + 1);
                endStr = line.substring(index + rightStr.length() + 1);
            }

            switch (sign) {
            case "+":
                leftStr = String.valueOf(Integer.parseInt(leftStr) + Integer.parseInt(rightStr));
                break;
            case "-":
                leftStr = String.valueOf(Integer.parseInt(leftStr) - Integer.parseInt(rightStr));
                break;
            }
            retStr = leftStr + endStr;

        } while (endStr.length() > 1);

        return retStr;
    }

    /**
     * 获取非-1, 较大数字的下标
     * 
     * @param index1
     * @param index2
     * @return
     */
    public static int getMaxIndex(int index1, int index2) {
        int max = -1; // 提前假设两者都是-1
        if (index1 > index2) {
            max = index1;
        }
        if (index1 < index2) {
            max = index2;
        }
        return max;
    }

    /**
     * 获取非-1, 较小数字的下标
     * 
     * @param index1
     * @param index2
     * @return
     */
    public static int getMinIndex(int index1, int index2) {
        int ret = 1;
        if (index1 == -1) {
            ret = index2;
        } else if (index2 == -1) {
            ret = index1;
        } else if (index1 > index2) {
            ret = index2;
        } else if (index1 < index2) {
            ret = index1;
        }
        return ret;

    }

}

  • 写回答

4条回答 默认 最新

  • huocaiyun 2018-01-31 00:35
    关注

    这个if循环用的太多了,但是可以看出是正确的。。。难道遇到什么问题了嘛?

    评论

报告相同问题?

悬赏问题

  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大
  • ¥15 使用spss做psm(倾向性评分匹配)遇到问题