Пример #1
0
        public string Subtraction(string Number1, string Number2)
        {// Result = Number1 - Number2
            #region For Short Result
            // if this process can't be made with easy, i am going to make with "String"
            if (Number1 == Number2)
            {
                return("0");
            }
            if (mySortClassObject.NumberIsZero(Number1))
            {
                return("-" + Number2);
            }
            if (mySortClassObject.NumberIsZero(Number2))
            {
                return(Number1);
            }
            if (!ForDeveloper && !NumberIsSoBig)
            {
                double example0, example1;
                if (double.TryParse(Number1, out example0) && double.TryParse(Number2, out example1))
                {
                    return((example0 - example1).ToString());
                }
            }
            if (Checking)
            {
                XtraMessageBox.Show("\tThis process can't do without this algorithm. Because your number is so big.\nError: OverFlow");
                Checking = false;
                return("0");
            }

            #endregion

            #region For Numbers Containing ","
            int    MaxLengthOfDenominator = 0;
            string DenominatorNumber0 = "1", DenominatorNumber1 = "1"; // number count is 2 => Number1 and Number2
            if (Number1.Contains(","))                                 // for example, if Number1 is 22,2 (decimal) so, if it is not integer
            {
                // 22,2 => 222 / 10
                // for caracter length that after comma is "1", Denominator of this number is "10"
                // if caracter length that after comma were being "2", Denominator of this number was being "100"
                string Denominator = "1";
                int    length      = Number1.Substring(Number1.IndexOf(",") + 1).Length; // caracter that After comma (",")
                for (int bx = 0; bx < length; bx++)
                {
                    Denominator += "0";
                }
                DenominatorNumber0 = Denominator;
                Number1            = Number1.Replace(",", ""); // i remove ",".  22,2 => 222 / 10
            }
            if (Number2.Contains(","))
            {
                // 22,2 => 222 / 10
                string Denominator = "1";
                int    length      = Number2.Substring(Number2.IndexOf(",") + 1).Length; // After comma (",")
                for (int bx = 0; bx < length; bx++)
                {
                    Denominator += "0";
                }
                DenominatorNumber1 = Denominator;
                Number2            = Number2.Replace(",", ""); // i remove ",". 22,2 => 222 / 10
            }

            if (DenominatorNumber0 != "1" || DenominatorNumber1 != "1") // if Denominator numbers is not be "1", comma is there. So we do process numbers with comma
            {
                if (DenominatorNumber0.Length > DenominatorNumber1.Length)
                {
                    MaxLengthOfDenominator = DenominatorNumber0.Length;
                }
                else if (DenominatorNumber1.Length > DenominatorNumber0.Length)
                {
                    MaxLengthOfDenominator = DenominatorNumber1.Length;
                }
                else // 2 numbers is equal. Therefore it does not matter
                {
                    MaxLengthOfDenominator = DenominatorNumber0.Length;
                }
                if (DenominatorNumber0.Length != MaxLengthOfDenominator)
                {
                    string Denominator = "1";
                    int    ZeroCount   = MaxLengthOfDenominator - DenominatorNumber0.Length;
                    for (int bx = 0; bx < ZeroCount; bx++)
                    {
                        Denominator += "0";
                    }
                    Number1 = Multiplication(Number1, Denominator);
                }
                if (DenominatorNumber1.Length != MaxLengthOfDenominator)
                {
                    string Denominator = "1";
                    int    ZeroCount   = MaxLengthOfDenominator - DenominatorNumber1.Length;
                    for (int bx = 0; bx < ZeroCount; bx++)
                    {
                        Denominator += "0";
                    }
                    Number2 = Multiplication(Number2, Denominator);
                }
            }

            #endregion

            #region Sorting
            bool ResultIsNegative = false;
            // Number1 should be the biggest one for my algorithm
            if (Number2.Replace("-", "").Replace(",", "").Length > Number1.Replace("-", "").Replace(",", "").Length)
            {
                string Next = Number1;
                Number1          = Number2;
                Number2          = Next;
                ResultIsNegative = true;
                // 3 - 4 = -(4 - 3)
            }
            else if (Number2.Length == Number1.Length)
            {
                if (Number1.Contains(",") || Number2.Contains(","))
                {
                    int index1 = Number1.IndexOf(","), index2 = Number2.IndexOf(",");
                    if ((index2 > index1 && index1 != -1) || (index2 == -1 && index1 != -1)) // number2 is big
                    {
                        string Next = Number1;
                        Number1          = Number2;
                        Number2          = Next;
                        ResultIsNegative = true;
                    }
                    else if (index1 == index2)// equal
                    {
                        string[] numberss = { Number1.Replace(",", "").Replace("-", ""), Number2.Replace(",", "").Replace("-", "") };
                        if (mySortClassObject.SortByStringLength(numberss)[1] == Number2.Replace(",", "").Replace("-", "")) // if number2 is big
                        {
                            string Next = Number1;
                            Number1          = Number2;
                            Number2          = Next;
                            ResultIsNegative = true;
                        }
                    }
                }
                else
                {
                    string[] numberss = { Number1.Replace("-", ""), Number2.Replace("-", "") };
                    if (mySortClassObject.SortByStringLength(numberss)[1] == Number2.Replace("-", "")) // if number2 is big
                    {
                        string Next = Number1;
                        Number1          = Number2;
                        Number2          = Next;
                        ResultIsNegative = true;
                    }
                }
            }

            #endregion

            #region Negative Numbers Control

            if (Number2.StartsWith("-") && Number1.StartsWith("-"))
            {
                // for example; -3 - (-2) = -3 + 2
                Number1          = Number1.Substring(1);
                ResultIsNegative = true; // -3 + 2 = -(3 - 2), therefore i will put "-" in the end like that;
                // 3 - 2 = 1 ---> -1
            }
            else if (Number1.StartsWith("-"))
            {
                // for example; -3 - 2 = -(3 + 2) so, i add 3 and 2. Then i will put "-" to result
                string[] Numbers = { Number1, Number2 };
                return("-" + Addition(Numbers));
            }
            else if (Number2.StartsWith("-"))
            {
                // for example; 3 - (-2) => 3 + 2
                string[] Numbers = { Number1, Number2 };
                return(Addition(Numbers));
            }

            #endregion

            // string Result = Addition(DataToBeCollected);
            #region Subtraction Process

            //  987654321 (43211)
            //- 123456789
            //-------------
            //      97532
            string Result = "";
            sbyte  Data;
            for (int ax = 1; ax <= Number1.Length; ax++)
            {
                if (Number2.Length >= ax)
                {
                    // 44500000
                    // 10000008
                    //-_________
                    //        2

                    Data = Convert.ToSByte(Convert.ToByte(Number1.Substring(Number1.Length - ax, 1)) - Convert.ToByte(Number2.Substring(Number2.Length - ax, 1)));
                    if (Data < 0)
                    {
                        Data  += 10;
                        Result = Data.ToString() + Result;
                        int NewNumber = ax + 1;
                        while (Number1.Substring(Number1.Length - NewNumber, 1) == "0")
                        {
                            NewNumber++;
                        }
                        //  987654311 (43211)
                        //- 123456789
                        //-------------
                        //      97532

                        // 44500000
                        // 10000008
                        //-_________
                        //
                        string NewData = Number1.Substring(0, Number1.Length - NewNumber) + Convert.ToByte(Convert.ToByte(Number1.Substring(Number1.Length - NewNumber, 1)) - 1).ToString();
                        if (NewNumber > ax + 1)
                        {
                            // i should change "0" to "9". For example;
                            //1000 > 100(0) - (2) > 099(10) - (2) > 0998
                            //   2
                            //-____
                            //0998 , in this example, i changed "ten digits" and "hundered digits".
                            int ZeroCount = (NewNumber - ax - 1);
                            for (int cx = 0; cx < ZeroCount; cx++)
                            {
                                NewData += "9";
                            }
                            NewData += Number1.Substring(Number1.Length - NewNumber + 1 + ZeroCount);
                        }
                        else
                        {
                            NewData += Number1.Substring(Number1.Length - NewNumber + 1);
                        }
                        Number1 = NewData;
                    }
                    else
                    {
                        Result = Data.ToString() + Result;
                    }
                }
                else
                {
                    Result = Number1.Substring(Number1.Length - ax, 1) + Result;
                }
            }
            #endregion

            // Result may be "0052" or "024" and such like ...
            // So, in left of result, it may contains "0"
            // therefore i should delete in left of result
            string FinalResult = mySortClassObject.DeleteTheZero(Result); // 55 / 10 > -55 / 10
            if (ResultIsNegative)
            {
                FinalResult = "-" + FinalResult;
            }

            #region For Numbers Containing "," (end)

            if (MaxLengthOfDenominator > 0) // if number1 or number2 contain comma
            {
                // For Example;
                // (345 / 100) - (220 / 100)
                // => (345 - 220) / 100 So,
                // => Result / 100 = True Result
                // i should divison Result and 100 in this example
                string Denominator = "1";
                for (int ax = 1; ax < MaxLengthOfDenominator; ax++)
                {
                    Denominator += "0";
                }

                return(Division(FinalResult, Denominator, false));
            }
            #endregion
            return(FinalResult);
        }