手记

四则混合运算算法,不足之处请斧正

最近在做一个计算器应用,但是其中的四则混合运算略有难度,于是尝试着自己写了一下四则混合运算的算法。运行初步觉得较为好用,当然,我不是大神,写的肯定会有许多不足之处,还请大家不吝赐教。

package tye;

import java.util.Scanner;

public class Mac {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //应输入规范的四则混合算式,不含括号
        String s = sc.next();
        Mac m = new Mac();
        System.out.println(m.getMath(s));
    }

    private float getMath(String s) {
        String[] mathString = s.split("[\\+,\\-,\\*,/]");
        float[] value = getValue(s, mathString.length);
        float[] smlValue = getSmlValue(s, value);
        float sum = 0;
        for (int i = 0; i < smlValue.length; i++) {
            sum += smlValue[i];
        }
        return sum;
    }

    private float[] getSmlValue(String s, float[] value) {
        char[] flchar = s.toCharArray();
        char[] fuhao = new char[value.length - 1];
        // 提取符号
        for (int i = 0, j = 0; i < flchar.length; i++) {
            if (flchar[i] == '+' || flchar[i] == '-' || flchar[i] == '*'
                    || flchar[i] == '/') {
                fuhao[j] = flchar[i];
                ++j;
            }
        }
        float[] data = new float[20];
        float[] ret = new float[20];

        boolean isNext = true;// 判断是否中断连乘连除
        // 处理所有乘除
        for (int i = 0, j = 0; i < fuhao.length; i++) {
            if (fuhao[i] == '*') {
                if (isNext) {
                    data[j] = value[i] * value[i + 1];
                } else {
                    data[j - 1] = data[j - 1] * value[i + 1];
                    --j;
                }
                ++j;
                isNext = false;
            } else if (fuhao[i] == '/') {
                if (isNext) {
                    data[j] = value[i] / value[i + 1];
                } else {
                    data[j - 1] = data[j - 1] / value[i + 1];
                    --j;
                }
                ++j;
                isNext = false;
            } else {
                isNext = true;
            }
        }

        // 组合新的数组
        float[] newRt = new float[20];
        for (int i = 0, j = 0, n = 0; i < fuhao.length; i++) {
            if (i == 0 && fuhao[i] != '*' && fuhao[i] != '/') {
                newRt[j] = value[i];
                ++j;
                if(fuhao.length>1){
                if(fuhao[i+1]!= '*'
                    && fuhao[i + 1] != '/'){
                    newRt[j]=value[i+1];
                    ++j;
                }}else{
                    newRt[j]=value[i+1];
                }
            } else if (i <= fuhao.length - 2 && fuhao[i] != '*'
                    && fuhao[i] != '/' && fuhao[i + 1] != '*'
                    && fuhao[i + 1] != '/') {
                newRt[j] = value[i + 1];
                ++j;
            } else if (i > fuhao.length - 2 && fuhao[i] != '*'
                    && fuhao[i] != '/') {
                newRt[j] = value[i + 1];
                ++j;
            } else {
                newRt[j] = data[n];
                ++n;
                ++j;
            }

        }

        return newRt;
    }

    //把所有非符号字符转为浮点数,且保留正负值
    private float[] getValue(String s, int length) {
        float[] flValue = new float[length];
        char[] flchar = s.toCharArray();
        int start = 0;
        int end = 0;
        boolean nextisNun = false;
        for (int i = 0, j = 0; i < flchar.length; i++) {
            if (flchar[i] == '+' || flchar[i] == '-' || flchar[i] == '*'
                    || flchar[i] == '/') {
                end = i;
                String str = s.substring(start, end);
                if (str.equals("") || str == null) {
                    str = "0";
                }
                if (nextisNun) {
                    flValue[j] = 0 - Float.valueOf(str);
                    ++j;
                } else {
                    flValue[j] = Float.valueOf(str);
                    ++j;
                }

                if (flchar[i] == '-') {
                    nextisNun = true;
                } else {
                    nextisNun = false;
                }
                start = end + 1;
            }
        }

        if (nextisNun) {
            flValue[flValue.length - 1] = 0 - Float.valueOf(s.substring(start,
                    flchar.length));
        } else {
            flValue[flValue.length - 1] = Float.valueOf(s.substring(start,
                    flchar.length));
        }
        return flValue;
    }
}

2017/5/5

4人推荐
随时随地看视频
慕课网APP

热门评论

逆波兰式,对于运算优先级比较方便。

查看全部评论