ITPub博客

首页 > IT职业 > IT职场 > 四则混合运算算法,不足之处请斧正

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

IT职场 作者:ylzwei 时间:2021-09-09 09:01:46 0 删除 编辑

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

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 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] != '/') {
                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 

2017/5/5

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2472/viewspace-2798747/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    109
  • 访问量
    871