/// <summary> /// Handles mathemathical operation. /// If l and r are both primitive values, the MathOp delegate is executed. /// /// TODO: Operator overloading. /// </summary> ISemantic HandleSingleMathOp(IExpression x, ISemantic l, ISemantic r, MathOp m) { if (l == null || r == null) { return(null); } var pl = l as PrimitiveValue; var pr = r as PrimitiveValue; //TODO: imaginary/complex parts if (pl != null && pr != null) { // If one if (pl.IsNaN || pr.IsNaN) { return(PrimitiveValue.CreateNaNValue(x, pl.IsNaN ? pl.BaseTypeToken : pr.BaseTypeToken)); } return(new PrimitiveValue(pl.BaseTypeToken, m(pl, pr), x)); } throw new NotImplementedException("Operator overloading not implemented yet."); }
public static void performOP() { MathOp squareOp = new MathOp(Square); squareOp(5); Console.WriteLine("\n Odd numbers"); //complex List <int> list = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List <int> oddNums = list.FindAll(delegate(int i) { return(i % 2 == 1); } ); foreach (int odd in oddNums) { Console.WriteLine(odd); } Console.WriteLine("\n Even numbers"); //simple List <int> evenNums = list.FindAll(j => j % 2 == 0); evenNums.ForEach(j => Console.WriteLine(j)); Console.WriteLine(); Compare comp = (k, n) => k == n.num; Console.WriteLine(comp(5, new Number { num = 6 })); }
static void Main(string[] args) { Stopwatch watch = new Stopwatch(); Stopwatch watch2 = new Stopwatch(); watch.Restart(); for (int i = 0; i < loopCount; i++) { Console.Write(DoOperation(Add, 1, 2)); } watch.Stop(); MathOp op = Add; watch2.Restart(); for (int i = 0; i < loopCount; i++) { Console.Write(DoOperation(op, 1, 2)); } watch2.Stop(); Console.WriteLine("Elapsed: {0}", watch.Elapsed); Console.WriteLine("Elapsed: {0}", watch2.Elapsed); Console.ReadKey(); }
static void Main(string[] args) { /* 1. A variable, declared outside the anonymous method can be accessed inside the anonymous method. * 2. A variable, declared inside the anonymous method can’t be accessed outside the anonymous method. */ int z = 3; MathOp d1 = delegate(int x, int y) { return(x * y * z); }; int z1 = d1(2, 3); Console.WriteLine("Result - {0}", z1); /*Anonymous is prefered over Tuple */ string FullName = "Naresh Kumar Penta"; //Tuple Tuple <string, string, string> tResult = ParseData(FullName); Console.WriteLine("First Name: {0}, Middle Name: {1}, Last Name: {2} ", tResult.Item1, tResult.Item2, tResult.Item3); //Anonymous var Result = Cast(ParseDataA(FullName), new { FirstName = "", MiddleName = "", LastName = "" }); Console.WriteLine("First Name: {0}, Middle Name: {1}, Last Name: {2} ", Result.FirstName, Result.MiddleName, Result.LastName); Console.ReadLine(); }
static void Main(string[] args) { ACalculator calc = new ACalculator(); int x = calc.Add(3, 4); // MathOp op1 = new MathOp(calc.Add); // AddressOf calc.Add MathOp op1 = calc.Add; //int result = op1(3, 2); op1 = calc.Subtract; // int result2 = op1(3, 2); IAsyncResult ar = op1.BeginInvoke(3, 2, CalcCompleted, op1); // int result3 = op1.EndInvoke(ar); //result = op1(3, 2); op1 += calc.Add; int result = op1(7, 2); Console.WriteLine(result); Func <int, int, int> op2 = calc.Add; result = op2(11, 3); Console.ReadLine(); }
public IActionResult Index([FromBody] MathOp mathOp) { int[] resultArray = new int[mathOp.Numbers.Length]; int intResult = 1; if (mathOp.Operation.Equals(Operation.sum)) { return(Json(new { result = mathOp.Numbers.Sum() })); } if (mathOp.Operation.Equals(Operation.multiplyAll)) { for (int i = 1; i < mathOp.Numbers.Length; i++) { intResult *= mathOp.Numbers[i]; } return(Json(new { result = intResult })); } if (mathOp.Operation.Equals(Operation.multiplyElementsByTwo)) { for (int i = 0; i < mathOp.Numbers.Length; i++) { resultArray[i] = mathOp.Numbers[i] * 2; } return(Json(new { result = resultArray })); } else { return(Json(new { error = "Please provide what to do with the numbers!" })); } }
/// <summary> /// Very simple local function /// </summary> static void BasicLocalFunction() { // Note that VS suggests to use a local function instead of // the lambda function. MathOp add = (x, y) => x + y; Console.WriteLine($"The result is {add(1, 2)}\n"); }
public void DoingMath(int a, int b, int expected) { MathOp op = (a, b) => a + b; var result = op(a, b); Assert.Equal(expected, result); }
static void CalcCompleted(IAsyncResult ar) { Console.WriteLine("CalcCompleted"); MathOp op1 = (MathOp)ar.AsyncState; int result = op1.EndInvoke(ar); Console.WriteLine($"result: {result}"); }
static void Main(string[] args) { Rectangle rec = new Rectangle(); MathOp op = rec.Area; op += rec.Perimeter; op(2, 4); }
public void Test1() { MathOp op = new MathOp(); //Arrange, create the input //Act, chew the data //Assert, result }
public static bool TryCalc(object a, object b, MathOp op, out object x) { x = null; try { if (a is int) { var i1 = Convert.ToInt32(a); var i2 = Convert.ToInt32(b); switch (op) { case MathOp.Add: x = i1 + i2; break; case MathOp.Sub: x = i1 - i2; break; case MathOp.Mul: x = i1 * i2; break; case MathOp.Div: x = i1 / i2; break; case MathOp.Xor: x = i1 ^ i2; break; case MathOp.Or: x = i1 | i2; break; case MathOp.And: x = i1 & i2; break; case MathOp.AndAnd: break; case MathOp.OrOr: break; } } } catch (InvalidCastException) { return(false); } return(true); }
void PerformSecondExpressionMember(string opType) { if (number == null || number == "-") { calculate = ChooseMathOperation(opType); number = SetDefaultArgumentForMathOperation(opType); } model.Argument2 = double.Parse(number); PerformAndShowResult(); }
void mainWindow_reset_clicked(object sender, EventArgs e) { calculate = null; mainWindow.display.Text = "0"; number = null; operationButtonIsBeanClickedBefore = false; mainWindow.NotCEButtons.IsEnabled = true; model.Argument1 = 0; model.Argument2 = 0; }
private void CallIt() { MathOp op = Add; double z = 0; // issue the call and return var ar = op.BeginInvoke( 3, 4, out z, ref _c, Completed, op); }
public void TestMethodDivide() { //Arrange MathOp Mo = new MathOp(); double Result; //Act Result = Mo.Divide(100, 10); //Assert Assert.AreEqual(10, Result); }
public IActionResult DoMathOp(MathOp mathOp) { if (ModelState.IsValid) { switch (mathOp.Operator) { case "Plus": mathOp.Result = mathOp.Left + mathOp.Right; break; case "Minus": mathOp.Result = mathOp.Left - mathOp.Right; break; case "Times": mathOp.Result = mathOp.Left * mathOp.Right; break; case "DivBy": if (mathOp.Right == 0) { return(View("Error")); } mathOp.Result = mathOp.Left / mathOp.Right; break; case "Modulus": mathOp.Result = mathOp.Left % mathOp.Right; break; default: MathOp op = new MathOp(); op.Left = mathOp.Left; op.Right = mathOp.Right; op.Operator = mathOp.Operator; op.Result = -999; return(View(op)); //break; } } else { return(View("Error")); } return(View(mathOp)); }
[TestMethod]//colocar [TestMethod] para poder ser identificado como test public void TestMethodAdd() { //Arrange Organiza los valores que puede tener y regresar MathOp Mo = new MathOp(); double Result; //Act ejecuta las acciones de los metodos Result = Mo.Add(20, 30); //Assert Assert.AreEqual(50, Result);// Varifica (Valor esperado, valor actual) }
public void TestMethodMultiply() { //Arrange MathOp Mo = new MathOp(); double Result; //Act Result = Mo.Multiply(20, 30); //Assert Assert.AreEqual(600, Result); }
public void TestMethodSubstract() { //Arrange MathOp Mo = new MathOp(); double Result; //Act Result = Mo.Substract(20, 30); //Assert Assert.AreEqual(-10, Result); }
internal static bool IsUnary(this MathOp op) { switch (op) { case MathOp.Negate: return(true); default: return(false); } }
void PerformAndShowMathOperation(string opType) { if (operationButtonIsBeanClickedBefore) { PerformSecondExpressionMember(opType); } calculate = ChooseMathOperation(opType); model.Argument1 = double.Parse(number); mainWindow.display.Text = number + " " + opType; number = null; operationButtonIsBeanClickedBefore = true; }
internal MathNode(MathOp operation, DiceAST left, DiceAST right) { Operation = operation; Left = left; Right = right ?? throw new ArgumentNullException(nameof(right)); if (!operation.IsUnary() && left == null) { throw new ArgumentNullException(nameof(left)); } _values = new List <DieResult>(); }
public void TestAddition () { MathOp op = new MathOp (); op.LeftOperand = 20; op.RightOperand = 20; op.Operator = "plus"; op.Result = 0; ViewResult ViewR = (ViewResult) controller.DoMathOp (op); MathOp model = (MathOp) ViewR.Model; Assert.Equal (60, model.Result); }
/// <summary> /// Simple local function with closure /// </summary> static void Closure() { var rand = new Random(); var randomValue = rand.Next(0, 10); // Note how add is using the local variable "randomValue" MathOp add = (x, y) => x + y + randomValue; Console.WriteLine($"The result is {add(1, 2)}"); randomValue = rand.Next(0, 10); Console.WriteLine($"The second result is {add(1, 2)}\n"); }
public static bool TryCalc(object a, object b, MathOp op, out object x) { x = null; try { if (a is int) { var i1 = Convert.ToInt32(a); var i2 = Convert.ToInt32(b); switch (op) { case MathOp.Add: x = i1 + i2; break; case MathOp.Sub: x = i1 - i2; break; case MathOp.Mul: x = i1 * i2; break; case MathOp.Div: x = i1 / i2; break; case MathOp.Xor: x = i1 ^ i2; break; case MathOp.Or: x = i1 | i2; break; case MathOp.And: x = i1 & i2; break; case MathOp.AndAnd: break; case MathOp.OrOr: break; } } } catch (InvalidCastException) { return false; } return true; }
public static float resolve(MathOp op, float a = 0.0f, float b = 0.0f){ switch(op.getOperation()){ case Operation.add: return add (a,b); case Operation.subtract: return subtract(a,b); case Operation.multiply: return multiply(a,b); case Operation.divide: return divide(a,b); default: return 0.0f; } }
public override Node Create(Vector2 pos) { MathOp node = CreateInstance <MathOp> (); node.name = "MathOp"; node.rect = new Rect(pos.x, pos.y, 100, 50); node.m_Value1 = new FloatRemap(0, -1, 10); node.CreateOutput("Output 1", "Float"); return(node); }
public override void DrawNodePropertyEditor() { base.DrawNodePropertyEditor(); m_OpType = (MathOp)UnityEditor.EditorGUILayout.EnumPopup(new GUIContent("Type", "The type of calculation performed on Input 1"), m_OpType, GUILayout.MaxWidth(200)); if (m_OpType == MathOp.Blend) { m_Value.SliderLabel(this, "Blend:");//, -1.0f, 1.0f);//,new GUIContent("Red", "Float"), m_R); } if (m_OpType == MathOp.SrcBlend) { m_Value.SliderLabel(this, "AlphaMult:");//, -1.0f, 1.0f);//,new GUIContent("Red", "Float"), m_R); } PostDrawNodePropertyEditor(); }
public void TestSubtraction () { //Arrange MathOp op = new MathOp (); op.LeftOperand = 20; op.RightOperand = 10; op.Operator = "Minus"; op.Result = 0; //Act ViewResult vr = (ViewResult) controller.DoMathOp (op); MathOp model = (MathOp) vr.Model; //Assert Assert.Equal (10, model.Result); }
public void TestDivision () { //Arrange MathOp op = new MathOp (); op.LeftOperand = 20; op.RightOperand = 20; op.Operator = "Divided By"; op.Result = 0; //Act ViewResult vr = (ViewResult) controller.DoMathOp (op); MathOp model = (MathOp) vr.Model; //Assert Assert.Equal (1.5, Math.Round(model.Result, 1) ); }
public void TestMultiplication () { //Arrange MathOp op = new MathOp (); op.LeftOperand = 30; op.RightOperand = 30; op.Operator = "Times"; op.Result = 0; //Act ViewResult vr = (ViewResult) controller.DoMathOp (op); MathOp model = (MathOp) vr.Model; //Assert Assert.Equal (900, model.Result); }
public TreeNode(MathOp op, TreeNode left, TreeNode right) { Op = op; Left = left; Right = right; }
public MathOpOrVal(MathOp op) { Op = op; }
/// <summary> /// Handles mathemathical operation. /// If l and r are both primitive values, the MathOp delegate is executed. /// /// TODO: Operator overloading. /// </summary> static ISymbolValue HandleSingleMathOp(IExpression x, ISemantic l, ISemantic r, MathOp m) { if(l == null || r == null) return null; var pl = l as PrimitiveValue; var pr = r as PrimitiveValue; //TODO: imaginary/complex parts if (pl != null && pr != null) { // If one if (pl.IsNaN || pr.IsNaN) return PrimitiveValue.CreateNaNValue(pl.IsNaN ? pl.BaseTypeToken : pr.BaseTypeToken, pl.IsNaN ? pl.Modifier : pr.Modifier); return new PrimitiveValue(pl.BaseTypeToken, m(pl, pr), 0M, pl.Modifier); } return null; //throw new NotImplementedException("Operator overloading not implemented yet."); }