Exemplo n.º 1
0
        }     //Add(uint)

        /**
         * Method Name: Mult <br>
         * Method Purpose: multiplies very large UInt's <br>
         *
         * <hr>
         * Date created: 10/15/2015 <br>
         *
         * <hr>
         * Notes on specifications, special algorithms, and assumptions:
         *   multiplies numbers together and accounts for overflow
         *
         * <hr>
         *   @param UInt that is to be multiplied by
         *   @return None
         */
        public void Mult(UInt z)
        {
            UInt[] toAdd = new UInt[count * 200];
            long   l;
            long   zeros = 0;
            int    index = 0;

            for (int i = 0; i < z.getCount(); i++)
            {
                for (int ii = 0; ii < 9; ii++)
                {
                    l = UInt32.Parse(z.getX()[i].ToString("D9").Substring(8 - ii, 1));

                    if (l == 0)
                    {
                        zeros += 1;
                        continue;
                    }
                    else
                    {
                        toAdd[index] = new UInt(Seperate(l, zeros)); //(long)(l * Math.Pow(10, zeros)
                        index       += 1;
                    }
                    zeros += 1;
                }
            }
            for (int i = 0; i < count + 1; i++)
            {
                setX(i, 0);
            }
            for (int i = 0; i < index; i++)
            {
                Add(toAdd[i]);
            }
        }//Mult(uint)
Exemplo n.º 2
0
        }//UInt(uint)

        public UInt(UInt uIn)
        {
            count = uIn.getCount();
            for (int i = 0; i < count + 1; i++)
            {
                x[i] = uIn.getX()[i];
            }
        }//UInt(UInt)
Exemplo n.º 3
0
        }//incCount

        /**
         * Method Name: Add <br>
         * Method Purpose: Adds very large UInt's <br>
         *
         * <hr>
         * Date created: 10/15/2015 <br>
         *
         * <hr>
         * Notes on specifications, special algorithms, and assumptions:
         *   Adds numbers together and accounts for overflow
         *
         * <hr>
         *   @param UInt that is being added to sum
         *   @return None
         */
        public void Add(UInt z)
        {
            long temp;
            uint quot   = 0;
            int  length = z.getCount();             //finds number of elements implemented

            for (int i = 0; i < length; i++)        //adds VL and z elements together
            {
                if (z.getX()[i] == 0)
                {
                    continue;
                }
                temp = x[i] + z.getX()[i];
                if (temp >= max)                    //checks for overflow
                {
                    quot      = (uint)(temp / max); //removes carry and adds it to next element over
                    x[i + 1] += quot;
                    x[i]      = (uint)(temp - (quot * max));
                    if (count <= i)
                    {
                        count = i + 1;
                    }
                }//if
                else
                {
                    x[i] = (uint)temp;                    //no overflow
                    if (count <= i)
                    {
                        count = i + 1;
                    }
                }
            }//for

            if (x[length] > max)                    //checks that carry doesn't cause next element to overflow
            {
                quot           = (x[length] / max);
                x[length + 1] += quot;
                x[length]     -= (quot * max);
                if (count <= length)
                {
                    count = length + 1;
                }
            } //if
             //}//else
        }     //Add(uint)
Exemplo n.º 4
0
        }//Mult(long)

        /**
         * Method Name: Mult <br>
         * Method Purpose: multiplies very large UInt's <br>
         *
         * <hr>
         * Date created: 10/15/2015 <br>
         *
         * <hr>
         * Notes on specifications, special algorithms, and assumptions:
         *   multiplies numbers together and accounts for overflow
         *
         * <hr>
         *   @param long that UInt is multiplied by
         *   @return None
         */
        public UInt Seperate(long n, long zeros)
        {
            long product;
            UInt output = new UInt();

            for (int i = 0; i < count; i++)
            {
                UInt tempUInt  = new UInt();
                long tempZeros = zeros;
                int  index     = i;
                while (tempZeros > 8)
                {
                    output.setX(i, 0);
                    tempZeros -= 9;
                    index     += 1;
                }
                if (x[i] == 0)
                {
                    continue;
                }
                if (tempZeros > 0)
                {
                    product = (long)checked (n * x[i] * Math.Pow(10, tempZeros));
                }
                else
                {
                    product = checked (n * x[i]);
                }
                if (product < max)
                {
                    tempUInt.setX(index, (uint)product);
                    tempUInt.setCount(index + 1);
                }//if
                else
                {
                    tempUInt.setX(index, UInt32.Parse(product.ToString().Substring(product.ToString().Length - 9, 9))); //finds last 9 digits and sets to x[i]
                    product = UInt32.Parse(product.ToString().Substring(0, product.ToString().Length - 9));             //finds remaining digits and sets it to product
                    if (product > max)
                    {
                        tempUInt.setX(index + 1, UInt32.Parse(product.ToString().Substring(product.ToString().Length - 9, 9)));
                        product = UInt32.Parse(product.ToString().Substring(0, product.ToString().Length - 9));
                        tempUInt.setX(index + 2, (uint)product);
                        if (tempUInt.getCount() < index + 3)                                                                      //checks to see count is correct and sets it accordingly
                        {
                            tempUInt.setCount(index + 3);
                        }
                    }//if
                    else
                    {
                        tempUInt.setX(index + 1, (uint)product);                                                           //stores carry for adding to next slot
                        if (tempUInt.getCount() < index + 2)                                                               //checks to see count is correct and sets it accordingly
                        {
                            tempUInt.setCount(index + 2);
                        }
                    } //else
                }     //else

                output.Add(tempUInt);
            }
            return(output);
        }//Mult(long)