/// <summary> /// Obtiene un valor predeterminado con un error /// </summary> public static ValueBase GetError(string error) { ValueString value = new ValueString("ERROR"); // Asigna el error value.Error = error; // Devuelve el valor return(value); }
/// <summary> /// Ejecuta una operación /// </summary> public override ValueBase Execute(ValueBase value, string operation) { ValueBase newValue = null; // Ejecuta la operación switch (operation) { case "+": if (value is ValueNumeric) { newValue = new ValueNumeric((value as ValueNumeric).Value + Value); } else { newValue = new ValueString((value as ValueString).Value + Content); } break; case "-": if (value is ValueNumeric) { newValue = new ValueNumeric((value as ValueNumeric).Value - Value); } break; case "/": if (value is ValueNumeric) { if (Value == 0) { newValue = ValueBase.GetError("No se puede dividir por cero"); } else { newValue = new ValueNumeric((value as ValueNumeric).Value / Value); } } break; case "*": if (value is ValueNumeric) { newValue = new ValueNumeric((value as ValueNumeric).Value * Value); } break; case ">=": case "<=": case "==": case "!=": case ">": case "<": if (value is ValueString) { newValue = new ValueString(Content).Execute(value as ValueString, operation); } else { double first = (value as ValueNumeric).Value; double second = Value; // Compara los números switch (operation) { case ">=": newValue = new ValueBool(first >= second); break; case "<=": newValue = new ValueBool(first <= second); break; case "==": newValue = new ValueBool(first == second); break; case "!=": newValue = new ValueBool(first != second); break; case ">": newValue = new ValueBool(first > second); break; case "<": newValue = new ValueBool(first < second); break; } } break; } // Crea el error if (newValue == null) { newValue = ValueBase.GetError($"No se puede utilizar el operador '{operation}' entre los valores {Content} y {value.Content}"); } // Devuelve el valor return(newValue); }
/// <summary> /// Ejecuta una operación /// </summary> public override ValueBase Execute(ValueBase value, string operation) { ValueBase newValue = null; // Ejecuta la operación switch (operation) { case "+": if (value is ValueNumeric) { newValue = new ValueString(value.Content + Value); } else { newValue = new ValueString((value as ValueString).Value + Value); } break; case ">=": case "<=": case "==": case "!=": case ">": case "<": int compare = Normalize(value.Content).CompareTo(Normalize(Content)); // Compara las cadenas switch (operation) { case ">=": newValue = new ValueBool(compare >= 0); break; case "<=": newValue = new ValueBool(compare <= 0); break; case "==": newValue = new ValueBool(compare == 0); break; case "!=": newValue = new ValueBool(compare != 0); break; case ">": newValue = new ValueBool(compare > 0); break; case "<": newValue = new ValueBool(compare < 0); break; } break; } // Crea el error if (newValue == null) { newValue = ValueBase.GetError($"No se puede utilizar el operador '{operation}' entre los valores {Content} y {value.Content}"); } // Devuelve el valor return(newValue); }