Ejemplo n.º 1
0
        public static string Multiply(string op_1, string op_2, NumericSystems NumSystem)
        {
            // split <first operand> into integer & fraction parts
            var(op_1_int_part, op_1_frac_part) = Formatter.SplitNumberByDot(op_1);

            string result = "0";

            // adding integer part <int_part> times
            var multiply_list = op_1_int_part.Reverse().ToArray();

            for (int i = 0; i < multiply_list.Length; i++)
            {
                for (int j = 0; j < NumberOf(multiply_list[i]); j++)
                {
                    result = Plus(result, Displace(op_2, i), NumSystem);
                }
            }

            // adding fraction part <frac_part> times
            for (int i = 0; i < op_1_frac_part.Length; i++)
            {
                for (int j = 0; j < NumberOf(op_1_frac_part[i]); j++)
                {
                    result = Plus(result, Displace(op_2, -(i + 1)), NumSystem);
                }
            }

            return(result);
        }
        private void Convert_Button_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBox_IntialNumericSystem.SelectedItem == null ||
                string.IsNullOrEmpty(TextBox_InitialNumber.Text))
            {
                MessageBox.Show("Missing data in fields!");
            }
            else
            {
                NumericSystems intialNumericSystem = (NumericSystems)ComboBox_IntialNumericSystem.SelectedItem;
                Validator      validator           = new Validator(intialNumericSystem, TextBox_InitialNumber.Text);

                if (validator.isValidate())
                {
                    FloatingNumberConvertor convertor = validator.GetFloatingNumberConvertor();

                    var(str, MachineCode) = convertor.Convert();
                    (MachineCode_TextBlock.Text, NumberResult_TextBlock.Text) = (MachineCode.ToString(), str);
                }
                else
                {
                    MessageBox.Show("Number is not validate to specified numeric system!");
                }
            }
        }
Ejemplo n.º 3
0
        public void NumericSystem_Converts_To_Another(string value, NumericSystem from, NumericSystem to, string expected)
        {
            //arrage
            DataStruct data = null;

            IEnumerable <DataStruct> dataList = new List <DataStruct>
            {
                new DataStruct
                {
                    Value = value,
                    From  = from,
                    To    = to
                }
            };

            var dataManager = new Mock <IDataManager>();

            dataManager.Setup(x => x.GetData()).Returns(dataList);
            dataManager.Setup(x => x.ProcessData(It.IsAny <DataStruct>())).Callback <DataStruct>(x => data = x);

            //act
            NumericSystems.Convert(dataManager.Object);

            //assert
            Assert.Equal(expected, data.Result);
        }
Ejemplo n.º 4
0
        private void Calculate_Button_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBox_NumericSystem.SelectedItem == null ||
                ComboBox_Operation.SelectedItem == null ||
                string.IsNullOrEmpty(TextBox_InitialOperand_1.Text) ||
                string.IsNullOrEmpty(TextBox_InitialOperand_2.Text))
            {
                MessageBox.Show("Missing data in fields!");
            }
            else
            {
                NumericSystems numericSystem = (NumericSystems)ComboBox_NumericSystem.SelectedItem;
                Operations     operation     = (Operations)ComboBox_Operation.SelectedItem;

                Validator validator_operand_1 = new Validator(numericSystem, TextBox_InitialOperand_1.Text);
                Validator validator_operand_2 = new Validator(numericSystem, TextBox_InitialOperand_2.Text);

                if (!validator_operand_1.isValidate())
                {
                    MessageBox.Show("First operand is not validate to specified numeric system!");
                }
                else
                if (!validator_operand_2.isValidate())
                {
                    MessageBox.Show("Second operand is not validate to specified numeric system!");
                }
                else
                {
                    Calculator calculator = new Calculator(numericSystem,
                                                           TextBox_InitialOperand_1.Text, TextBox_InitialOperand_2.Text);

                    Calculated_TextBlock.Text = calculator.Solve(operation);
                }
            }
        }
        private void Convert_Button_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBox_ConvertedFrom.SelectedItem == null ||
                ComboBox_ConvertedTo.SelectedItem == null ||
                string.IsNullOrEmpty(TextBox_InitialNumber.Text))
            {
                MessageBox.Show("Missing data in fields!");
            }
            else
            {
                NumericSystems numericSystemConvertFrom = (NumericSystems)ComboBox_ConvertedFrom.SelectedItem;
                NumericSystems numericSystemConvertTo   = (NumericSystems)ComboBox_ConvertedTo.SelectedItem;
                Validator      validator = new Validator(numericSystemConvertFrom, TextBox_InitialNumber.Text);

                if (validator.isValidate())
                {
                    Convertor convertor = validator.GetConvertor();

                    Converted_TextBlock.Text = convertor.ConvertToOtherSystem(numericSystemConvertTo);
                }
                else
                {
                    MessageBox.Show("Number is not validate to specified numeric system!");
                }
            }
        }
