字符串加减乘除计算

package com.test.algorithm.sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符计算机
 *字符串加减乘除计算
 * @author fym
 */
public class TestFour {

    public static void main(String[] args) {
        String str = "(1+(3*2)-(2/1))";
        start(str);
    }

    @SuppressWarnings("unchecked")
    private static void start(String str) {
        System.out.println(str);

        List<Object> stackAll = new ArrayList<Object>();

        Stack stack = new Stack();
        Stack stack2 = new Stack();

        List<Integer> list1 = new ArrayList<Integer>();
        List<String> list2 = new ArrayList<String>();

        char[] charArr = str.toCharArray();
        for (int i = 0; i < charArr.length; i++) {
            String str2 = charArr[i] + "";
            if (figure(str2)) {
                Object[] objArr = m(charArr, i);
                stack.add(objArr[0]);
                list1.add(Integer.parseInt(objArr[0].toString()));
                i = (Integer) objArr[1];
                stackAll.add(objArr[0]);
            } else {
                stack2.add(str2);
                list2.add(str2.toString());
                stackAll.add(str2);
            }
        }

        Stack stack3 = new Stack();

        for (int i = 0; i < stackAll.size(); i++) {
            String s = (String) stackAll.get(i);
            if (s.equals(")")) {
                Stack temp_stack = new Stack();
                while (!stack3.empty()) {
                    Object obj = stack3.pop();
                    if (obj.equals("(")) {
                        double count = 0;
                        while (!temp_stack.empty()) {
                            if (temp_stack.size() == 3) {
                                Object one = temp_stack.pop();
                                Object two = temp_stack.pop();
                                Object three = temp_stack.pop();
                                if (two.equals("+")) {
                                    count = Double.parseDouble(one.toString()) + Double.parseDouble(three.toString());
                                    break;
                                } else if (two.equals("-")) {
                                    count = Double.parseDouble(one.toString()) - Double.parseDouble(three.toString());
                                    break;
                                } else if (two.equals("*")) {
                                    count = Double.parseDouble(one.toString()) * Double.parseDouble(three.toString());
                                    break;
                                } else if (two.equals("/")) {
                                    count = Double.parseDouble(one.toString()) / Double.parseDouble(three.toString());
                                    break;
                                }
                            } else if (temp_stack.size() > 3) {
                                Object one = temp_stack.pop();
                                Object two = temp_stack.pop();
                                Object three = temp_stack.pop();
                                if (two.equals("*")) {
                                    Double four = Double.parseDouble(one.toString()) * Double.parseDouble(three.toString());
                                    temp_stack.push(four);
                                    continue;
                                } else if (two.equals("/")) {
                                    Double four = Double.parseDouble(one.toString()) / Double.parseDouble(three.toString());
                                    temp_stack.push(four);
                                    continue;
                                }
                                Object four = temp_stack.pop();
                                if (four.equals("+") || four.equals("-")) {
                                    if (two.equals("+")) {
                                        Double four2 = Double.parseDouble(one.toString()) + Double.parseDouble(three.toString());
                                        temp_stack.push(four);
                                        temp_stack.push(four2);
                                        continue;
                                    } else if (two.equals("-")) {
                                        Double four2 = Double.parseDouble(one.toString()) - Double.parseDouble(three.toString());
                                        temp_stack.push(four);
                                        temp_stack.push(four2);
                                        continue;
                                    }
                                }
                                if (four.equals("*")) {
                                    Double five = Double.parseDouble(temp_stack.pop().toString());
                                    Double six = Double.parseDouble(three.toString()) * five;
                                    temp_stack.push(six);
                                    temp_stack.push(two);
                                    temp_stack.push(one);
                                    continue;

                                } else if (four.equals("/")) {
                                    Double five = Double.parseDouble(temp_stack.pop().toString());
                                    Double six = Double.parseDouble(three.toString()) / five;
                                    temp_stack.push(six);
                                    temp_stack.push(two);
                                    temp_stack.push(one);
                                    continue;
                                }
                            }
                        }
                        stack3.push(count);
                        break;
                    }
                    temp_stack.add(obj);
                }
                continue;
            }
            stack3.add(s);
        }
        System.out.println(stack3);
    }

    public static Object[] m(char[] charArr, int startIndex) {
        Object[] objArr = new Object[2];
        boolean b = false;
        int outindex = 0;
        String returnStr = "";
        for (int i = startIndex; i < charArr.length; i++) {
            String str = charArr[i] + "";
            b = figure(str);
            if (b) {
                returnStr += str;
            } else {
                outindex = i;
                break;
            }
        }
        objArr[0] = returnStr;
        objArr[1] = outindex - 1;
        return objArr;
    }

    public static boolean figure(String str) {
        Pattern pattern = Pattern.compile("[0-9]");
        Matcher m = pattern.matcher(str);
        boolean b = m.matches();
        return b;
    }

    public static boolean Str(String str) {
        String pattern = "(+-*/)";
        return pattern.contains(str);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值