public void Run(string[] delimiters, bool allowNegative = true, int?upperBound = null, OperatorTypes mathOperator = OperatorTypes.Add) { try { if (delimiters?.Length > 0) { _parser.SetDelimiters(delimiters); } _parser.AllowNegative = allowNegative; _parser.UpperBound = upperBound; do { try { _parser.Reset(); Console.Write("Please enter numbers: "); int input; do { input = Console.Read(); char ch; try { ch = Convert.ToChar(input); } catch (OverflowException) { ch = 'a'; // causes entry to be 0 } _parser.Read(ch); }while (input != 13); Console.ReadLine(); // read the rest of the line - \n List <int> numbers = _parser.GetNumbers(); ICalculationResult result = _calculator.Calculate(numbers, mathOperator); PrintResult(result.Text); } catch (FormatException formatException) { PrintError("Failed to process input: " + formatException.Message); Console.ReadLine(); } catch (Exception exception) { PrintError("Failed to read input: " + exception.Message); Console.ReadLine(); } }while (true); } catch (Exception exception) { PrintError("Failed to run calculator: " + exception.Message); } }
public ConditionEvaluationResultBase(DateTime creationTime, ICondition condition, IReferenceValue referenceValue, bool conditionIsMet, ICalculationResult calculationResult) : base(creationTime, calculationResult.Successful) { Condition = condition; ReferenceValue = referenceValue; ConditionIsMet = conditionIsMet; CalculationResult = calculationResult; }
/// <summary> /// Automatic update function called by manager /// </summary> /// <param name="parser">Receiver reference object from the android system</param> internal void Update(AndroidJavaObject parser) { // This date is used for removing anchors that are not up-to-date DateTime now = DateTime.Now; // This loop is done as long as the device has packets while (parser.Call <bool>("hasPacket")) { // Getting the packet AndroidJavaObject packet = parser.Call <AndroidJavaObject>("popPacket"); // Taking the information int id = packet.Call <int>("getAnchorId"); int distance = packet.Call <int>("getDistanceInMillimeters"); // Anchor creation/get Anchor anchor; if (anchors.ContainsKey(id)) { anchor = anchors[id]; } else { anchor = new Anchor(this, id); anchors.Add(id, anchor); // On new anchor -> inform OnAnchorAppeared(anchor); } // Setting data anchor.Set(now, distance); locator.SetAnchorDistance(id, distance * .001f); // if superautoupdate => update if (UpdateLevel == UpdateLevel.OnValueUpdate) { CalculationResult = locator.Calculate(); } } // Remove all anchors that are invalid (haven't updated in a "long" time) HashSet <int> removables = new HashSet <int>(); foreach (int anchorId in anchors.Keys) { if ((now - anchors[anchorId].Timestamp) > Manager.DiscardInterval) { anchors[anchorId].Remove(); anchors.Remove(anchorId); locator.UnsetAnchorDistance(anchorId); } } // if autoupdate => update if (UpdateLevel == UpdateLevel.OnUpdate) { CalculationResult = locator.Calculate(); } }
public void DivisionArithmeticTest() { List <int> numbers = new List <int>(new int[3] { 60, 5, 2 }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Divide); Assert.AreEqual(6, result.Numeric); Assert.AreEqual("60/5/2 = 6", result.Text); }
public void LogCalculation(ICalculationResult result) { string calculation = JsonConvert.SerializeObject(result, Formatting.None); lock (_SyncLock) { string filePath = Path.Combine(Directory.GetCurrentDirectory(), "Logs", "CalculationHistory.txt"); File.AppendAllText(filePath, calculation + Environment.NewLine); } }
public void CanGetCalculationResultObj() { // Arrange double inputResult = 1; // Act ICalculationResult result = AppCoreFactory.CreateCalculatinoResult(inputResult); // Assert Assert.IsNotNull(result); Assert.AreEqual(result.Result, inputResult); }
private char[,] CloneData(char[,] boardData, ICalculationResult current) { char[,] cloneData = new char[boardData.GetLength(0), boardData.GetLength(1)]; for (int i = 0; i < boardData.GetLength(0); ++i) { for (int j = 0; j < boardData.GetLength(0); ++j) { cloneData[i, j] = boardData[i, j]; } } cloneData[current.RowIndex, current.ColumnIndex] = this.PlayerID; return(cloneData); }
public ICalculationResult GetCalculationResult( double leftInput, double rightInput, int logicCode ) { if (!IsInputsValidForProbabilityCalculation(leftInput, rightInput)) { throw new InvalidOperationException(c_InvalidInputErrMsg); } if (logicCode == default(int)) { throw new InvalidOperationException(nameof(logicCode)); } ICalculationResult result = null; IProbabilityLogic probabilityLogic = AppCoreFactory.CreateCalculationLogic(logicCode); if (probabilityLogic != null) { result = AppCoreFactory.CreateCalculatinoResult( // using Decorator Pattern probabilityLogic.GetCalculationResult(leftInput, rightInput) ); result.CalculationLogging = AppCoreFactory.GetCalculationLoggingObj( c_ResultLogginMessage, leftInput, rightInput, (int)logicCode, result.Result, // TODO : fix testability technical debt DateTime.Now ); _logger.LogInformation( $"{result.CalculationLogging.Message} " + $"timeStamp = {result.CalculationLogging.TimeStamp}, " + $"leftInput = {result.CalculationLogging.LeftInput}, " + $"rightInput = {result.CalculationLogging.RightInput}, " + $"calculationLogic = {result.CalculationLogging.LogicId}, " + $"result = {result.CalculationLogging.Result}" ); } return(result); }
public int GenerateMove(IGameEngine gameEngine, IPlayer otherPlayer) { char[,] boardData = gameEngine.BoardData; ICalculationResult result = CalculateResult(gameEngine, otherPlayer.PlayerID); if (result == null) { return(-1); } else { return(result.ColumnIndex); } }
private void Operate(ICalculationResult result, OperatorTypes mathOperator, int nextNumber) { string text = nextNumber < 0 ? "(" + nextNumber.ToString() + ")" : nextNumber.ToString(); if (result.Text == "") { result.Numeric = nextNumber; result.Text = text; return; } switch (mathOperator) { case OperatorTypes.Add: result.Numeric += nextNumber; if (!string.IsNullOrEmpty(result.Text)) { result.Text += "+"; } break; case OperatorTypes.Subtract: result.Numeric -= nextNumber; if (!string.IsNullOrEmpty(result.Text)) { result.Text += "-"; } break; case OperatorTypes.Multiply: result.Numeric *= nextNumber; if (!string.IsNullOrEmpty(result.Text)) { result.Text += "*"; } break; case OperatorTypes.Divide: result.Numeric /= nextNumber; if (!string.IsNullOrEmpty(result.Text)) { result.Text += "/"; } break; } result.Text += text; }
public void MultiplicationArithmeticTest() { List <int> numbers = new List <int>(new int[5] { 1, 2, 3, 4, 5 }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Multiply); Assert.AreEqual(120, result.Numeric); Assert.AreEqual("1*2*3*4*5 = 120", result.Text); numbers = new List <int>(new int[5] { -3, 10, -28, -25, 4 }); result = _calculator.Calculate(numbers, OperatorTypes.Multiply); Assert.AreEqual(-84000, result.Numeric); Assert.AreEqual("(-3)*10*(-28)*(-25)*4 = -84000", result.Text); }
public void AdditionBoundaryTest() { try { List <int> numbers = new List <int>(new int[2] { int.MaxValue, int.MaxValue }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Add); long sum = (long)int.MaxValue + (long)int.MaxValue; Assert.AreEqual(sum, result.Numeric); Assert.AreEqual(int.MaxValue.ToString() + "+" + int.MaxValue.ToString() + " = " + sum.ToString(), result.Text); } catch (Exception exception) { Assert.Fail("Large integer addition exception is unhandled. " + exception.Message); } }
public void AdditionArithmeticTest() { List <int> numbers = new List <int>(new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Add); Assert.AreEqual(55, result.Numeric); Assert.AreEqual("1+2+3+4+5+6+7+8+9+10 = 55", result.Text); numbers = new List <int>(new int[10] { -4, 0, 2, -5, -2, 7, 100, -13, 99, -200 }); result = _calculator.Calculate(numbers, OperatorTypes.Add); Assert.AreEqual(-16, result.Numeric); Assert.AreEqual("(-4)+0+2+(-5)+(-2)+7+100+(-13)+99+(-200) = -16", result.Text); }
public void MultiplicationBoundaryTest() { try { List <int> numbers = new List <int>(new int[2] { int.MaxValue, int.MaxValue }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Multiply); long product = (long)int.MaxValue * (long)int.MaxValue; Assert.AreEqual(product, result.Numeric); Assert.AreEqual(int.MaxValue.ToString() + "*" + int.MaxValue.ToString() + " = " + product.ToString(), result.Text); } catch (Exception exception) { Assert.Fail("Large integer multiplication exception is unhandled. " + exception.Message); } }
public void SubtractionArithmeticTest() { List <int> numbers = new List <int>(new int[4] { 55, 22, 11, 2 }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Subtract); Assert.AreEqual(20, result.Numeric); Assert.AreEqual("55-22-11-2 = 20", result.Text); numbers = new List <int>(new int[5] { 0, -10, 4, 99, -150 }); result = _calculator.Calculate(numbers, OperatorTypes.Subtract); Assert.AreEqual(57, result.Numeric); Assert.AreEqual("0-(-10)-4-99-(-150) = 57", result.Text); }
public void SubtractionBoundaryTest() { try { List <int> numbers = new List <int>(new int[2] { int.MinValue, int.MaxValue }); ICalculationResult result = _calculator.Calculate(numbers, OperatorTypes.Subtract); long difference = (long)int.MinValue - (long)int.MaxValue; Assert.AreEqual(difference, result.Numeric); Assert.AreEqual("(" + int.MinValue.ToString() + ")-" + int.MaxValue.ToString() + " = " + difference.ToString(), result.Text); } catch (Exception exception) { Assert.Fail("Small integer subtraction exception is unhandled. " + exception.Message); } }
protected bool IsOpponentNextMoveWinningMove(IGameEngine gameEngine, ICalculationResult current, char[,] boardData, char otherPlayer) { char[,] cloneData = CloneData(boardData, current); for (int i = 0; i < boardData.GetLength(1); ++i) { int rowIndex = GetRow(i, cloneData); if (rowIndex >= 0) { IMove move = new ConnectMove(new WebPlayer("Player 1", otherPlayer), 0, rowIndex, i); if (ConnectFourGameEngine.IsWinningMove(move, cloneData)) { return(true); } } } return(false); }
protected virtual ICalculationResult CalculateResult(IGameEngine gameEngine, char otherPlayerID) { char[,] boardData = gameEngine.BoardData; ICalculationResult max = null; for (int i = 0; i < boardData.GetLength(1); ++i) { int rowIndex = GetRow(i, boardData); if (rowIndex >= 0) { ICalculationResult current = CalculateColumnBlocks(rowIndex, i, boardData, otherPlayerID); if (max == null || current.CompareTo(max) == 1) { if (!IsOpponentNextMoveWinningMove(gameEngine, current, boardData, otherPlayerID)) { max = current; } } } } return(max); }
protected override ICalculationResult CalculateResult(IGameEngine gameEngine, char otherPlayerID) { ICalculationResult max = null; char[,] boardData = gameEngine.BoardData; for (int i = 0; i < boardData.GetLength(1); ++i) { int rowIndex = GetRow(i, boardData); if (rowIndex >= 0) { IMove move = new ConnectMove(this, 0, rowIndex, i); if (gameEngine.IsWinningMove(move)) { CalculationResult result = new CalculationResult(rowIndex, i, boardData.GetLength(1)); result.IsWinningMove = true; return(result); } } } return(base.CalculateResult(gameEngine, otherPlayerID)); }
// Update is called once per frame void Update() { // Update manager to actively keep track of data (Use this as often as you want, but remember: The data will not disappear until popped by update) manager.Update(); // Example string build for data StringBuilder builder = new StringBuilder(); foreach (Receiver receiver in manager.Receivers) { builder.AppendLine("Receiver \"" + receiver.Serial + "\""); // Get latest calculation result ICalculationResult result = receiver.CalculationResult; foreach (Anchor anchor in receiver.Anchors) { if (anchorPositions.ContainsKey(anchor.Id)) { builder.AppendLine("Anchor " + anchor.Id + " distance: " + anchor.Distance + " (position: " + VectorString(anchorPositions[anchor.Id]) + ")"); } else { builder.AppendLine("Anchor " + anchor.Id + " distance: " + anchor.Distance); } } builder.AppendLine("----- RESULT -----"); // how to handle the result: Check the type from enum field and then cast switch (result.ResultType) { case CalculationResultType.Null: builder.AppendLine("No result"); break; case CalculationResultType.Single: SinglePointCalculationResult singleResult = (SinglePointCalculationResult)result; builder.AppendLine("Single point"); builder.AppendLine("Position: " + VectorString(singleResult.Position)); builder.AppendLine("Radius: " + singleResult.Distance); break; case CalculationResultType.Double: DoublePointCalculationResult doubleResult = (DoublePointCalculationResult)result; builder.AppendLine("Circle result"); builder.AppendLine("Center position: " + VectorString(doubleResult.Position)); builder.AppendLine("Normal Direction: " + VectorString(doubleResult.Normal)); builder.AppendLine("Radius: " + doubleResult.Radius); break; case CalculationResultType.Trilinear: TrilinearCalculationResult triResult = (TrilinearCalculationResult)result; builder.AppendLine("Trilinear result"); if (triResult.CalculationErrored) { builder.AppendLine("ERROR: Trilinear calculation failed"); } else { builder.AppendLine("Result 1: " + VectorString(triResult.Result1)); builder.AppendLine("Result 2: " + VectorString(triResult.Result2)); } break; case CalculationResultType.Quadlinear: QuadlinearCalculationResult quadResult = (QuadlinearCalculationResult)result; builder.AppendLine("Quadlinear result"); builder.AppendLine("Calculated from " + quadResult.CalculationOptions + " points"); builder.AppendLine("Calculated with " + quadResult.CalculationLevel + " calculation level"); if (quadResult.Errored) { builder.AppendLine("ERROR: Quadlinear calculation failed"); } else { builder.AppendLine("Position: " + VectorString(quadResult.Position)); } break; } builder.AppendLine(); } // build to string dataString = builder.ToString(); }
public void LogCalculation(ICalculationResult result) { }
public SimpleConditionEvaluationResult(DateTime creationTime, ICondition condition, IReferenceValue referenceValue, bool conditionIsMet, ICalculationResult calculationResult) : base(creationTime, condition, referenceValue, conditionIsMet, calculationResult) { }
/// <summary> /// Calculates the current position /// Can always be used but best used when there is no /// </summary> public void Calculate() { CalculationResult = locator.Calculate(); }