Ejemplo n.º 6
0
        private string ConvertToGivenNumericSystem(string s, NumericSystems numericSystems)
        {
            string result = "";

            try
            {
                switch (numericSystems)
                {
                case NumericSystems.Binary:
                    result = Convert.ToString(Convert.ToInt32(s, 10), 2);
                    break;

                case NumericSystems.Decimal:
                    result = Convert.ToString(Convert.ToInt32(s, 2), 10);
                    break;
                }
            }
            catch (OverflowException)
            {
                MessageBox.Show(@"The number is too big!");
                return("0");
            }

            return(result);
        }
        public static string GetInvertedNumber(string number, NumericSystems NumSystem)
        {
            var result = "";

            for (int i = 0; i < number.Length; i++)
            {
                result += SymbolOf((byte)((byte)NumSystem - 1 - NumberOf(number[i])));
            }

            return(result);
        }
Ejemplo n.º 8
0
        public string ConvertToOtherSystem(NumericSystems otherNumSystem)
        {
            if (otherNumSystem == intitialNumericSystem)
            {
                return(Formatter.GetFormat(number));
            }
            else
            if (otherNumSystem == NumericSystems.Decimal)
            {
                var(numb, fraction) = ConvertToDecimalSystem();
                return(Formatter.GetFormat(numb, fraction));
            }
            else // (otherNumSystem != originalNumSystem)
            {
                BigInteger integer_part;
                double     fraction_part;

                if (intitialNumericSystem != NumericSystems.Decimal)
                {
                    (integer_part, fraction_part) = ConvertToDecimalSystem();
                }
                else
                {
                    var(str_int, str_frac)        = Formatter.SplitNumberByDot(number);
                    (integer_part, fraction_part) = (BigInteger.Parse(str_int), double.Parse("0." + str_frac));
                }

                StringBuilder
                    sb_integer  = new StringBuilder(),
                    sb_fraction = new StringBuilder();

                while (integer_part != 0)
                {
                    sb_integer.Append(SymbolOf((byte)(integer_part % (int)otherNumSystem)));

                    integer_part /= (int)otherNumSystem;
                }

                while (sb_fraction.Length < 40 && fraction_part != 0)
                {
                    fraction_part *= (int)otherNumSystem;
                    var integer_number = (byte)Math.Truncate(fraction_part);

                    sb_fraction.Append(SymbolOf(integer_number));
                    fraction_part -= integer_number;
                }


                return(Formatter.GetFormat(
                           string.Join("", sb_integer.ToString().Reverse()), sb_fraction.ToString()));
            }
        }
Ejemplo n.º 9
0
        public void NotFound_Data_Throws_ArgumentNullException()
        {
            //arrage
            var dataManager = new Mock <IDataManager>();

            dataManager.Setup(x => x.GetData()).Returns <IEnumerable <DataStruct> >(null);

            //act
            Action actual = () => NumericSystems.Convert(dataManager.Object);

            //assert
            Assert.Throws <ArgumentNullException>(actual);
        }
