Пример #1
0
        public static MachineCode Multiply(MachineCode operand_1, MachineCode operand_2)
        {
            /*(operand_1, operand_2) = SetCommonDisplace(operand_1, operand_2);
             * MachineCode result_code = new MachineCode("0 10000000 0");
             * for (int i = 0; i < operand_2.binary_code.Length; i++)
             * {
             *  if (operand_2.binary_code[i])
             *  {
             *      result_code = Plus(result_code, DisplaceMachineCode(operand_1, i));
             *  }
             * }
             * return new MachineCode(operand_1.sign != operand_2.sign, result_code.displace, result_code.binary_code);*/

            var code_1     = FloatingNumberConvertor.BackConvert(operand_1);
            var code_2     = FloatingNumberConvertor.BackConvert(operand_2);
            var calculator = new Calculator(NumericSystems.Binary, code_1, code_2);

            var(a, b) = FloatingNumberConvertor.Convert(calculator.Solve(Operations.Multiply), NumericSystems.Binary);

            var disp = GetMatrixFromNumber(GetNumberFromBinary(operand_1.displace) + GetNumberFromBinary(operand_1.displace));

            return
                (new MachineCode(
                     operand_1.sign != operand_2.sign,
                     disp.Length <= 8 ? disp : GetAdditionalCode(disp),
                     b.binary_code));
        }
Пример #2
0
        public static MachineCode Minus(MachineCode operand_1, MachineCode operand_2)
        {
            /*if (operand_1.sign != operand_2.sign)
             *  return Plus(operand_1, new MachineCode(!operand_2.sign, operand_2.displace, operand_2.binary_code));
             * (operand_1, operand_2) = SetCommonDisplace(operand_1, operand_2);
             *
             * if (Comparator.CompareReverseBoolMatrix(operand_1.binary_code, operand_2.binary_code) < 0)
             * {
             *  var temporary = new MachineCode(!operand_1.sign, operand_1.displace, operand_1.binary_code);
             *  operand_1 = new MachineCode(!operand_2.sign, operand_2.displace, operand_2.binary_code);
             *  operand_2 = temporary;
             * }
             * (operand_1, operand_2) = SetCommonLength(operand_1, new MachineCode(operand_2.sign, operand_2.displace, GetAdditionalCode(operand_2.binary_code)));
             * var result = Plus(operand_1, operand_2);
             * return new MachineCode(result.sign, result.displace,
             *  result.binary_code.Take(result.binary_code.Length - 1).ToArray());*/


            if (operand_1.sign != operand_2.sign)
            {
                return(Plus(operand_1, new MachineCode(!operand_2.sign, operand_2.displace, operand_2.binary_code)));
            }

            var code_1     = FloatingNumberConvertor.BackConvert(operand_1);
            var code_2     = FloatingNumberConvertor.BackConvert(operand_2);
            var calculator = new Calculator(NumericSystems.Binary, code_1, code_2);

            var(a, b) = FloatingNumberConvertor.Convert(calculator.Solve(Operations.Minus), NumericSystems.Binary);
            return
                (new MachineCode(
                     b.sign,
                     b.displace,
                     b.binary_code));
        }
Пример #3
0
        public static MachineCode Solve(MachineCode operand_1, MachineCode operand_2, Operations operation)
        {
            switch (operation)
            {
            case Operations.Plus: return(Plus(operand_1, operand_2));

            case Operations.Minus: return(Minus(operand_1, operand_2));

            case Operations.Multiply: return(Multiply(operand_1, operand_2));

            case Operations.Divide: return(Divide(operand_1, operand_2));

            default: return(null);
            }
        }
