Beispiel #1
0
        public static MyBigNumber Sum2MyBigNumbers(MyBigNumber myNmbr1, MyBigNumber myNmbr2)
        {
            int         smallerByAbs = MyBigNumber.SmallerByAbsIsFirstOrSecond(myNmbr1, myNmbr2);
            MyBigNumber bgrByAbs, smlrByAbs;
            int         signOfBigger = 0;

            if (smallerByAbs == 1)
            {
                smlrByAbs    = myNmbr1;
                bgrByAbs     = myNmbr2;
                signOfBigger = myNmbr2.SignNmbr;
            }
            else
            {
                smlrByAbs    = myNmbr2;
                bgrByAbs     = myNmbr1;
                signOfBigger = myNmbr1.SignNmbr;
            }
            string biggerAbsNmbrs  = bgrByAbs.PartDigits;
            string smallerAbsNmbrs = smlrByAbs.PartDigits;
            int    biggerAbsFlPos  = bgrByAbs.FloatPointPosition;
            int    smallerAbsFlPos = smlrByAbs.FloatPointPosition;
            int    biggerFlPPos    = 0;

            if (biggerAbsFlPos > smallerAbsFlPos)
            {
                biggerFlPPos    = biggerAbsFlPos;
                smallerAbsNmbrs = MyBigNumber.AddNmbrZero(smallerAbsNmbrs, -1 * (biggerAbsFlPos - smallerAbsFlPos));
            }
            else
            {
                biggerFlPPos   = smallerAbsFlPos;
                biggerAbsNmbrs = MyBigNumber.AddNmbrZero(biggerAbsNmbrs, -1 * (smallerAbsFlPos - biggerAbsFlPos));
            }
            if (smallerAbsNmbrs.Length < biggerAbsNmbrs.Length)
            {
                smallerAbsNmbrs = MyBigNumber.AddNmbrZero(smallerAbsNmbrs, biggerAbsNmbrs.Length - smallerAbsNmbrs.Length);
            }
            string newStrPartDigits;

            if ((myNmbr1.SignNmbr * myNmbr2.SignNmbr) >= 0)
            {
                newStrPartDigits = MyBigNumber.AddTwoLines(biggerAbsNmbrs, smallerAbsNmbrs);
            }
            else
            {
                newStrPartDigits = MyBigNumber.BiggerMinusSmaller(biggerAbsNmbrs, smallerAbsNmbrs);
            }
            var myNmbr3 = new MyBigNumber()
            {
                PartDigits = newStrPartDigits, SignNmbr = signOfBigger, FloatPointPosition = biggerFlPPos
            };
            string newStringVal = myNmbr3.Value();

            return(MyBigNumber.CreateMyBigNumber(newStringVal));
        }
Beispiel #2
0
        public static int SmallerByAbsIsFirstOrSecond(MyBigNumber myNmbr1, MyBigNumber myNmbr2)
        {
            int         floatPstn1 = myNmbr1.FloatPointPosition;
            int         floatPstn2 = myNmbr2.FloatPointPosition;
            MyBigNumber longFloatPart, shortFloatPart;
            int         longFloatLngth  = 0;
            int         shortFloatLngth = 0;

            if (floatPstn2 > floatPstn1)
            {
                longFloatLngth  = floatPstn2;
                shortFloatLngth = floatPstn1;
                longFloatPart   = myNmbr2;
                shortFloatPart  = myNmbr1;
            }
            else
            {
                longFloatLngth  = floatPstn1;
                shortFloatLngth = floatPstn2;
                longFloatPart   = myNmbr1;
                shortFloatPart  = myNmbr2;
            }
            string longFltStr  = longFloatPart.PartDigits;
            string shortFltStr = MyBigNumber.AddNmbrZero(shortFloatPart.PartDigits, -1 * (longFloatLngth - shortFloatLngth));
            var    lsToEqual   = new List <string>(2);

            lsToEqual.Add(longFltStr);
            lsToEqual.Add(shortFltStr);
            MyBigNumber.IndentLengthWithZeroes(lsToEqual);
            bool smallerLong = MyBigNumber.FirstLineSmallerThanSecond(lsToEqual[0], lsToEqual[1]);
            int  resCmpr     = 0;

            if (smallerLong)
            {
                if (floatPstn2 > floatPstn1)
                {
                    resCmpr = 2;
                }
                else
                {
                    resCmpr = 1;
                }
            }
            else
            {
                if (floatPstn2 > floatPstn1)
                {
                    resCmpr = 1;
                }
                else
                {
                    resCmpr = 2;
                }
            }
            return(resCmpr);
        }
