首先是Calculator計算器類java
package zydCalr; public class Calculator { public double addition(double number1, double number2) { return number1+number2; } public double subtraction(double number1, double number2) { return number1-number2; } public double multiplication(double number1, double number2) { return number1*number2; } public double divsition(double number1, double number2) { return number1/number2; } }
接下來是Execute運行類express
package zydCalr; import java.util.Scanner; public class Execution { public double execute(String str){ String expression = str + "+1"; // 初始化開始 // char[] Cexpression = expression.toCharArray(); // 建立運算器 Calculator calculator = new Calculator(); // 數值列表 double[] numbers = new double[30]; int numbersindex = 0; // 轉型列表 char[] sub = new char[30]; // 數值下標位置 int count = 0; // 符號列表 char[] symbols = new char[10]; int symbolsindex = 0; // temp1是數值列表上一個數值,temp2是當前的數值 double temp1 = 0, temp2 = 0; ; // 符號 char symbol = 0; int flag = 1; // 初始化結束 // 第一次遍歷 for (int i = 0; i < expression.length(); i++) { if (Cexpression[i] >= '0' && sub[i] <= '9') { sub[count++] = Cexpression[i]; } else { // 字符串轉型double temp2 = transition(sub,count); // 當flag=2時進行運算 if (flag == 2) { flag = 1; // 獲取數值列表前一個數值; temp1 = numbers[numbersindex - 1]; // 判斷symbol乘法仍是除法,成功是乘法,失敗爲除法 if (decide(symbol)==1) { temp2 = calculator.multiplication(temp1, temp2); } else { temp2 = calculator.divsition(temp1, temp2); } // 覆蓋前一個數值 numbersindex--; } // temp2存入數值列表 numbers[numbersindex++] = temp2; // 獲取符號 symbol = Cexpression[i]; // 轉型數值下標位置清零 count = 0; // 判斷是否大於flag,flag=2,不大於則加入到符號列表 if (judge(symbol) > flag) { flag = 2; } else { // 加入到符號列表 symbols[symbolsindex++] = symbol; } } } double temp = numbers[0]; count = 0; for (int i = 1; i < numbers.length; i++) { if (symbols[count] == '+') { temp += numbers[i]; count++; } else if (symbols[count] == '-') { temp -= numbers[i]; count++; } } return temp; } private static int judge(char symbol) { if (symbol == '+') { return 1; } else if (symbol == '-') { return 1; } else if (symbol == '*') { return 2; } else if (symbol == '/') { return 2; } return 0; } private static int decide(char symbol) { if (symbol == '*') { return 1; } else if (symbol == '/') { return 2; } return 2; } private static int transition(char[] sub1,int count) { int temp = 0; for (int i = 0; i < count; i++) { if (sub1[i] >= '0' && sub1[i] <= '9') { temp = temp * 10; temp += (sub1[i] - '0'); } } return temp; } }
產生表達式類app
package zydCalr; import java.util.Random; public class ProductionExpression {//生成數值 public static String producerNumber() { int number = (int) (Math.random() * 50); return String.valueOf(number); } //生成數值 public static String producerOpreator() { int number = (int) (Math.random() * 4); String[] operator = { "+", "-", "*", "/" }; return operator[number]; } //產生 public static String producer(int i) { StringBuilder sb = new StringBuilder(); int flag = (int) (Math.random() * 5); if(flag==1){ sb.append("-"); } sb.append(producerNumber()); while (i > 0) { sb.append(producerOpreator()); sb.append(producerNumber()); i--; } return sb.toString(); } //產生表達式 public static String producerExpression() { int lenght = (int) (Math.random() * 5); if (lenght % 2 == 0) { lenght++; } String expression = producer(lenght); return expression; } }
生產者類dom
package zydCalr; import java.util.ArrayList; import java.util.List; public class Producer implements Runnable { static final int MAXQUEUE = 5; private List<String> messages = new ArrayList<String>(); private ProductionExpression pe=new ProductionExpression(); @Override public void run() { // TODO Auto-generated method stub while (true) { putMessage(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } private synchronized void putMessage() { while (messages.size() >= MAXQUEUE) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //生成表達式 messages.add(pe.producerExpression()); notify(); } public synchronized String getMessage() { while (messages.size() == 0) { try { notify(); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } String message = (String) messages.remove(0); notify(); return message; } }
消費者類ide
package zydCalr; public class Consumer implements Runnable { Producer producer; Consumer(Producer producer) { this.producer = producer; } public void run() { while (true) { System.out.println("-------------開始一組表達式處理-------------"); String message = producer.getMessage(); System.out.println("生成表達式:" + message); Execution exe=new Execution(); System.out.printf("處理結果:"+"%.2f\n",exe.execute(message)); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Producer producer = new Producer(); new Thread(producer).start(); Consumer consumer = new Consumer(producer); new Thread(consumer).start(); } }
代碼仍是很簡單的,主要有兩個列表,一個列表存數值,一個列表存符號,若是符號是乘法或者除法,則把當前的數值和數值列表最後的數值相乘或相除,而後覆蓋到數字列表的最後,若是不是,則存入數值列表的最後,符號列表添加當前的減法或者乘法,最後我發現許多細節的東西沒有處理ui