ASD

发布时间 2023-08-17 21:26:40作者: 浮沉丶随心

package com.cicc.irp.riskmgmt.service.impl;

import cn.hutool.core.collection.CollUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

public class ExpressionParser {

public static void main(String[] args) {

    Map<String, List<String>> map = new HashMap<>();
    List<String> A = Arrays.asList("0", "1", "2", "3", "4", "5", "6");
    List<String> B = Arrays.asList("1", "3", "5");
    List<String> C = Arrays.asList("1", "2", "4");
    map.put("A",A);
    map.put("B",B);
    map.put("C",C);


    String expression1 = "     (A and B or (C and D)) or (E and(F or G)) and F and (H or (I and k))";
    String expression2 = "(A and (B or C)";
    List<String> list = evaluateExpression(expression1.trim(), map);

}


public static List<String> evaluateExpression(String expression,Map<String,List<String>> map) {
    // 利用正则表达式将表达式拆分成操作数和操作符
    //
    // String[] tokens = expression.split("\\(");
    //
    // Stack<List<String>> operandStack = new Stack<>(); // 操作数栈
    // Stack<String> operatorStack = new Stack<>(); // 操作符栈
    //
    // for (String token : tokens) {
    //     if (token.equals("(")) {
    //         operatorStack.push(token);
    //     } else if (token.equals(")")) {
    //         // 处理括号内的子表达式
    //         while (!operatorStack.peek().equals("(")) {
    //             // applyOperation(operandStack, operatorStack);
    //         }
    //         operatorStack.pop(); // 弹出左括号
    //     } else if (token.equals("and") || token.equals("or")) {
    //         // 处理逻辑运算符
    //         while (!operatorStack.isEmpty() && !operatorStack.peek().equals("(") && hasHigherPrecedence(token, operatorStack.peek())) {
    //             // applyOperation(operandStack, operatorStack);
    //         }
    //         operatorStack.push(token);
    //     } else {
    //         // 处理操作数
    //         if (token.equals("B")) {
    //             operandStack.push(checkIfEmpty(B));
    //         } else if (token.equals("C")) {
    //             operandStack.push(checkIfEmpty(C));
    //         } else if (token.equals("D")) {
    //             operandStack.push(checkIfEmpty(D));
    //         } else {
    //             // 可以在这里处理其他变量或者抛出异常
    //         }
    //     }
    // }

    // 处理剩余的操作符
    // while (!operatorStack.isEmpty()) {
    //     // applyOperation(operandStack, operatorStack);
    // }
    // return operandStack.pop(); // 返回最终结果
    return new ArrayList<>();
}

private static List<String> applyOperation(List<String> left, String operate,List<String>  right) {
    if(operate.equals("and")){
        left.retainAll(right);
       return left;
    } else {
        List<String> list = new ArrayList<>();
        if(CollUtil.isNotEmpty(left)){
            list.addAll(left);
        }

        if(CollUtil.isNotEmpty(right)){
            list.addAll(right);
        }
        return list;
    }
}

}