Beispiel #3
0
        public static string DevideByN_baseInt(MyBigNumber myBigNmbr, char n_BaseCHAR)
        {
            string strPartDgt1    = myBigNmbr.PartDigits;
            int    base_N         = int.Parse(n_BaseCHAR.ToString());
            int    mainPartLength = strPartDgt1.Length - myBigNmbr.FloatPointPosition;
            var    lsRmnngs       = new List <int>();
            int    testDigit      = 0;
            string testStr        = "";
            int    rmnngVal       = 0;
            int    hlpVal         = 0;

            lsRmnngs.Add(rmnngVal);
            for (int i = 0; i < mainPartLength; i++)
            {
                testStr   = strPartDgt1[i].ToString();
                testDigit = int.Parse(testStr) + hlpVal * 10;
                if (testDigit < base_N)
                {
                    for (int m = i + 1; m < mainPartLength; m++)
                    {
                        testStr  += strPartDgt1[i + 1].ToString();
                        testDigit = int.Parse(testStr);
                        i++;
                        lsRmnngs.Add(0);
                        if (testDigit >= base_N)
                        {
                            break;
                        }
                    }
                }
                if (testDigit < base_N)
                {
                    lsRmnngs.Add(0);
                    break;
                }
                rmnngVal = testDigit / base_N;
                lsRmnngs.Add(rmnngVal);
                hlpVal = testDigit - rmnngVal * base_N;
            }
            string reslTmp   = string.Join("", lsRmnngs);
            string resNoZero = reslTmp.TrimStart('0');

            if (resNoZero.Length == 0)
            {
                resNoZero = "0";
            }
            else
            {
                if (myBigNmbr.SignNmbr < 0)
                {
                    resNoZero = "-" + resNoZero;
                }
            }
            return(resNoZero);
        }
