使用List在JAVA中模拟精确的小数类(不完整)

使用List在JAVA中模拟精确的小数类(不完整)

题目

由于计算机中浮点数表示具有“不精确性”,无法用在要求精确运算(例如银行)的场景。如果将小数的整数部分和小数部分分别以字符串保存,并且在算术计算时分别计算整数和小数部分(并考虑小数向整数部分的进位),就可以实现精确的小数类。类的属性、方法设计可以参考Java的BigDecimal类。

立意

这是Java作业,原本是要利用字符串去做模拟,不过为了实现无穷大的精确小数类,并且躲开字符串的操作,我选择用易于增加的Int型的List来模拟。
碍于乘除部分实在复杂,我又比较懒,这个作业又是不那么大的作业,我就只写了加减法有兴趣可以根据类似加减法的方式写出乘除法。

讲解思路

暂时不讲,这两天比较忙,而且代码不是很复杂(应该),等我有空,或者有需求的话我再补(有空补大概是不可能,所有有问题就在下边问,我会补齐讲解然后回复你的)。

代码部分

package Matrix;

import java.util.ArrayList;
import java.util.List;

public class Exactdecimal {
    private boolean minus=false;
    private List<Integer> leftValue=new ArrayList<Integer>();
    private List<Integer> rightValue=new ArrayList<Integer>();
    private static int ten=10;


    //构造函数
    public Exactdecimal(String value)
    {
        int record=0;
        String[] ipt=value.split("\\.");
        char[]leftData =ipt[0].toCharArray();
        if(leftData[0]=='-')
        {
            minus=true;
            record=1;
        }
        for (int i=leftData.length-1;i>=record;i--)
        {
            var currentData= String.valueOf(leftData[i]);
            leftValue.add(Integer.parseInt(currentData));
        }
        if(ipt.length>=2)
        {
            char[] rightData = ipt[1].toCharArray();
            for (int i = 0; i < rightData.length; i++)
            {
                var currentData = String.valueOf(rightData[i]);
                rightValue.add(Integer.parseInt(currentData));
            }
        }
    }

