自己写的计算器(加减乘除)代码

首先是Calculator计算器类

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运行类

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;
    }
}

产生表达式类

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;
    }

}

生产者类

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;
    }

}

消费者类

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();
        
    }
}

 

 

代码还是很简单的,主要有两个列表,一个列表存数值,一个列表存符号,如果符号是乘法或者除法,则把当前的数值和数值列表最后的数值相乘或相除,然后覆盖到数字列表的最后,如果不是,则存入数值列表的最后,符号列表添加当前的减法或者乘法,最后我发现许多细节的东西没有处理

posted @ 2017-06-24 21:57  哈久克路  阅读(1747)  评论(0编辑  收藏  举报