Пример #4
0
        public static MachineCode Divide(MachineCode operand_1, MachineCode operand_2)
        {
            var code_1     = FloatingNumberConvertor.BackConvert(operand_1);
            var code_2     = FloatingNumberConvertor.BackConvert(operand_2);
            var calculator = new Calculator(NumericSystems.Binary, code_1, code_2);

            var(a, b) = FloatingNumberConvertor.Convert(calculator.Solve(Operations.Divide), NumericSystems.Binary);


            return
                (new MachineCode(
                     operand_1.sign != operand_2.sign,
                     b.displace,
                     b.binary_code));
        }
        public static MachineCode DisplaceMachineCode(MachineCode code, int count)
        {
            if (count < 0)
            {
                throw new IndexOutOfRangeException();
            }

            var binary_code = (new bool[count]).Concat(code.binary_code).ToArray();

            var displace =
                Calculator.Plus(string.Join("", GetStringNumberFromMatrix(code.displace).Reverse()),
                                GetStringNumberFromMatrix(GetMatrixFromNumber(count)), NumericSystems.Binary);

            return(new MachineCode(code.sign, GetBoolMatrix(displace), binary_code));
        }
        public static (MachineCode, MachineCode) SetCommonLength(MachineCode operand_1, MachineCode operand_2)
        {
            if (operand_1.binary_code.Length - operand_2.binary_code.Length != 0)
            {
                if (operand_1.binary_code.Length > operand_2.binary_code.Length)
                {
                    operand_2 = new MachineCode(operand_2.sign, operand_2.displace,
                                                operand_2.binary_code.Concat(new bool[operand_1.binary_code.Length - operand_2.binary_code.Length]).ToArray());
                }
                else
                {
                    operand_1 = new MachineCode(operand_1.sign, operand_1.displace,
                                                operand_1.binary_code.Concat(new bool[operand_2.binary_code.Length - operand_1.binary_code.Length]).ToArray());
                }
            }

            return(operand_1, operand_2);
        }
        public static (MachineCode, MachineCode) SetCommonDisplace(MachineCode op_1, MachineCode op_2)
        {
            int dislace_1_numb = GetNumberFromBinary(op_1.displace);
            int dislace_2_numb = GetNumberFromBinary(op_2.displace);

            int after_dot_numb_1 = op_1.binary_code.Length - dislace_1_numb;
            int after_dot_numb_2 = op_2.binary_code.Length - dislace_2_numb;
            var delta_dot        = Math.Abs(after_dot_numb_1 - after_dot_numb_2);

            if (after_dot_numb_1 != after_dot_numb_2)
            {
                if (after_dot_numb_1 < after_dot_numb_2)
                {
                    op_1 = DisplaceMachineCode(op_1, delta_dot);
                }
                else
                {
                    op_2 = DisplaceMachineCode(op_2, delta_dot);
                }
            }

            return(op_1, op_2);
        }
Пример #8
0
        public static MachineCode Plus(MachineCode operand_1, MachineCode operand_2)
        {
            if (operand_1.sign != operand_2.sign)
            {
                return(Minus(operand_1, new MachineCode(!operand_2.sign, operand_2.displace, operand_2.binary_code)));
            }

            (operand_1, operand_2) = SetCommonDisplace(operand_1, operand_2);


            var list_result = new List <bool>();
            var buffer      = false;

            for (int i = 0; i < operand_1.binary_code.Length || i < operand_2.binary_code.Length; i++)
            {
                if (i >= operand_1.binary_code.Length)
                {
                    if (operand_2.binary_code[i])
                    {
                        list_result.Add(!buffer);
                    }
                    else
                    {
                        list_result.Add(buffer);
                        buffer = false;
                    }
                }
                else
                if (i >= operand_2.binary_code.Length)
                {
                    if (operand_1.binary_code[i])
                    {
                        list_result.Add(!buffer);
                    }
                    else
                    {
                        list_result.Add(buffer);
                        buffer = false;
                    }
                }
                else
                {
                    if (operand_1.binary_code[i] == operand_2.binary_code[i])
                    {
                        list_result.Add(buffer);
                        buffer = operand_1.binary_code[i] && operand_2.binary_code[i];
                    }
                    else
                    {
                        list_result.Add(!buffer);
                    }
                }
            }

            if (buffer)
            {
                list_result.Add(buffer);
            }

            return(new MachineCode(operand_1.sign, operand_1.displace, list_result.ToArray()));
        }
 public static int GetNumbersAfterDot(MachineCode code)
 => code.binary_code.Length - GetNumberFromBinary(code.displace) - 1;