public double CalculatePower(string input) { input = input.Trim(); if (!string.IsNullOrEmpty(input) && InputIsPower(input)) { string[] digitsStrings = input.Split('^'); string firstDigit = digitsStrings[0]; string secondDigit = digitsStrings[1]; double total = Math.Pow(double.Parse(firstDigit), double.Parse(secondDigit)); if (digitsStrings.Length > 2) { var digitList = new List<string>(digitsStrings); digitList.RemoveAt(0); digitList.RemoveAt(0); foreach (string digit in digitList) { double i = double.Parse(digit); total = Math.Pow(total, i); } } return total; } return 0; }
public double DivideNumbers(string input) { input = input.Trim(); if (!string.IsNullOrEmpty(input) && InputIsDivision(input)) { string[] digitsStrings = input.Split('/'); string firstDigit = digitsStrings[0]; string secondDigit = digitsStrings[1]; double total = double.Parse(firstDigit) / double.Parse(secondDigit); if (digitsStrings.Length > 2) { var digitList = new List<string>(digitsStrings); digitList.RemoveAt(0); digitList.RemoveAt(0); foreach (string digit in digitList) { double i = double.Parse(digit); total = total / i; } } return total; } return 0; }
public double SubtractNumbers(string input) { string trimmedInput = input.Replace(" ", ""); ; if (!string.IsNullOrEmpty(trimmedInput) && InputIsSubtraction(trimmedInput)) { string[] digitsStrings = input.Split('-'); string firstDigit = digitsStrings[0]; string secondDigit = digitsStrings[1]; double total = double.Parse(firstDigit) - double.Parse(secondDigit); if (digitsStrings.Length > 2) { var digitList = new List<string>(digitsStrings); digitList.RemoveAt(0); digitList.RemoveAt(0); foreach (string digit in digitList) { double i = double.Parse(digit); total = total - i; } } return total; } return 0; }
/// <summary> /// Finds the first instance of currentOperator (i.e. '+'), takes the operands on the left and right /// to the operator and builds an Expression instance (i.e. AddExpression in case of '+'). /// </summary> /// <param name="expression">A list containing numbers, operators and expressions.</param> /// <param name="currentOperator">The symbol of the current operator to be processed.</param> private static void CreateExpression(List<dynamic> expression, char currentOperator) { int operatorIndex = expression.IndexOf(currentOperator); Expression operation; dynamic leftOperand, rightOperand; try { if (expression.ElementAt(operatorIndex - 1) is double) { leftOperand = new NumberExpression(expression.ElementAt(operatorIndex - 1)); } else if (expression.ElementAt(operatorIndex - 1) is Expression) { leftOperand = expression.ElementAt(operatorIndex - 1); } else { throw new ArgumentException("Invalid expression string."); } if (expression.ElementAt(operatorIndex + 1) is double) { rightOperand = new NumberExpression(expression.ElementAt(operatorIndex + 1)); } else if (expression.ElementAt(operatorIndex + 1) is Expression) { rightOperand = expression.ElementAt(operatorIndex + 1); } else { throw new ArgumentException("Invalid expression string."); } } catch (Exception ex) { throw new ArgumentException("Invalid expression string."); } switch (currentOperator) { case '+': operation = new AddExpression(leftOperand, rightOperand); break; case '-': operation = new SubtractExpression(leftOperand, rightOperand); break; case '*': operation = new MultiplyExpression(leftOperand, rightOperand); break; case '/': operation = new DivideExpression(leftOperand, rightOperand); break; default: operation = new NumberExpression(0); break; } expression.RemoveAt(operatorIndex + 1); expression.RemoveAt(operatorIndex); expression.RemoveAt(operatorIndex - 1); expression.Insert(operatorIndex - 1, operation); }
private void sin(List<object> List) { double result; for (int i = 0; i < List.Count; i++) { if (List[i] is Operator && (Operator)List[i] == Operator.Sinus) { result = Math.Sin(((double)List[i + 1])*Math.PI / 180); List[i] = result; List.RemoveAt(i + 1); i--; } } }
private void multiply(List<object> List) { double result; for (int i = 0; i < List.Count; i++) { if (List[i] is double) continue; if (((Operator)List[i]) == Operator.Multiply && List[i - 1] is double && List[i + 1] is double) { result = (double)List[i - 1] * (double)List[i + 1]; List.RemoveAt(i + 1); List.RemoveAt(i); List[i - 1] = result; i -= 2; } } }
private void divide(List<object> List) { double result; for (int i = 0; i < List.Count; i++) { Console.WriteLine(List[i].ToString()); if (List[i] is double) continue; if (((Operator)List[i]) == Operator.Divide && List[i - 1] is double && List[i + 1] is double) { result = (double)List[i - 1] / (double)List[i + 1]; List.RemoveAt(i + 1); List.RemoveAt(i); List[i - 1] = result; i -= 2; } } }