private void ClearButton_OnClick(object sender, RoutedEventArgs e) { TextBox.Text = ""; _numberA = null; _numberB = null; _operator = null; currentPart = EquationPart.Left; }
private void SubstractButton_OnClick(object sender, RoutedEventArgs e) { if (_numberA != null && _numberB != null) { _numberA = CalculateResult(_numberA, _numberB, _operator); } TextBox.Text = "-"; _operator = '-'; currentPart = EquationPart.Right; }
List <EquationPart> ExtractPartsFromInput(string input) { var parts = new List <EquationPart>(); var startIndex = 0; var currentIndex = 0; var nestingLevel = 0; while (currentIndex < input.Length) { if (input[currentIndex] == ' ' && nestingLevel == 0) { if (startIndex == currentIndex) { currentIndex++; startIndex = currentIndex; continue; // after group } var strPart = input.Substring(startIndex, currentIndex - startIndex); parts.Add(CreatePartFromInput(strPart)); startIndex = currentIndex + 1; } if (input[currentIndex] == '(') { nestingLevel++; } if (input[currentIndex] == ')') { nestingLevel--; if (nestingLevel == 0) { var groupPart = new EquationPart() { Type = EquationPartType.Group, Group = input.Substring(startIndex + 1, currentIndex - startIndex - 1) }; parts.Add(groupPart); startIndex = currentIndex + 1; } } currentIndex++; } // if we didn't end in a group, then need to add entry for last value if (startIndex != currentIndex) { var strPart = input.Substring(startIndex, currentIndex - startIndex); parts.Add(CreatePartFromInput(strPart)); } return(parts); }
//private double CalcOffset(Vector<double> functionResult, Vector<double> coordY, int count) //{ // //////return (((y - y1) + 1) ^ 2) - 1 // ////Vector<double> diff = System.Numerics.Vector.Abs<double>(functionResult - coordY) + Vector<double>.One; // ////Vector<double> vectorOffset = (diff * diff) - Vector<double>.One; // ////if (count == Constants.VECTOR_LENGTH) // ////{ // //// double[] vectorOffsets = new double[Constants.VECTOR_LENGTH]; // //// vectorOffset.CopyTo(vectorOffsets); // this branch is probably not needed as Tools.GetPartOfVectorFunction could be used for both // //// return vectorOffsets.Sum(); // ////} // ////else // ////{ // //// return Tools.GetPartOfVectorResult(vectorOffset, count).Sum(); // ////} //} private Vector <double> GetFunctionResult(Vector <double> x) { Vector <double> Result = x; foreach (Operator EquationPart in EquationParts) { Result = EquationPart.Calculate(Result, x); if (!Tools.IsANumber(Result)) { return(Constants.NAN_VECTOR); } } return(Result); }
long SolveEquationPt1(string input) { var parts = ExtractPartsFromInput(input); long total = -99; EquationPart op = null; foreach (var part in parts) { long val = 0; if (part.Type == EquationPartType.Group) { val = SolveEquationPt1(part.Group); } else if (part.Type == EquationPartType.OpAdd || part.Type == EquationPartType.OpMult) { op = part; continue; } else { val = part.Number; } if (total == -99) { total = val; continue; } if (op.Type == EquationPartType.OpAdd) { total += val; } if (op.Type == EquationPartType.OpMult) { total *= val; } } return(total); }
public string CreateFunction() { StringBuilder Forwards = new StringBuilder(); StringBuilder Backwards = new StringBuilder(); const string Variable = "x"; Forwards.Append(Variable); foreach (Operator EquationPart in EquationParts) { EquationPart.ShowOperator(Variable, Forwards, Backwards); } StringBuilder Result = new StringBuilder(Backwards.Length + Forwards.Length); for (int i = Backwards.Length - 1; i >= 0; i--) { Result.Append(Backwards[i]); } Result.Append(Forwards.ToString()); return(Result.ToString()); }
long SolveEquationPt2(string input) { var parts = ExtractPartsFromInput(input); long total = -99; EquationPart op = null; var additionFound = false; do { additionFound = false; for (int i = 1; i < parts.Count; i += 2) { op = parts[i]; var left = parts[i - 1]; var right = parts[i + 1]; if (parts[i].Type == EquationPartType.OpAdd) { var leftVal = left.Type == EquationPartType.Number ? left.Number : SolveEquationPt2(left.Group); var rightVal = right.Type == EquationPartType.Number ? right.Number : SolveEquationPt2(right.Group); var sumPart = new EquationPart() { Type = EquationPartType.Number, Number = leftVal + rightVal }; var newParts = new List <EquationPart>(); if (i > 1) { newParts.AddRange(parts.GetRange(0, i - 1)); } newParts.Add(sumPart); if (i + 2 < parts.Count) { newParts.AddRange(parts.GetRange(i + 2, parts.Count - i - 2)); } parts = newParts; additionFound = true; break; } } } while (additionFound); foreach (var part in parts) { long val = 0; if (part.Type == EquationPartType.Group) { val = SolveEquationPt2(part.Group); } else if (part.Type == EquationPartType.OpAdd || part.Type == EquationPartType.OpMult) { op = part; continue; } else { val = part.Number; } if (total == -99) { total = val; continue; } if (op.Type == EquationPartType.OpAdd) { total += val; } if (op.Type == EquationPartType.OpMult) { total *= val; } } return(total); }