public static string GetTrigonometricFunctionText(TrigonometricFunctionType key) { if (!TrigonometricFunctionTexts.TryGetValue(key, out string result)) { throw new InvalidEnumArgumentException(nameof(key), (int)key, typeof(TrigonometricFunctionType)); } return(result); }
public TrigonometricFunctionCommandParameter(UserInteractionType userInteractionType, UserInteractionType inverseUserInteractionType, TrigonometricFunctionType trigonometricFunctionType, TrigonometricFunctionType inverseTrigonometricFunctionType) { UserInteractionType = userInteractionType; InverseUserInteractionType = inverseUserInteractionType; TrigonometricFunctionType = trigonometricFunctionType; InverseTrigonometricFunctionType = inverseTrigonometricFunctionType; TrigonometricFunctionText = Functions.GetTrigonometricFunctionText(trigonometricFunctionType); InverseTrigonometricFunctionText = Functions.GetTrigonometricFunctionText(inverseTrigonometricFunctionType); }
public double Calculate(Queue <string> queue, AngleUnitType angleUnitType) { Stack stack = new Stack(); while (queue.Any()) { string item = queue.Dequeue(); if (string.IsNullOrWhiteSpace(item)) { throw new NotAllowedElementInQueueException($"The following element does not allowed in the queue! Element value: {item}"); } if (item.IsDecimalNumber()) { stack.Push(item); } else if (Functions.IsTrigonometricFunction(item)) { TrigonometricFunctionType trigonometricFunctionType = Functions.GetTrigonometricFunctionTypeByText(item); CalculateWithTrigonometricFunction(stack, trigonometricFunctionType, angleUnitType); } else if (Functions.IsFunction(item)) { FunctionType functionType = Functions.GetFunctionTypeByText(item); CalculateWithFunction(stack, functionType); } else if (Operators.IsOperator(item)) { OperatorType operatorType = Operators.GetOperatorTypeByText(item); CalculateWithOperator(stack, operatorType); } else { throw new UnknownElementInQueueException($"The following element does not allowed in the queue! Element value: {item}"); } } if (stack.Count != 1) { throw new SyntaxErrorException("The stack contains more items than it should!"); } return(double.Parse(Convert.ToString(stack.Pop()))); }
public ITrigonometricFunction WithFunctionType(TrigonometricFunctionType type) { if (type == TrigonometricFunctionType.atan2) { _Ports = "[2 1]"; } else if (type == TrigonometricFunctionType.sincos) { _Ports = "[1 2]"; } else { _Ports = "[1 1]"; } _Operator = type; return(this); }
public TrigonometricFunction(TrigonometricFunctionType type) : base("TrigonometricFunctionExpressionTemplate") { this.type = type; switch (type) { case TrigonometricFunctionType.Sine: Value = "sin"; break; case TrigonometricFunctionType.Cosine: Value = "cos"; break; case TrigonometricFunctionType.Tangent: Value = "tg"; break; default: throw new NotSupportedException(); } }
private static void CalculateWithTrigonometricFunction(Stack stack, TrigonometricFunctionType trigonometricFunctionType, AngleUnitType angleUnitType) { double operand; switch (trigonometricFunctionType) { case TrigonometricFunctionType.Sin: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Sin(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Sin(operand * (Math.PI / 180))); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; case TrigonometricFunctionType.ArcSin: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Asin(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Asin(operand) * 180 / Math.PI); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; case TrigonometricFunctionType.Cos: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Cos(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Cos(operand * (Math.PI / 180))); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; case TrigonometricFunctionType.ArcCos: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Acos(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Acos(operand) * 180 / Math.PI); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; case TrigonometricFunctionType.Tan: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Tan(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Tan(operand * (Math.PI / 180))); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; case TrigonometricFunctionType.ArcTan: operand = double.Parse(Convert.ToString(stack.Pop())); switch (angleUnitType) { case AngleUnitType.Radian: stack.Push(Math.Atan(operand) * 1); break; case AngleUnitType.Degree: stack.Push(Math.Atan(operand) * 180 / Math.PI); break; default: throw new InvalidEnumArgumentException(nameof(angleUnitType), (int)angleUnitType, typeof(AngleUnitType)); } break; default: throw new InvalidEnumArgumentException(nameof(trigonometricFunctionType), (int)trigonometricFunctionType, typeof(TrigonometricFunctionType)); } }