Beispiel #4
0
        public static MyBigNumber CreateMyBigNumber(string nmbrString)
        {
            string[] strArr   = nmbrString.Split(new char[] { '.', ',', '-', '+' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            int      signNmbr = 1;

            for (int i = 0; i < nmbrString.Length; i++)
            {
                if (nmbrString[i].Equals('-'))
                {
                    signNmbr *= -1;
                }
                else if (!(nmbrString[i].Equals('+')))
                {
                    break;
                }
            }
            string tmpStrVal = strArr[0].TrimStart('0');

            if (tmpStrVal.Equals(""))
            {
                strArr[0] = "0";
            }
            else
            {
                strArr[0] = tmpStrVal;
            }
            int floatPointPosition = 0;

            if (strArr.Length > 1)
            {
                tmpStrVal = strArr[1].TrimEnd('0');
                if (!tmpStrVal.Equals(""))
                {
                    floatPointPosition = tmpStrVal.Length;
                    strArr[0]         += tmpStrVal;
                }
            }

            if (strArr[0].Equals("0"))
            {
                signNmbr = 1;
            }
            MyBigNumber nmbr = new MyBigNumber()
            {
                PartDigits = strArr[0], FloatPointPosition = floatPointPosition, SignNmbr = signNmbr
            };

            return(nmbr);
        }
Beispiel #5
0
        public static List <MyBigNumber> ReturnDevisionSeparatedMdl(MyBigNumber myBigNmbr, char base_N_inCHAR)
        {
            string strBase           = base_N_inCHAR.ToString();
            string intBigPart        = MyBigNumber.DevideByN_baseInt(myBigNmbr, strBase[0]);
            var    myIntPartBigNmbr  = MyBigNumber.CreateMyBigNumber(intBigPart);
            string mltplr            = "-" + strBase;
            var    myHlpBig          = MyBigNumber.CreateMyBigNumber(mltplr);
            var    myNegativeIntPart = MyBigNumber.MultiplyBy(myIntPartBigNmbr, myHlpBig);
            var    myRmnng           = MyBigNumber.Sum2MyBigNumbers(myBigNmbr, myNegativeIntPart);
            var    lsResult          = new List <MyBigNumber>()
            {
                myIntPartBigNmbr, myRmnng
            };

            return(lsResult);
        }
Beispiel #6
0
        public static MyBigNumber MultiplyBy(MyBigNumber myBigNumber, MyBigNumber myBigMultiplier)
        {
            string tmpNmbrString   = myBigNumber.PartDigits;
            string tmpMltplrString = myBigMultiplier.PartDigits;
            var    lstMtpldLines   = new List <string>(tmpMltplrString.Length);

            for (int i = tmpMltplrString.Length - 1; i >= 0; i--)
            {
                string line = MyBigNumber.MultiplyLineByChar(tmpNmbrString, tmpMltplrString[i]);
                line = MyBigNumber.AddNmbrZero(line, (-1 * (tmpMltplrString.Length - 1 - i)));
                lstMtpldLines.Add(line);
            }
            MyBigNumber.IndentLengthWithZeroes(lstMtpldLines);
            string lineSum = lstMtpldLines[0];

            for (int i = 1; i < lstMtpldLines.Count; i++)
            {
                string tmpLine  = lstMtpldLines[i];
                var    lsAppend = new List <string>(2);
                lsAppend.Add(lineSum);
                lsAppend.Add(tmpLine);
                MyBigNumber.IndentLengthWithZeroes(lsAppend);
                lineSum = MyBigNumber.AddTwoLines(lsAppend[0], lsAppend[1]);
            }
            int pstnFloatPoint = myBigNumber.FloatPointPosition + myBigMultiplier.FloatPointPosition;
            int nmbrLeadZeroes = pstnFloatPoint - lineSum.Length + 1;

            if (nmbrLeadZeroes > 0)
            {
                lineSum = MyBigNumber.AddNmbrZero(lineSum, nmbrLeadZeroes);
            }
            int newSign     = myBigMultiplier.SignNmbr * myBigNumber.SignNmbr;
            var newMyNumber = new MyBigNumber()
            {
                PartDigits = lineSum, FloatPointPosition = pstnFloatPoint, SignNmbr = newSign
            };
            string resultValue = newMyNumber.Value();
            var    resMyNumber = MyBigNumber.CreateMyBigNumber(resultValue);

            return(resMyNumber);
        }
Beispiel #7
0
        public static void IndentLengthWithZeroes(List <string> inLsLines)
        {
            int maxLength = 0;

            for (int i = 0; i < inLsLines.Count; i++)
            {
                if (maxLength < inLsLines[i].Length)
                {
                    maxLength = inLsLines[i].Length;
                }
            }
            for (int i = 0; i < inLsLines.Count; i++)
            {
                string elm     = inLsLines[i];
                int    nmbZero = maxLength - elm.Length;
                if (nmbZero > 0)
                {
                    string newElm = MyBigNumber.AddNmbrZero(elm, nmbZero);
                    inLsLines[i] = newElm;
                }
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            string[] strArr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

            int n            = (int)Math.Round(double.Parse(strArr[0]));
            var numbrAtBas10 = MyBigNumber.CreateMyBigNumber(strArr[1]);
            var lsRmnngs     = new List <string>();

            string n_BaseStr     = "" + n;
            string mainPartStr   = MyBigNumber.DevideByN_baseInt(numbrAtBas10, '1');
            string rmnngStr      = "";
            var    myHlpMainPart = MyBigNumber.CreateMyBigNumber(mainPartStr);
            var    testExitBool  = false;

            do
            {
                var lsDvsnResults = MyBigNumber.ReturnDevisionSeparatedMdl(myHlpMainPart, n_BaseStr[0]);

                rmnngStr    = lsDvsnResults[1].Value();
                mainPartStr = lsDvsnResults[0].Value();
                var lsCmprExit = new List <string>(2)
                {
                    n_BaseStr, mainPartStr
                };
                MyBigNumber.IndentLengthWithZeroes(lsCmprExit);
                lsRmnngs.Add(rmnngStr);
                myHlpMainPart = MyBigNumber.CreateMyBigNumber(mainPartStr);
                testExitBool  = MyBigNumber.FirstLineSmallerThanSecond(lsCmprExit[0], lsCmprExit[1]);
            } while (testExitBool);
            string nBasedStr = "" + mainPartStr;

            for (int i = lsRmnngs.Count; i > 0; i--)
            {
                nBasedStr += lsRmnngs[i - 1];
            }
            Console.WriteLine(nBasedStr);
        }