コード例 #1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Input numbers in exexpontntal form (1,3e2 or 0,000023E-23)");
            Console.WriteLine("If you want to stop addition press '='\n");


            NewDouble a = NewDouble.Empty;

            do
            {
                Console.Write("Input==>");
                string text = Console.ReadLine();
                if (text == "=")
                {
                    break;
                }

                NewDouble b;
                try
                {
                    b = NewDouble.Input(text);
                }

                catch (FormatException e)
                {
                    Console.WriteLine(e.Message);
                    b = NewDouble.Empty;
                }

                a = a + b;
            } while (true);

            Console.WriteLine($"Result==> {a}");
            Console.ReadKey();
        }
コード例 #2
0
        public static NewDouble Input(string text)
        {
            // Type of the number ==> one or more natural digits -> '.' or ',' -> from one to 40 digits -> 'E' or 'e' -> one or more digit
            Match match = Regex.Match(text, @"^(\d+(?:[\.|,]?\d{1,39})?)(?:[E|e]([+|-]?\d+)?)?$", RegexOptions.Compiled);

            List <int> nums;
            int        exp;

            if (match.Success)
            {
                // take an numbers before 'E' or 'e' and conver to int list, where separator is '.' or ','
                nums = match.Groups[1].Value.Select(s => (int)char.GetNumericValue(s)).ToList();

                //If text doesnt contain separator (4e22, or 12e-1)add an separator and zero ( 4e12 ->4,0e12)
                if (!nums.Contains(-1))
                {
                    nums.AddRange(new[] { -1, 0 });
                }
                if (!text.Contains('e') && !text.Contains('E'))
                {
                    exp = 0;
                }
                // Take an numbers after 'E' or 'e'
                else
                {
                    exp = int.Parse(match.Groups[2].Value);
                }
            }
            else
            {
                throw new FormatException("Invalid format");
            }

            NewDouble result = new NewDouble(exp, nums);

            result.SetNormalForm();
            result.Trim();
            return(result);
        }
コード例 #3
0
        public static NewDouble operator +(NewDouble elem1, NewDouble elem2)
        {
            int resultExponent = (elem1._exponent + elem2._exponent) / 2;

            // Set the same exponential in both numbers
            if (elem1._exponent != elem2._exponent)
            {
                elem1.SetExponent(resultExponent);
                elem2.SetExponent(resultExponent);
            }

            //Synchronization of elements in nums
            int index1 = elem1._nums.IndexOf(-1);
            int index2 = elem2._nums.IndexOf(-1);

            while (index1 != index2)
            {
                if (index1 > index2)
                {
                    elem2._nums.Insert(0, 0);
                    index2++;
                }
                else
                {
                    elem1._nums.Insert(0, 0);
                    index1++;
                }
            }

            while (elem1._nums.Count != elem2._nums.Count)
            {
                if (elem1._nums.Count > elem2._nums.Count)
                {
                    elem2._nums.Insert(elem2._nums.Count, 0);
                }
                else
                {
                    elem1._nums.Insert(elem1._nums.Count, 0);
                }
            }

            // Addition
            int[] resultNums = new int[elem1._nums.Count + 1];

            for (int i = elem1._nums.Count - 1; i >= 0; i--)
            {
                if (elem1._nums[i] == -1)
                {
                    if (resultNums[i + 1] != 0)
                    {
                        resultNums[i]++;
                    }

                    resultNums[i + 1] = -1;
                    continue;
                }

                resultNums[i + 1] += elem1._nums[i] + elem2._nums[i];

                if (resultNums[i + 1] >= 10)
                {
                    resultNums[i + 1] -= 10;
                    resultNums[i]++;
                }
            }

            NewDouble result = new NewDouble(resultExponent, resultNums);

            result.SetNormalForm();
            return(result);
        }