您好,登錄后才能下訂單哦!
Java中怎么實現并行計算器,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
代碼實現:
package test;import java.util.*;import java.util.concurrent.*;public class Main { final static ExecutorService executorService = Executors.newCachedThreadPool(); public static void main(String [] args) throws ExecutionException, InterruptedException { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { System.out.println("請輸入加法計算器和乘法計算器個數"); String inputStr = scanner.nextLine(); String[] operateNumArry = scanner.nextLine().split(" "); int plusNum = Integer.parseInt(operateNumArry[0]); int mulNum = Integer.parseInt(operateNumArry[1]); if (plusNum < 0 || mulNum < 0) { System.out.println("并行計算器數量要大于0"); break; } LinkedList<Integer> plus_numbers = new LinkedList<>(); LinkedList<LinkedList<Integer>> mul_number_array = new LinkedList<>(); String [] inputArray = inputStr.split("\\+"); for (int i = 0; i < inputArray.length; i++) {if (!inputArray[i].contains("*")) { plus_numbers.add(Integer.parseInt(inputArray[i])); } else { LinkedList<Integer> mul_numbers = new LinkedList<>(); String [] mul_numbers_str = inputArray[i].split("\\*"); for (int j = 0; j < mul_numbers_str.length; j++) { mul_numbers.add(Integer.parseInt(mul_numbers_str[j])); }//乘排序 Collections.sort(mul_numbers); //添加到list mul_number_array.add(mul_numbers); } } StringBuilder builder = new StringBuilder(); calculateSum(plus_numbers, mul_number_array, plusNum, mulNum,builder); System.out.println(builder.toString()); } } /** * 計算 * @param plusNumbers * @param mulNumbers * @param plusNum * @param mulNum */ public static void calculateSum(LinkedList<Integer> plusNumbers, LinkedList<LinkedList<Integer>> mulNumbers, int plusNum, int mulNum, StringBuilder builder) throws ExecutionException, InterruptedException {//首先打印 printResult(plusNumbers, mulNumbers, builder); if (plusNumbers.size() == 1) {return; } //計算加 int requiredPlusNum = plusNumbers.size(); if (requiredPlusNum > plusNum) requiredPlusNum = plusNum; else if (requiredPlusNum <= plusNum) requiredPlusNum--; for (int i = 0; i < requiredPlusNum; i++) { Integer first_plus_factor = plusNumbers.poll(); Integer second_plus_factor = plusNumbers.poll(); Future<Integer> future = executorService.submit(new Operator(first_plus_factor, second_plus_factor, 0)); plusNumbers.addLast(future.get()); } //計算乘 int requiredMulNum = mulNumbers.size(); if (requiredMulNum > mulNum) requiredMulNum = mulNum; for (int i = 0; i < requiredMulNum; i++) { LinkedList<Integer> mulNumberArray = mulNumbers.poll(); Integer first_mul_factor = mulNumberArray.poll(); Integer second_mul_factor = mulNumberArray.poll(); Future<Integer> future = executorService.submit(new Operator(first_mul_factor, second_mul_factor, 2)); if (mulNumberArray.isEmpty()) plusNumbers.add(future.get()); else { mulNumberArray.offer(future.get()); mulNumbers.offer(mulNumberArray); } } //遞歸調用 calculateSum(plusNumbers, mulNumbers, plusNum, mulNum, builder); } static void printResult(final LinkedList<Integer> plusNumbers, final LinkedList<LinkedList<Integer>> mulNumbers, StringBuilder builder) {//加排序 Collections.sort(plusNumbers); builder.append("{"); plusNumbers.forEach(item-> {builder.append(item).append("+"); }); if (!mulNumbers.isEmpty()) {//相同數量的乘因子按第一個不同的最小值排序 Collections.sort(mulNumbers, new CustomComparator()); mulNumbers.forEach(item->{ item.forEach(i->{builder.append(i).append("*"); }); builder.deleteCharAt(builder.lastIndexOf("*")).append("+"); }); } builder.deleteCharAt(builder.lastIndexOf("+")).append("}").append("\n"); } static class CustomComparator implements Comparator<List<Integer>> {@Override public int compare(List<Integer> o1, List<Integer> o2) {if (o1.size() == o2.size()) {for (int i = 0; i < o1.size(); i++) {if (o1.get(i) > o2.get(i))return 1; else if (o1.get(i) < o2.get(i))return -1; else return 0; } return 0; } else if (o1.size() > o2.size()){return 1; } return -1; } } static class Operator implements Callable <Integer> {//第一因子 private int firstNumber; //第二因子 private int secondNumber; //操作類型:加、減、乘、除:0/1/2/3 private int type; public Operator(int firstNumber, int secondNumber, int type) {this.secondNumber = secondNumber; this.firstNumber = firstNumber; this.type = type; } public Integer call() throws Exception {if (type == 0)return firstNumber + secondNumber; if (type == 1)return firstNumber - secondNumber; if (type == 2)return firstNumber * secondNumber; if (type == 3)return firstNumber / secondNumber; if (type == 4)return firstNumber % secondNumber; return 0; } } }
關于Java中怎么實現并行計算器問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。