Ejemplo n.º 10
0
        public static string Minus(string op_1, string op_2, NumericSystems NumSystem)
        {
            string sign = "";

            // if second operator biggen than first - change their place & make sign minus
            if (Compare(op_1, op_2) < 0)
            {
                var temp = op_1;
                op_1 = op_2;
                op_2 = temp;
                sign = "-";
            }

            // convert numbers into integer & fraction parts
            var(op_1_int_part, op_1_frac_part) = Formatter.SplitNumberByDot(op_1);
            var(op_2_int_part, op_2_frac_part) = Formatter.SplitNumberByDot(op_2);
            Formatter.DigitsAfterDotAlignment(ref op_1_frac_part, ref op_2_frac_part);

            // add zeros if number's length are not equal
            if (op_1_int_part.Length > op_2_int_part.Length)
            {
                var difference = op_1_int_part.Length - op_2_int_part.Length;

                for (int i = 0; i < difference; i++)
                {
                    op_2_int_part = op_2_int_part.Insert(0, "0");
                }
            }

            // find integer part
            var integer = Plus(op_1_int_part, GetAdditionalCode(op_2_int_part, NumSystem), NumSystem).Remove(0, 1);

            string fraction;

            // find out if 1 fraction more than another
            if (Compare(op_1_frac_part, op_2_frac_part) < 0)
            {
                fraction = GetAdditionalCode(
                    Plus(op_2_frac_part, GetAdditionalCode(op_1_frac_part, NumSystem), NumSystem)
                    .Remove(0, 1), NumSystem);

                integer = Minus(integer, "1", NumSystem);
            }
            else
            {
                fraction = Plus(op_1_frac_part, GetAdditionalCode(op_2_frac_part, NumSystem), NumSystem).Remove(0, 1);
            }

            return(sign + Formatter.GetFormat(integer, fraction));
        }
Ejemplo n.º 11
0
        public static string Plus(string op_1, string op_2, NumericSystems NumSystem)
        {
            // factor numbers into integer & fraction parts
            var(op_1_int_part, op_1_frac_part) = Formatter.SplitNumberByDot(op_1);
            var(op_2_int_part, op_2_frac_part) = Formatter.SplitNumberByDot(op_2);
            var dotIndex = Formatter.DigitsAfterDotAlignment(ref op_1_frac_part, ref op_2_frac_part);

            var operand_1 = op_1_int_part.Concat(op_1_frac_part).Reverse().ToArray();
            var operand_2 = op_2_int_part.Concat(op_2_frac_part).Reverse().ToArray();
            var result    = new List <char>();

            // create buffer
            int num_sys = (int)NumSystem;
            int buffer  = 0;

            // going in cycle for max count between 1 & 2 operators times
            for (int i = 0; i < operand_1.Length || i < operand_2.Length; i++)
            {
                // add value of next elements in every operator if it's not end
                if (i < operand_1.Length)
                {
                    buffer += NumberOf(operand_1[i]);
                }
                if (i < operand_2.Length)
                {
                    buffer += NumberOf(operand_2[i]);
                }

                // add to result remainder of the division
                result.Add(SymbolOf((byte)(buffer % num_sys)));

                // save to buffer integer of the division
                buffer /= num_sys;
            }

            // throw off buffer to result if it's not zero
            if (buffer != 0)
            {
                result.Add(SymbolOf((byte)buffer));
            }

            result.Insert(dotIndex, '.');
            result.Reverse();
            return(Formatter.GetFormat(string.Join("", result)));
        }
Ejemplo n.º 12
0
        private string ConvertExpressionFromTextBox(NumericSystems numericSystems)
        {
            List <string> tokenList = new List <string>();

            tokenList.Clear();
            tokenList = richTextBox1.Text.Split(' ').ToList();

            for (int i = 0; i < tokenList.Count; i++)
            {
                if (_tokens.IsNumber(tokenList[i]))
                {
                    int index = tokenList.IndexOf(tokenList[i]);
                    tokenList[index] = ConvertToGivenNumericSystem(tokenList[i], numericSystems);
                }
            }

            string result = string.Join(" ", tokenList);

            return(result);
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            try
            {
                var file = new FileDataManager();
                Console.WriteLine("File path (read data):");
                file.EntryFile = Console.ReadLine();

                Console.WriteLine("File path (write data result):");
                file.OutputFile = Console.ReadLine();

                NumericSystems.Convert(file);
                Console.Write("The process has finished");
            }
            catch (Exception ex)
            {
                Console.Write($"An error has occurred: {ex}");
            }
            finally
            {
                Console.ReadKey();
            }
        }
 public FloatingNumberConvertor(NumericSystems intitialNumericSystem, string number)
 {
     this.intitialNumericSystem = intitialNumericSystem;
     this.number = Formatter.RemoveExcessZeros(number);
 }
