private static ExpressionItemNode BuildList(char[] arr, int j) { ExpressionItemNode curr = null; for (int i = 0; i <= j; i++) { var node = new ExpressionItemNode { prev = curr, next = null, val = char.IsDigit(arr[i]) ? (int)char.GetNumericValue(arr[i]) : (long?)null, operand = !char.IsDigit(arr[i]) ? arr[i] : (char?)null }; if (curr != null) { curr.next = node; } curr = node; } // move to head while (curr.prev != null) { curr = curr.prev; } return(curr); }
public ExpressionItemTransform(int offset, int length, ExpressionItemNode target, ExpressionNode transform, ExpressionNode separator) : base(offset, length) { Target = target; target.SetParent(this); Transform = transform; transform?.SetParent(this); Separator = separator; separator?.SetParent(this); }
public ExpressionItemFunctionInvocation(int offset, int length, ExpressionItemNode target, ExpressionFunctionName methodName, ExpressionArgumentList arguments) : base(offset, length) { Target = target; target.SetParent(this); Function = methodName; methodName?.SetParent(this); Arguments = arguments; arguments?.SetParent(this); }
private static long Evaluate(char[] arr, int j) { ExpressionItemNode head = BuildList(arr, j); char[] operands = new char[] { '"', '*', '+' }; ExpressionItemNode curr = head; for (int i = 0; i < operands.Length; i++) { curr = head; while (curr != null) { if (curr.operand.HasValue && curr.operand == operands[i]) { // merge the previous and the next nodes var prev = curr.prev; var next = curr.next; long finalValue = 0; if (operands[i] == '"') { string concated = $"{prev.val}{next.val}"; finalValue = long.Parse(concated); } else if (operands[i] == '*') { finalValue = prev.val.Value * next.val.Value; } else { finalValue = prev.val.Value + next.val.Value; } // delete the curr and next nodes prev.val = finalValue; prev.next = next.next; if (next.next != null) { next.next.prev = prev; } // reset the curr node curr = prev; } curr = curr.next; } } return(head.val.Value); }