    //数加
    private Exactdecimal NumAdd(Exactdecimal tmain, Exactdecimal other)
    {
        int i=0;
        //TODO:位加
        while (tmain.leftValue.size()>i||other.leftValue.size()>i)
        {
            if(tmain.leftValue.size()>i)
            {
                if(other.leftValue.size()>i)
                    tmain.leftValue.set(i,tmain.leftValue.get(i)+other.leftValue.get(i));
            }
            else tmain.leftValue.add(other.leftValue.get(i));
            i++;
        }
        i=0;
        while (tmain.rightValue.size()>i||other.rightValue.size()>i)
        {
            if(tmain.rightValue.size()>i)
            {
                if(other.rightValue.size()>i)
                    tmain.rightValue.set(i,tmain.rightValue.get(i)+other.rightValue.get(i));
            }
            else tmain.rightValue.add(other.rightValue.get(i));
            i++;
        }
        //TODO:进位
        for(i=tmain.rightValue.size()-1;i>=0;i--)
        {
            if(i!=0)
                tmain.rightValue.set(i-1,(tmain.rightValue.get(i)/ten)+tmain.rightValue.get(i-1));
            else
                tmain.leftValue.set(0,(tmain.rightValue.get(i)/ten)+tmain.leftValue.get(0));
            tmain.rightValue.set(i,tmain.rightValue.get(i)%ten);
        }
        for (i=0;i<tmain.leftValue.size();i++)
        {
            if(i+1<tmain.leftValue.size())
                tmain.leftValue.set(i+1,(tmain.leftValue.get(i)/ten)+tmain.leftValue.get(i+1));
            else
               if((tmain.leftValue.get(i) / ten)>0)tmain.leftValue.add(tmain.leftValue.get(i)/ten);
            tmain.leftValue.set(i,tmain.leftValue.get(i)%ten);
        }
        return this;
    }
    //数减
    private Exactdecimal NumSubtract(Exactdecimal tmain,Exactdecimal other)//大数在前
    {
        //TODO:零的同步
        while (true)
        {
            if(tmain.rightValue.size()>=other.rightValue.size())
                break;
            tmain.rightValue.add(0);
        }
        while (true)
        {
            if(tmain.rightValue.size()<=other.rightValue.size())
                break;
            other.rightValue.add(0);
        }
        //TODO:位减
        for (int i=0;i<tmain.rightValue.size();i++)
        {
            tmain.rightValue.set(i,tmain.rightValue.get(i)-other.rightValue.get(i));
        }
        for (int i=0;i<tmain.leftValue.size();i++)
        {
            if(other.leftValue.size()-1>=i)
            tmain.leftValue.set(i,tmain.leftValue.get(i)-other.leftValue.get(i));
        }
        //TODO:进位
        for (int i=tmain.rightValue.size()-1;i>=0;i--)
        {
            if(tmain.rightValue.get(i)<0)
            {
                if(i==0)
                {
                    tmain.leftValue.set(0,tmain.leftValue.get(0)-1);
                    tmain.rightValue.set(i,10+tmain.rightValue.get(i));
                    break;
                }
                tmain.rightValue.set(i-1,tmain.rightValue.get(i-1)-1);
                tmain.rightValue.set(i,10+tmain.rightValue.get(i));
            }
        }
        for (int i=0;i<tmain.leftValue.size();i++)
        {
            if(tmain.leftValue.get(i)<0)
            {
                tmain.leftValue.set(i + 1, tmain.leftValue.get(i + 1) - 1);
                tmain.leftValue.set(i, 10 - tmain.leftValue.get(i));
            }
        }
        //TODO:去零
        int i=tmain.leftValue.size()-1;
        if(i!=0)
        {
            while (tmain.leftValue.get(i)==0&&i!=0)
            {
                tmain.leftValue.remove(i);
                i--;
            }
        }

        return tmain;
    }
    //减法
    public Exactdecimal Subtract(Exactdecimal other)
    {
        Exactdecimal resultofthis = null;
        int state=ThisIsBigThenOther(other);

        if (!this.minus && other.minus || !other.minus && this.minus)
        {
            resultofthis= NumAdd(this,other);
            if(!other.minus)
                resultofthis.minus = true;
        }
        else {
            switch (state)
            {
                case 1:
                    resultofthis = NumSubtract(this,other);
                    break;
                case -1:
                    resultofthis = NumSubtract(other,this);
                    resultofthis.minus=true;
                    break;
                case 0:
                    resultofthis = new Exactdecimal("0.0");
                    break;
            }
            if(this.minus)
                resultofthis.minus=!resultofthis.minus;;
        }
        return resultofthis;
    }
    //加法
    public Exactdecimal Add(Exactdecimal other)
    {
        Exactdecimal resultofthis = null;

        if (!this.minus && other.minus || !other.minus && this.minus)
        {
            int state=ThisIsBigThenOther(other);
            switch (state)
            {
                case 1:
                    resultofthis = NumSubtract(this,other);
                    break;
                case -1:
                    resultofthis = NumSubtract(other,this);
                    resultofthis.minus=true;
                    break;
                case 0:
                    resultofthis = new Exactdecimal("0.0");
                    break;
            }
            if(this.minus)
                resultofthis.minus=!resultofthis.minus;;
        }
        else {
            resultofthis= NumAdd(this,other);
            if(!other.minus)
                resultofthis.minus = true;
        }
        return resultofthis;
    }
    //输出
    public void  ShowData()
    {
        if(this.minus==true)
            System.out.print("-");
        for (int i=leftValue.size()-1;i>=0;i--)
        {
            System.out.print(leftValue.get(i));
        }
        System.out.print(".");
        for (int i=0;i<rightValue.size();i++)
        {
            System.out.print(rightValue.get(i));
        }
    }
    //当前数是否大于另一个
    private int ThisIsBigThenOther(Exactdecimal other)
    {
        if(this.leftValue.size()>other.leftValue.size())
        {
            return 1;
        }else if(this.leftValue.size()<other.leftValue.size())
        {
            return -1;
        }
        for (int i=this.leftValue.size()-1;i>=0;i--)
        {
            if(this.leftValue.get(i)>other.leftValue.get(i))
            {
                return 1;
            }
            else if(this.leftValue.get(i)<other.leftValue.get(i))
            {
                return -1;
            }
        }
        if(this.rightValue.size()>other.rightValue.size())
        {
            return 1;
        }else if(this.rightValue.size()<other.rightValue.size())
        {
            return -1;
        }else
        {
            for(int i=0;i<this.rightValue.size();i++)
            {
                if(this.rightValue.get(i)>other.rightValue.get(i))
                {
                    return 1;
                }
                else if(this.rightValue.get(i)<other.rightValue.get(i))
                {
                    return -1;
                }
            }
            return 0;
        }
    }

    public  static String reverseTestOne(String s)
    {
        return  new StringBuffer(s).reverse().toString();
    }
}

效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_52540105/article/details/127454988