Ejemplo n.º 15
0
 public Validator(NumericSystems originalNumSystem, string number)
 {
     this.originalNumSystem = originalNumSystem;
     this.number            = Formatter.RemoveExcessZeros(number);
 }
Ejemplo n.º 16
0
        public static string Convert(NumericSystems intitialNumericSystem, NumericSystems otherNumSystem, string number)
        {
            Convertor convertor = new Convertor(intitialNumericSystem, number);

            return(convertor.ConvertToOtherSystem(otherNumSystem));
        }
Ejemplo n.º 17
0
 public Convertor(NumericSystems intitialNumericSystem, string number)
 {
     this.intitialNumericSystem = intitialNumericSystem;
     this.number = number;
 }
Ejemplo n.º 18
0
 public Calculator(NumericSystems NumericSystem, string operand_1, string operand_2)
 {
     this.NumericSystem = NumericSystem;
     this.operand_1     = Formatter.RemoveExcessZeros(operand_1);
     this.operand_2     = Formatter.RemoveExcessZeros(operand_2);
 }
Ejemplo n.º 19
0
        public static string Divide(string op_1, string op_2, NumericSystems NumSystem)
        {
            string result_ = "";

            var numbers_to_Displace = Formatter.DigitsAfterDotAlignment(op_1, op_2);

            op_1 = Displace(op_1, numbers_to_Displace);
            op_2 = Displace(op_2, numbers_to_Displace);

            int  integer_number_count = 1;
            bool is_first_time = true;
            int  now_int_number, last_int_number = 0, base_displace = 0;


            while (!Formatter.IsZero(op_1) && result_.Length - integer_number_count < 20)
            {
                // if second OP bigger than first
                while (Compare(op_1, op_2) < 0)
                {
                    op_1 = Displace(op_1, 1);
                    base_displace++;
                }


                // create dividing part - number from <second operand>, that can be maximum divided by <first operand>
                string dividing_part = op_2;
                now_int_number = 1 - base_displace;
                while (Compare(Displace(dividing_part, 1), op_1) < 0)
                {
                    dividing_part = Displace(dividing_part, 1);
                    now_int_number++;
                }

                if (is_first_time)
                {
                    integer_number_count = now_int_number;
                    is_first_time        = false;
                }
                else
                {
                    var discharge = last_int_number - now_int_number;
                    for (int i = 0; i < discharge - 1; i++)
                    {
                        result_ += '0';
                    }
                }
                last_int_number = now_int_number;


                // <first operand> minus <dividing part> (toResult) times
                var toResult = 0;

                while (Compare(op_1, dividing_part) >= 0)
                {
                    op_1 = Minus(op_1, dividing_part, NumSystem);
                    toResult++;
                }

                if (toResult >= (int)NumSystem)
                {
                    result_ += Convertor.Convert(NumericSystems.Decimal, NumSystem, toResult.ToString());
                    integer_number_count++;
                }
                else
                {
                    result_ += SymbolOf((byte)toResult);
                }
            }


            // displace result by <integer number count>
            return(Displace(result_, integer_number_count - result_.Length));
        }
 public static (string, MachineCode) Convert(string number, NumericSystems numericSystem)
 => new FloatingNumberConvertor(numericSystem, number).Convert();
 public static string GetAdditionalCode(string number, NumericSystems NumSystem)
 => Calculator.Plus(GetInvertedNumber(number, NumSystem), "1", NumSystem);