public void SubstactionTest(double first, double second, double expected) { ITwoArgumentsCalculator calculator = new Substraction(); double result = calculator.Calculate(first, second); Assert.AreEqual(expected, result); }
static void Main(string[] args) { Expression exp0 = new Number(-1); Console.WriteLine(exp0.Format()); Expression exp1 = new Addition(new Number(2), new Number(5)); Console.WriteLine(exp1.Format()); Expression exp2 = new Addition(new Number(3), new Addition(new Number(4), new Number(10))); Console.WriteLine(exp2.Format()); Expression exp3 = new Substraction(new Number(2), new Number(5)); Console.WriteLine(exp3.Format()); Expression exp4 = new Addition(new Substraction(new Number(13), new Number(3)), new Number(10)); Console.WriteLine(exp4.Format()); Expression exp5 = new Substraction(new Substraction(new Number(-1), new Number(-2)), new Substraction(new Number(-3), new Number(-4))); Console.WriteLine(exp5.Format()); Expression exp6 = new Substraction(new Addition(new Number(3), new Number(2)), new Addition(new Number(45), new Number(5))); Console.WriteLine(exp6.Format()); Console.WriteLine(new Addition(exp1, new Substraction(new Addition(exp2, exp3), new Substraction(exp4, exp5))).Format()); }
/// <summary> /// Método que invoca <see cref="SubAsync(Substraction)"/> de forma sincrona /// </summary> /// <param name="substraction">Objeto que contiene parametros de entrada</param> /// <returns>Retorna el resultado de la operación</returns> public Substraction Sub(Substraction substraction) { var result = default(Substraction); Task.Run(async() => result = await SubAsync(substraction)).Wait(); return(result); }
public static IMathOperation GetMathOperation(string operationKey) { IMathOperation operation; switch (operationKey) { case "+": operation = new Addition(); break; case "-": operation = new Substraction(); break; case "*": operation = new Multiplication(); break; case "/": operation = new Divition(); break; case "fib": operation = new GetFibonacci(); break; default: operation = null; break; } return(operation); }
public override Bitmap ProcessImage(Bitmap im, BackgroundWorker bw) { ClosingFilter close = new ClosingFilter(); Substraction sub = new Substraction(close.ProcessImage(im, bw)); return(sub.ProcessImage(im, bw)); }
public void CheckExpressions() { // Arrange Expression opr1 = new TerminalExpression(3.5); Expression opr2 = new TerminalExpression(2); Expression add = new Addition(opr1, opr2); Expression subs = new Substraction(opr1, opr2); Expression mult = new Multiplication(opr1, opr2); Expression div = new Division(opr1, opr2); Expression pow = new Power(opr1, opr2); Expression sqrt = new SquareRoot(opr1); Expression mod = new Modulus(opr1, opr2); // Act double addRes = add.solve(); double subsRes = subs.solve(); double multRes = mult.solve(); double divRes = div.solve(); double powRes = pow.solve(); double sqrtRes = sqrt.solve(); double modRes = mod.solve(); // Assert Assert.AreEqual(3.5 + 2, addRes); Assert.AreEqual(3.5 - 2, subsRes); Assert.AreEqual(3.5 * 2, multRes); Assert.AreEqual(3.5 / 2, divRes); Assert.AreEqual(Math.Pow(3.5, 2), powRes); Assert.AreEqual(Math.Pow(3.5, 0.5), sqrtRes); Assert.AreEqual(3.5 % 2, modRes); }
public void TestBuildFormula3() { AstBuilder builder = new AstBuilder(); Operation operation = builder.Build(new List <Token>() { new Token() { Value = 2, TokenType = TokenType.Integer }, new Token() { Value = '*', TokenType = TokenType.Operation }, new Token() { Value = 8, TokenType = TokenType.Integer }, new Token() { Value = '-', TokenType = TokenType.Operation }, new Token() { Value = 3, TokenType = TokenType.Integer } }); Substraction substraction = (Substraction)operation; Multiplication multiplication = (Multiplication)substraction.Argument1; Assert.AreEqual(3, ((Constant <int>)substraction.Argument2).Value); Assert.AreEqual(2, ((Constant <int>)multiplication.Argument1).Value); Assert.AreEqual(8, ((Constant <int>)multiplication.Argument2).Value); }
public void CalculateTest(double firstValue, double secondValue, double expected) { var calculator = new Substraction(); var actualResult = calculator.Calculate(firstValue, secondValue); Assert.AreEqual(expected, actualResult); }
public override Bitmap ProcessImage(Bitmap im, BackgroundWorker bw) { OpeningFilter op = new OpeningFilter(); Substraction sub = new Substraction(op.ProcessImage(im, bw)); return(sub.ProcessImage(im, bw)); }
public void TestCalculate() { var operation = new Substraction(); var answer = operation.Calculate(1.0, 2.0); Assert.AreEqual(-1.0, answer, 0.0001); }
public override Bitmap ProcessImage(Bitmap im, BackgroundWorker bw) { DilationFilter dilfil = new DilationFilter(); ErosionFilter erfil = new ErosionFilter(); Substraction sub = new Substraction(dilfil.ProcessImage(im, bw)); return(sub.ProcessImage(erfil.ProcessImage(im, bw), bw)); }
//----------------------------------------------------------- public Type Visit(Substraction node) { VisitBinaryNumericOperator("-", node); var firstType = Visit((dynamic)node[0]); var secondType = Visit((dynamic)node[1]); return((firstType == Type.REAL || secondType == Type.REAL) ? Type.REAL : Type.INTEGER); }
public void SubstractionTest() { double first = 2; double second = 2; var calc = new Substraction(); double result = calc.ExecuteOperation(first, second); Assert.AreEqual(0, result); }
public void Simple_Substraction() { const int expected = (6 - 2); var expression = new Substraction( new Number(6), new Number(2)); AssertExpression(expression, expected); }
private TreeViewItem CreateTreeViewItem(Operation operation) { TreeViewItem item = new TreeViewItem(); item.Header = GetLabelText(operation); if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; item.Items.Add(CreateTreeViewItem(multiplication.Argument1)); item.Items.Add(CreateTreeViewItem(multiplication.Argument2)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; item.Items.Add(CreateTreeViewItem(addition.Argument1)); item.Items.Add(CreateTreeViewItem(addition.Argument2)); } else if (operation.GetType() == typeof(Substraction)) { Substraction addition = (Substraction)operation; item.Items.Add(CreateTreeViewItem(addition.Argument1)); item.Items.Add(CreateTreeViewItem(addition.Argument2)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; item.Items.Add(CreateTreeViewItem(division.Dividend)); item.Items.Add(CreateTreeViewItem(division.Divisor)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; item.Items.Add(CreateTreeViewItem(exponentiation.Base)); item.Items.Add(CreateTreeViewItem(exponentiation.Exponent)); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; foreach (Operation argument in function.Arguments) { item.Items.Add(CreateTreeViewItem(argument)); } } return(item); }
private void GetVariables(Operation operation, List <Variable> variables) { if (operation.DependsOnVariables) { if (operation.GetType() == typeof(Variable)) { variables.Add((Variable)operation); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; GetVariables(addition.Argument1, variables); GetVariables(addition.Argument2, variables); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; GetVariables(multiplication.Argument1, variables); GetVariables(multiplication.Argument2, variables); } else if (operation.GetType() == typeof(Substraction)) { Substraction substraction = (Substraction)operation; GetVariables(substraction.Argument1, variables); GetVariables(substraction.Argument2, variables); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; GetVariables(division.Dividend, variables); GetVariables(division.Divisor, variables); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; GetVariables(exponentiation.Base, variables); GetVariables(exponentiation.Exponent, variables); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; foreach (Operation argument in function.Arguments) { GetVariables(argument, variables); } } } }
public void Complex_Expression() { const int expected = ((3 + 5 * 6 / 2) - 8); var expression = new Substraction( new Addition( new Number(3), new Multiplication( new Number(5), new Division( new Number(6), new Number(2)))), new Number(8)); AssertExpression(expression, expected); }
public T Operate(Operation operate, T left, T right) { switch (operate) { case Operation.Addition: return(Addition.Operate(left, right)); case Operation.Substraction: return(Substraction.Operate(left, right)); case Operation.Division: return(Division.Operate(left, right)); default: return(default); } }
public void Test_Complex_Expression() { const string expected = "( ( 3 + 5 * 2 / 6 ) - 8 )"; var expression = new Substraction( new Addition( new Number(3), new Multiplication( new Number(5), new Division( new Number(2), new Number(6)))), new Number(8)); AssertExpression(expression, expected); }
public Substraction Sub(Substraction substraction) { var businessLayer = new Operations(HeaderValue); try { var result = businessLayer.Subtraction(substraction.Minuend, substraction.Subtrahend); return(result); } catch (InvalidOperationException ioex) { throw HandledError(HttpStatusCode.BadRequest, ioex.Message); } catch { throw HandledError(HttpStatusCode.InternalServerError, "Has occurred a error, please try again or contact your administrator"); } }
public Operation Optimize(Operation operation) { if (!operation.DependsOnVariables && operation.GetType() != typeof(IntegerConstant) && operation.GetType() != typeof(FloatingPointConstant)) { double result = executor.Execute(operation); return(new FloatingPointConstant(result)); } else { if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; addition.Argument1 = Optimize(addition.Argument1); addition.Argument2 = Optimize(addition.Argument2); } else if (operation.GetType() == typeof(Substraction)) { Substraction substraction = (Substraction)operation; substraction.Argument1 = Optimize(substraction.Argument1); substraction.Argument2 = Optimize(substraction.Argument2); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; multiplication.Argument1 = Optimize(multiplication.Argument1); multiplication.Argument2 = Optimize(multiplication.Argument2); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; division.Dividend = Optimize(division.Dividend); division.Divisor = Optimize(division.Divisor); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation division = (Exponentiation)operation; division.Base = Optimize(division.Base); division.Exponent = Optimize(division.Exponent); } return(operation); } }
public static void Visitor() { //"( ( 3 + 5 * 2 / 6 ) - 8 )" var expression = new Substraction( new Addition( new Number(3), new Multiplication( new Number(5), new Division( new Number(2), new Number(6)))), new Number(8)); var visitor = new ToTextVisitor(); expression.Accpet(visitor); string expressionText = visitor.GetResult(); Console.WriteLine(expressionText); }
public static NDArray <T> operator -(NDArray <T> np1, NDArray <T> np2) { dynamic sub = null; dynamic np1Dyn = np1.Data.ToArray(); dynamic np2Dyn = np2.Data.ToArray(); var dataType = typeof(T); switch (dataType.Name) { case ("Double"): sub = new NDArray <double>().Array(((double[])Substraction.SubDoubleArrayFromDoubleArray(np1Dyn, np2Dyn)).ToList()); break; case ("Float"): sub = new NDArray <float>().Array(((float[])Substraction.SubfloatArrayFromfloatArray(np1Dyn, np2Dyn)).ToList()); break; case ("Complex"): sub = new NDArray <Complex>().Array(((Complex[])Substraction.SubComplexArrayFromComplexArray(np1Dyn, np2Dyn)).ToList()); break; case ("Quaternion"): sub = new NDArray <Quaternion>().Array(((Quaternion[])Substraction.SubQuaternionArrayFromQuaternionArray(np1Dyn, np2Dyn)).ToList()); break; } return((NDArray <T>)sub); }
static void Sub(Proxy proxy) { Console.Write("Enter the minuend number and separated by coma the subtrahend number: "); var values = Console.ReadLine(); var numbers = values.Split(',').Select(v => { if (!int.TryParse(v, out var output)) { return(default(int?)); } else { return(output); } }).ToArray(); var subtraction = new Substraction { Minuend = numbers.ElementAtOrDefault(0), Subtrahend = numbers.ElementAtOrDefault(1) }; var result = proxy.Sub(subtraction); Console.WriteLine($"The result operation is: {result.ToString()}"); }
public void TestBuildFormula3() { IFunctionRegistry registry = new MockFunctionRegistry(); AstBuilder builder = new AstBuilder(registry); Operation operation = builder.Build(new List <Token>() { new Token() { Value = 2.0f, TokenType = TokenType.FloatingPoint }, new Token() { Value = '*', TokenType = TokenType.Operation }, new Token() { Value = 8.0f, TokenType = TokenType.FloatingPoint }, new Token() { Value = '-', TokenType = TokenType.Operation }, new Token() { Value = 3.0f, TokenType = TokenType.FloatingPoint } }); Substraction substraction = (Substraction)operation; Multiplication multiplication = (Multiplication)substraction.Argument1; Assert.AreEqual(3, ((FloatingPointConstant)substraction.Argument2).Value); Assert.AreEqual(2, ((FloatingPointConstant)multiplication.Argument1).Value); Assert.AreEqual(8, ((FloatingPointConstant)multiplication.Argument2).Value); }
public BigInteger Counting() { //BigInteger result = 0; //Результат Stack <BigInteger> temp = new Stack <BigInteger>(); //Временный стек для решения BigInteger firstNumber = 0, secondNumber; StringBuilder a; Expression expression;// = new Addition(); short Length = (short)RPN.Length; char RPNI; for (int i = 0; i < Length; i++) //Для каждого символа в строке { RPNI = RPN[i]; //Если символ - цифра, то читаем все число и записываем на вершину стека if (Char.IsDigit(RPNI)) { a = new StringBuilder(Length); while (RPNI != ' ' && !IsOperator(RPNI) && RPNI != unaryMinus) //Пока не разделитель { a.Append(RPNI); //Добавляем i++; RPNI = RPN[i]; if (i == Length) { break; } } temp.Push(BigInteger.Parse(a.ToString())); //Записываем в стек i--; RPNI = RPN[i]; a = null; } else if (IsOperator(RPNI) || RPNI == unaryMinus) //Если символ - оператор { //Берем два последних значения из стека firstNumber = temp.Pop(); if (temp.Count == 0 || RPNI == unaryMinus) { secondNumber = 0; } else { secondNumber = temp.Pop(); } switch (RPNI) //И производим над ними действие, согласно оператору { case '+': #if Inheritance //lock (expression) { expression = new Addition(secondNumber, firstNumber); firstNumber = expression.Operator(); } #else firstNumber = firstNumber + secondNumber; #endif break; case '-': #if Inheritance //lock (expression) { expression = new Substraction(secondNumber, firstNumber); firstNumber = expression.Operator(); } #else firstNumber = -firstNumber; #endif break; case unaryMinus: #if Inheritance //lock (expression) { expression = new UnaryNegative(secondNumber, firstNumber); firstNumber = expression.Operator(); } #else firstNumber = secondNumber - firstNumber; #endif break; case '*': #if Inheritance //lock (expression) { expression = new Multiplication(secondNumber, firstNumber); firstNumber = expression.Operator(); } #else firstNumber = firstNumber * secondNumber; #endif break; case '/': #if Inheritance //lock (expression) { expression = new Division(secondNumber, firstNumber); firstNumber = expression.Operator(); } #else firstNumber = secondNumber / firstNumber; #endif break; } temp.Push(firstNumber); //Результат вычисления записываем обратно в стек } } temp = null; expression = null; return(firstNumber);//temp.Peek(); //Забираем результат всех вычислений из стека и возвращаем его }
/// <summary> /// Método que invoca el servicio calculator/sub - Ideal para móviles /// </summary> /// <param name="substraction">Objeto que contiene parametros de entrada</param> /// <returns>Retorna una tarea con el resultado de la operación</returns> public async Task <Substraction> SubAsync(Substraction substraction) { return(await SendPost <Substraction, Substraction>("calculator/sub", substraction)); }
public double SubtractionTest(double a, double b) { var operation = new Substraction(); return(operation.MathOperation(a, b)); }
private void GenerateMethodBody(ILGenerator generator, Operation operation) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; generator.Emit(OpCodes.Ldc_I4, constant.Value); generator.Emit(OpCodes.Conv_R8); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; generator.Emit(OpCodes.Ldc_R8, constant.Value); } else if (operation.GetType() == typeof(Variable)) { Type dictionaryType = typeof(Dictionary <string, double>); Variable variable = (Variable)operation; Label throwExceptionLabel = generator.DefineLabel(); Label returnLabel = generator.DefineLabel(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, variable.Name); generator.Emit(OpCodes.Callvirt, dictionaryType.GetMethod("ContainsKey", new Type[] { typeof(string) })); generator.Emit(OpCodes.Ldc_I4_0); generator.Emit(OpCodes.Ceq); generator.Emit(OpCodes.Brtrue_S, throwExceptionLabel); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, variable.Name); generator.Emit(OpCodes.Callvirt, dictionaryType.GetMethod("get_Item", new Type[] { typeof(string) })); generator.Emit(OpCodes.Br_S, returnLabel); generator.MarkLabel(throwExceptionLabel); generator.Emit(OpCodes.Ldstr, string.Format("The variable \"{0}\" used is not defined.", variable.Name)); generator.Emit(OpCodes.Newobj, typeof(VariableNotDefinedException).GetConstructor(new Type[] { typeof(string) })); generator.Emit(OpCodes.Throw); generator.MarkLabel(returnLabel); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; GenerateMethodBody(generator, multiplication.Argument1); GenerateMethodBody(generator, multiplication.Argument2); generator.Emit(OpCodes.Mul); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; GenerateMethodBody(generator, addition.Argument1); GenerateMethodBody(generator, addition.Argument2); generator.Emit(OpCodes.Add); } else if (operation.GetType() == typeof(Substraction)) { Substraction addition = (Substraction)operation; GenerateMethodBody(generator, addition.Argument1); GenerateMethodBody(generator, addition.Argument2); generator.Emit(OpCodes.Sub); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; GenerateMethodBody(generator, division.Dividend); GenerateMethodBody(generator, division.Divisor); generator.Emit(OpCodes.Div); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentation = (Exponentiation)operation; GenerateMethodBody(generator, exponentation.Base); GenerateMethodBody(generator, exponentation.Exponent); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Pow")); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; switch (function.FunctionType) { case FunctionType.Sine: GenerateMethodBody(generator, function.Arguments[0]); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Sin")); break; case FunctionType.Cosine: GenerateMethodBody(generator, function.Arguments[0]); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Cos")); break; case FunctionType.Loge: GenerateMethodBody(generator, function.Arguments[0]); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Log", new Type[] { typeof(double) })); break; case FunctionType.Log10: GenerateMethodBody(generator, function.Arguments[0]); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Log10")); break; case FunctionType.Logn: GenerateMethodBody(generator, function.Arguments[0]); GenerateMethodBody(generator, function.Arguments[1]); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Log", new Type[] { typeof(double), typeof(double) })); break; default: throw new ArgumentException(string.Format("Unsupported function \"{0}\".", function.FunctionType), "operation"); } } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
public void SubstractionTest() { var calculator = new Substraction(); Assert.AreEqual(2, calculator.Calculate(3, 1)); }