public IActionResult Index(BinaryCalculator model) { SetViewBagValues(); if (!ModelState.IsValid) { return(View()); } String selectedValue = model.SelectedOperation; switch (selectedValue) { case "Add": model.ResultInBinary = model.CalculateAdditionResultInBinary(); model.ResultInDecimal = model.CalculateAdditionResultInDecimal(); break; case "Subtract": model.ResultInBinary = model.CalculateSubtractionResultInBinary(); model.ResultInDecimal = model.CalculateSubtractionResultInDecimal(); break; case "Multiply": model.ResultInBinary = model.CalculateMultiplicationResultInBinary(); model.ResultInDecimal = model.CalculateMultiplicationResultInDecimal(); break; case "Divide": model.ResultInBinary = model.CalculateDivisionResultInBinary(); model.ResultInDecimal = model.CalculateDivisionResultInDecimal(); break; } return(View(model)); }
private static void EvaluateInput(string input,out string output) { input = input.Trim(); // // Use Regex to identify the input type and split into tokens. // var intOpRegex = @"^\d+\s*(\+|\-|\*|\/)\s*\d+$"; var binOpRegex = @"^b[01]+\s*(\+|\-|\*|\/)\s*b[01]+$"; var strOpRegex = @"^[A-Za-z0-9]+\s*(\+|\-|\*|\/)\s*[A-Za-z0-9]+$"; string[] tokens = Regex.Split(input, @"(\+|\-|\*|\/)"); if (Regex.IsMatch(input, intOpRegex)) { ICalculator<int> intCalc = new IntegerCalculator(); output = intCalc.eval(tokens[0], tokens[1], tokens[2].TrimStart()).ToString(); } else if (Regex.IsMatch(input, binOpRegex)) { ICalculator<Binary> binCalc = new BinaryCalculator(); output = binCalc.eval(tokens[0].Substring(1, tokens[0].Length - 1).TrimEnd(), tokens[1], tokens[2].TrimStart().Substring(1)).ToString(); } else if(Regex.IsMatch(input,strOpRegex)) { ICalculator<string> strCalc = new StringCalculator(); output = strCalc.eval(tokens[0].TrimEnd(), tokens[1], tokens[2].TrimStart()); } else { output = "Operation cannot be performed."; } }
private static void EvaluateInput(string input, out string output) { input = input.Trim(); // // Use Regex to identify the input type and split into tokens. // var intOpRegex = @"^\d+\s*(\+|\-|\*|\/)\s*\d+$"; var binOpRegex = @"^b[01]+\s*(\+|\-|\*|\/)\s*b[01]+$"; var strOpRegex = @"^[A-Za-z0-9]+\s*(\+|\-|\*|\/)\s*[A-Za-z0-9]+$"; string[] tokens = Regex.Split(input, @"(\+|\-|\*|\/)"); if (Regex.IsMatch(input, intOpRegex)) { ICalculator <int> intCalc = new IntegerCalculator(); output = intCalc.eval(tokens[0], tokens[1], tokens[2].TrimStart()).ToString(); } else if (Regex.IsMatch(input, binOpRegex)) { ICalculator <Binary> binCalc = new BinaryCalculator(); output = binCalc.eval(tokens[0].Substring(1, tokens[0].Length - 1).TrimEnd(), tokens[1], tokens[2].TrimStart().Substring(1)).ToString(); } else if (Regex.IsMatch(input, strOpRegex)) { ICalculator <string> strCalc = new StringCalculator(); output = strCalc.eval(tokens[0].TrimEnd(), tokens[1], tokens[2].TrimStart()); } else { output = "Operation cannot be performed."; } }
public ConnectionMatrix ToConnectionMatrix() { var conMatrix = new ConnectionMatrix(); var neuronsCount = _neurons.Count; conMatrix.ActiFunctions = new string[neuronsCount]; conMatrix.ConnectionPresence = new bool[neuronsCount * neuronsCount]; conMatrix.WeightsBinary = new int[neuronsCount * ConnectionMatrix.WeightBitsPrecision * neuronsCount]; var n = 0; foreach (var neuron in _neurons) { conMatrix.ActiFunctions[n] = neuron.ActivationFunction.Name; n++; } foreach (var connection in _connections) { var row = _neurons.IndexOf(connection.InputNeuron); var col = _neurons.IndexOf(connection.OutputNeuron); // update connection presence conMatrix.ConnectionPresence[row * neuronsCount + col] = true; // update connection weight var weightBinary = BinaryCalculator.ToBinary(connection.Weight, ConnectionMatrix.WeightBitsPrecision, ConnectionMatrix.MinWeight, ConnectionMatrix.MaxWeight); for (var i = 0; i < weightBinary.Length; ++i) { conMatrix.WeightsBinary[row * neuronsCount * ConnectionMatrix.WeightBitsPrecision + col * ConnectionMatrix.WeightBitsPrecision + i] = weightBinary[i]; } } return(conMatrix); }
public Network(ConnectionMatrix connectionMatrix) { var neuronsCount = connectionMatrix.ActiFunctions.Length; foreach (var actiFunction in connectionMatrix.ActiFunctions) { var function = ActiFunctionsCollection.ActivationFunctionsArray.Single(x => x.Name == actiFunction); AddNeuron(new Neuron { ActivationFunction = function }); } var buffer = new int[ConnectionMatrix.WeightBitsPrecision]; for (int row = 0; row < neuronsCount; row++) { for (int column = 0; column < neuronsCount; column++) { if (connectionMatrix.ConnectionPresence[row * neuronsCount + column]) { // get weight from binary Array.Copy(connectionMatrix.WeightsBinary, row * neuronsCount * ConnectionMatrix.WeightBitsPrecision + column * ConnectionMatrix.WeightBitsPrecision, buffer, 0, ConnectionMatrix.WeightBitsPrecision); var weight = BinaryCalculator.ToDecimal(buffer, ConnectionMatrix.MinWeight, ConnectionMatrix.MaxWeight); // setup connection AddConnection(new Connection(_neurons[row], _neurons[column], weight)); } } } }
public void BinaryToDecimalTest(int[] binary, double min, double max, double expectedResult) { // arrange // act var output = BinaryCalculator.ToDecimal(binary, min, max); // assert Assert.True(Math.Abs(expectedResult - output) < 0.1); }
public void DecimalToBinaryTest(double toConvert, int precision, double min, double max, int[] expectedResult) { // arrange // act var output = BinaryCalculator.ToBinary(toConvert, precision, min, max); // assert Assert.Equal(expectedResult.Length, output.Length); Assert.Equal(expectedResult, output); }
public CalculatorAdapter() { _binaryCalculator = new BinaryCalculator(); }
private void OperatorClicked(string operation) { BinaryCalculator.ExecuteOperation(operation); RaisePropertyChanged("BinaryCalculator"); }
public void ReturnAnInteger() { var result = BinaryCalculator.CalculateBinaryGap(9); result.Should().BeOfType(typeof(int), "", typeof(int)); }
public void ExceptionTest(string firstValue) { var calculator = new BinaryCalculator(); Assert.Throws <FormatException>(() => calculator.Calculate(firstValue)); }
public void PositiveNumberTest() { BinaryCalculator calculator = new BinaryCalculator(); Assert.AreEqual("111", calculator.Calculate("7")); }
public void NegativeNumberTest() { BinaryCalculator calculator = new BinaryCalculator(); Assert.AreEqual("11111111111111111111111111111001", calculator.Calculate("-7")); }
private void ClickedOnClear(string ClearOption) { BinaryCalculator.Clear(ClearOption); RaisePropertyChanged("BinaryCalculator"); }
private void DigitClicked(string bit) { BinaryCalculator.GetBitIntoByteField(bit); RaisePropertyChanged("BinaryCalculator"); }
[TestCase(561892, 3)] //10001001001011100100 public void ReturnMaxSequenceOfZerosForBinaryOfInteger(int i, int expectedBinaryGap) { var result = BinaryCalculator.CalculateBinaryGap(i); result.Should().Be(expectedBinaryGap); }
public BinaryCalculatorViewModel() { BinaryCalculator = new BinaryCalculator(); }