⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java.txt

📁 java 四则运算算法
💻 TXT
字号:
public Vector getDataAndOperator(String calculation) {
        Vector v = new Vector();
        Vector operator = new Vector();
        String[] str = new String[] {"+", "-", "*", "/", "(", ")"};
        for (int i = 0; i < str.length; i++) {
            operator.add(str[i]);
        }
        calculation = calculation.replace(" ", "");
        String data = "";
        for (int i = 0; i < calculation.length(); i++) {
            if (operator.contains(calculation.charAt(i) + "")) {
                if (!data.equals("")) {
                    v.add(data);
                    data = "";
                }
                v.add(calculation.charAt(i) + "");
            } else if (calculation.charAt(i) >= '0' && calculation.charAt(i) <= '9') {
                data = data + calculation.charAt(i);
            } else {
                v.add( -1);
                break;
            }
            if (i == calculation.length() - 1) {
                if (!data.equals("")) {
                    v.add(data);
                }
            }
        }
        return v;
    }

    public void submitB_actionPerformed(ActionEvent e) {
        Vector dataAndOperator = new Vector();
        Stack data = new Stack();
        Vector operator = new Vector();
        Stack operatorData = new Stack();
        String[] str = new String[] {"+", "-", "*", "/", "(", ")", "#"};
        for (int i = 0; i < str.length; i++) {
            operator.add(str[i]);
        }
        int[][] priority = { {2, 2, 1, 1, 1, 2, 2}, {2, 2, 1, 1, 1, 2, 2}, {2, 2, 2, 2, 1, 2, 2}, {2, 2, 2, 2, 1, 2, 2}, {1, 1, 1, 1, 1, 0, 3}, {2, 2, 2, 2, 3, 2, 2}, {1, 1, 1, 1, 1, 3, 0}, //+ - * / ( ) 优先级比较矩阵
        };
        Vector resultV = getDataAndOperator(resultCalculation.getText());
        if (resultV.size() == 1) {
            if (operator.contains(resultV.get(0))) {
                JOptionPane.showMessageDialog(this, "公式含错误的编号", "温馨提示", JOptionPane.ERROR_MESSAGE);
                return;
            } else {
                if (itemCodeV.contains(resultV.get(0))) {
                } else {
                    JOptionPane.showMessageDialog(this, "公式含错误的编号", "温馨提示", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "公式含错误的编号", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (!calculation.getText().replace(" ", "").equals("")) {
            dataAndOperator.add("#");
            dataAndOperator.addAll(getDataAndOperator(calculation.getText()));
            dataAndOperator.add("#");
            if (dataAndOperator.contains(resultV.get(0))) {
                JOptionPane.showMessageDialog(this, "公式逻辑有错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
                return;
            } else {
                if (dataAndOperator.contains( -1)) {
                    JOptionPane.showMessageDialog(this, "公式含有不能识别的符号", "温馨提示", JOptionPane.ERROR_MESSAGE);
                    return;
                } else {
                    for (int i = 0; i < dataAndOperator.size(); i++) {
                        if (i == 0) {
                            operatorData.push(dataAndOperator.get(i));
                        } else {
                            if (operator.contains(dataAndOperator.get(i))) {
                                int m = getOperatorIndex(operatorData.peek().toString().charAt(0));
                                int n = getOperatorIndex(dataAndOperator.get(i).toString().charAt(0));
                                if (priority[m][n] == 1) {
                                    operatorData.push(dataAndOperator.get(i));
                                } else if (priority[m][n] == 2) {
                                    if (data.size() > 1) {
                                        if (operator.contains(dataAndOperator.get(i))) {
                                            i--;
                                            operatorData.pop();
                                            data.pop();
                                        } else {
                                            JOptionPane.showMessageDialog(this, "公式逻辑有错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
                                            return;
                                        }
                                    } else {
                                        JOptionPane.showMessageDialog(this, "公式逻辑有错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
                                        return;
                                    }
                                } else if (priority[m][n] == 3) {
                                    JOptionPane.showMessageDialog(this, "公式逻辑有错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
                                    return;
                                } else if (priority[m][n] == 0) {
                                    operatorData.pop();
                                }
                            } else {
                                if (itemCodeV.contains(dataAndOperator.get(i))) {
                                    data.push(dataAndOperator.get(i));
                                } else {
                                    JOptionPane.showMessageDialog(this, "公式含错误的编号", "温馨提示", JOptionPane.ERROR_MESSAGE);
                                    return;
                                }
                            }
                        }
                        if (i == dataAndOperator.size() - 1 && operatorData.size() == 0) {
                            if (tabletree.getLastSelectedPathComponent() != null) {
                                DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tabletree.getLastSelectedPathComponent();
                                SOW sow = (SOW) selectNode.getUserObject();
                                queryDataSet = DBUtils.executeQuery("select * from ReportItem where id=" + sow.getId() + " and code='" + resultV.get(0).toString() + "'");
                                queryDataSet.editRow();
                                queryDataSet.setString("calculationFormula", "=" + calculation.getText().replace(" ", ""));
                                queryDataSet.post();
                                DBUtils.saveChanges(queryDataSet);
                                tabletree_valueChanged(null);
                                JOptionPane.showMessageDialog(this, "修改公式成功", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                    }

                }
            }
        }
    }

    public int getOperatorIndex(char symbol) {
        int i = -1;
        switch (symbol) {
        case '+':
            i = 0;
            break;
        case '-':
            i = 1;
            break;
        case '*':
            i = 2;
            break;
        case '/':
            i = 3;
            break;
        case '(':
            i = 4;
            break;
        case ')':
            i = 5;
            break;
        case '#':
            i = 6;
            break;
        } //end switch
        return i;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -