Exemple #1
0
		private static string GetOperationSymbol(MathOperation op)
		{
			switch (op)
			{
				case MathOperation.Add:
					return "+";
				case MathOperation.Subtract:
					return "-";
				case MathOperation.Multiply:
					return "*";
				case MathOperation.Divide:
					return "/";
				case MathOperation.Modulus:
					return "%";
				case MathOperation.And:
					return "&";
				case MathOperation.Or:
					return "|";
				case MathOperation.Xor:
					return "^";
				case MathOperation.ShiftLeft:
					return "<<";
				case MathOperation.ShiftRight:
					return ">>";
				case MathOperation.ShiftRightSignExtended:
					return ">>>";
				default:
					throw new NotSupportedException();
			}
		}
Exemple #2
0
		public Math(LIRMethod parent, ISource srcA, ISource srcB, IDestination dest, MathOperation op, LIRType argType) : base(parent, LIROpCode.Math)
		{
			SourceA = srcA;
			SourceB = srcB;
			Destination = dest;
			Operation = op;
			ArgumentType = argType;
		}
Exemple #3
0
        public IActionResult DoCalculation([FromForm] MathOperation mathOperation)
        {
            //_logger.LogInformation()
            //_logger.LogInformation("##### --> Operator Value: {0}", mathOperation.Operator);

            if (ModelState.IsValid)
            {
                //_logger.LogInformation("##### --> Model state error count: {0}", ModelState.ErrorCount);
            }
            else
            {
            }

            if (mathOperation == null)
            {
                return(View("Error"));
            }
            else
            {
                switch (mathOperation.Operator)
                {
                case "Add":
                    mathOperation.Result =
                        mathOperation.LeftOperand + mathOperation.RightOperand;
                    break;

                case "Subtract":
                    mathOperation.Result =
                        mathOperation.LeftOperand - mathOperation.RightOperand;
                    break;

                case "Multiply":
                    mathOperation.Result =
                        mathOperation.LeftOperand * mathOperation.RightOperand;
                    break;

                case "Divide":
                    mathOperation.Result =
                        mathOperation.LeftOperand / mathOperation.RightOperand;
                    break;

                case "Modulus":
                    mathOperation.Result =
                        mathOperation.LeftOperand % mathOperation.RightOperand;
                    break;

                default:
                    MathOperation op = new MathOperation();
                    op.LeftOperand = mathOperation.LeftOperand;
                    op.LeftOperand = mathOperation.RightOperand;
                    op.Operator    = mathOperation.Operator;
                    op.Result      = 0;
                    return(View(op));
                    //break;
                }
            }
            return(View(mathOperation));
        }
Exemple #4
0
        static void MyCallBackMethod(IAsyncResult ar)
        {
            AsyncResult   asyncResult = (AsyncResult)ar;
            MathOperation op          = (MathOperation)asyncResult.AsyncDelegate;

            double result = op.EndInvoke(ar);

            Console.WriteLine("Result: {0}", result);
        }
Exemple #5
0
        public EquationValidity IsValid(string first, MathOperation operation, string second)
        {
            if (MathOperation.Division == operation && second == "0")
            {
                return(EquationValidity.DivisionByZero);
            }

            return(EquationValidity.Valid);
        }
Exemple #6
0
        public void Test_Expression_With_One_Operation()
        {
            var expression = new MathExpression("10-3");

            Assert.AreEqual(expression.FirstValue, 10);
            var operation = new MathOperation(Operator.Subtract, 3);

            Assert.AreEqual(expression.Operations.FirstOrDefault(), operation);
        }
        public void EnumValues_AreAllHandled(MathOperation operation)
        {
            var converter = new MathMultipleConverter
            {
                Operation = operation
            };

            Assert.True(converter.Convert(new object?[] { 1.0, 1.0 }, null, null, CultureInfo.CurrentUICulture) is double);
        }
        static void Main(string[] args)
        {
            var add    = new MathOperation(Addition);
            int result = add(5, 2);

            Console.Write(result);

            Console.ReadKey();
        }
Exemple #9
0
        static void Main(string[] args)
        {
            int num1 = 10;
            int num2 = 20;
            int sum  = MathOperation.Add(num1, num2); // Method from class library

            Console.WriteLine($"{num1} + {num2} = {sum}");
            Console.ReadLine();
        }
Exemple #10
0
        public void EnumValues_AreAllHandled(MathOperation operation)
        {
            var converter = new MathConverter
            {
                Operation = operation
            };

            Assert.True(converter.Convert(1.0, null, 1.0, CultureInfo.CurrentUICulture) is double);
        }
Exemple #11
0
        public void GIVEN_func_is_possible_THEN_perform_returns_success_result()
        {
            MathOperation mathOperation = new MathOperation(
                (i, j) => i + j, "somestring");

            var result = mathOperation.Perform(1, 2);

            Assert.IsTrue(result.Success);
        }
Exemple #12
0
        static void Main(string[] args)
        {
            var add = new MathOperation(Addition);
            int result = add(5, 2);

            Console.Write(result);

            Console.ReadKey();
        }
Exemple #13
0
 static int[] MapMathOperation(int[] arr1, int[] arr2, MathOperation m)
 {
     int[] result = new int[arr1.Length];
     for (int i = 0; i < arr1.Length; i++)
     {
         result[i] = m(arr1[i], arr2[i]);
     }
     return(result);
 }
        private MathQuestion GetRandomQuestion()
        {
            MathOperation operation    = _mathManager.GetRendomOperation();
            int           firstNumber  = _mathManager.GetFirstNumber();
            int           secondNumber = _mathManager.GetSecondNumber();
            MathQuestion  mathQuestion = _mathManager.CreateMathQuestion(firstNumber, secondNumber, operation);

            return(mathQuestion);
        }
        public void Calculate_LogsCorrectEntry(int operand1, int operand2, MathOperation operation,
                                               string expectedLogEntry)
        {
            MockLogger  mockLogger = MakeMockLogger();
            ICalculator calculator = MakeCalculator(mockLogger);

            calculator.Calculate(operand1, operand2, operation);
            Assert.That(mockLogger.LogEntries.Last(), Is.EqualTo(expectedLogEntry));
        }
Exemple #16
0
        public void GIVEN_func_causes_divide_by_zero_THEN_perform_returns_failure_result()
        {
            MathOperation mathOperation = new MathOperation(
                (i, j) => i / 0, "somestring");

            var result = mathOperation.Perform(1, 2);

            Assert.IsFalse(result.Success);
        }
 public BinaryOperatorValueExtractor(
     IValueExtractor left,
     IValueExtractor right,
     MathOperation	 operation)
 {
     mLeft				= left;
     mRight			= right;
     mOperation		= operation;
 }
Exemple #18
0
 public BinaryOperatorValueExtractor(
     IValueExtractor left,
     IValueExtractor right,
     MathOperation operation)
 {
     mLeft      = left;
     mRight     = right;
     mOperation = operation;
 }
Exemple #19
0
        public void Create_Math_Operation()
        {
            int      value         = 10;
            Operator mathOperator  = Operator.Add;
            var      mathOperation =
                new MathOperation(mathOperator, value);

            Assert.AreEqual(mathOperation.Value, 10);
            Assert.AreEqual(mathOperation.Operator, mathOperator);
        }
Exemple #20
0
 private static float Calculate(float first, float second, MathOperation op)
 {
     return(op switch
     {
         MathOperation.add => (first + second),
         MathOperation.sub => (first - second),
         MathOperation.mul => (first * second),
         MathOperation.div => (second == 0 ? 0 : first / second),
         _ => 0
     });
Exemple #21
0
        public void Operation(MathOperation operation, double value)
        {
            var foundOperation = MathOperations.TryGetValue(operation, out var operationFunc);

            if (foundOperation)
            {
                _cachedValue = operationFunc(_cachedValue, value);
                Console.WriteLine($"Current value = {_cachedValue} (last operation: {operation} {value})");
            }
        }
        public void Calculate_ReturnsCorrectResult(int operand1, int operand2, MathOperation operation,
                                                   int expectedResult)
        {
            StubLogger  stubLogger = MakeStubLogger();
            ICalculator calculator = MakeCalculator(stubLogger);

            int result = calculator.Calculate(operand1, operand2, operation);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Exemple #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MathTime"/> with the specified
        /// lengths and mathematical operation.
        /// </summary>
        /// <param name="length1">The <see cref="ILength"/> to add <paramref name="length2"/> to or
        /// subtract <paramref name="length2"/> from.</param>
        /// <param name="length2">The <see cref="ILength"/> to add to or subtract from <paramref name="length1"/>.</param>
        /// <param name="operation">Mathematical operation to perform on <paramref name="length1"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="length1"/> is null. -or-
        /// <paramref name="length2"/> is null.</exception>
        /// <exception cref="InvalidEnumArgumentException"><paramref name="operation"/> specified
        /// an invalid value.</exception>
        public MathLength(ILength length1, ILength length2, MathOperation operation = MathOperation.Sum)
        {
            ThrowIfArgument.IsNull(nameof(length1), length1);
            ThrowIfArgument.IsNull(nameof(length2), length2);
            ThrowIfArgument.IsInvalidEnumValue(nameof(operation), operation);

            Length1   = length1;
            Length2   = length2;
            Operation = operation;
        }
        public void Sum(int x, int y)
        {
            LastX         = x;
            LastY         = y;
            LastOperation = MathOperation.Sum;

            LastResult = x + y;

            ShowResult(LastResult);
        }
Exemple #25
0
        static void Main(string[] args)
        {
            //method group conversion
            MathOperation addition = Addition;
            int           result   = addition(5, 2);

            Console.Write(result);

            Console.ReadKey();
        }
        public void Subtract(int x, int y)
        {
            LastX         = x;
            LastY         = y;
            LastOperation = MathOperation.Subtract;

            LastResult = x - y;

            ShowResult(LastResult);
        }
Exemple #27
0
        public IActionResult DoCalculation([FromForm] MathOperation mathOperation)
        {
            if (ModelState.IsValid)
            {
            }
            else
            {
            }

            if (mathOperation == null)
            {
                return(View("Error"));
            }
            else
            {
                switch (mathOperation.Operator)
                {
                case "Add":
                    mathOperation.Result =
                        mathOperation.LeftOperand + mathOperation.RightOperand;
                    break;

                case "Subtract":
                    mathOperation.Result =
                        mathOperation.LeftOperand - mathOperation.RightOperand;
                    break;

                case "Multiply":
                    mathOperation.Result =
                        mathOperation.LeftOperand * mathOperation.RightOperand;
                    break;

                case "Divide":
                    mathOperation.Result =
                        mathOperation.LeftOperand / mathOperation.RightOperand;
                    break;

                case "Modulus":
                    mathOperation.Result =
                        mathOperation.LeftOperand % mathOperation.RightOperand;
                    break;

                default:
                    MathOperation op = new MathOperation();
                    op.LeftOperand = mathOperation.LeftOperand;
                    op.LeftOperand = mathOperation.RightOperand;
                    op.Operator    = mathOperation.Operator;
                    op.Result      = 0;
                    return(View(op));
                    //break;
                }
            }
            return(View(mathOperation));
        }
Exemple #28
0
        int Operation(int[] numbers, int start, MathOperation operation)
        {
            int sum = start;

            foreach (var number in numbers)
            {
                operation(sum, number);
            }

            return(sum);
        }
Exemple #29
0
        public static int ProcessNumbers(int a, int b, MathOperation mathOperation)
        {
            //int store
            // int validation
            // int datye, business check
            MathOperation test = new MathOperation(Sum);

            test += Subtract;

            return(test(10, 20));
        }
        public void TestMinimum()
        {
            //arrange
            int a = 6; int b = 7; int c = 8;
            var expectedValue = 6;
            //act
            var min = MathOperation.Minimum(a, b, c);

            //assert
            Assert.Equal(expectedValue, min);
        }
        public void Sqrt_WithDecimalNumber_ReturnsResult()
        {
            // Arrange
            decimal p = 16;

            // Act
            decimal lResult = MathOperation.Sqrt(p);

            // Assert
            Assert.AreEqual(4, lResult);
        }
        public void Sqrt_WithDoubleNumber_ReturnsResult()
        {
            // Arrange
            double p = 16;

            // Act
            string lResult = MathOperation.Sqrt(p);

            // Assert
            Assert.AreEqual("4", lResult);
        }
Exemple #33
0
        static void Main(string[] args)
        {
            AsyncCallback callback = MyCallBackMethod;

            MathOperation op = Math.Sin;

            //last parameter, state information
            op.BeginInvoke(Math.PI / 2, callback, "name");
            Console.WriteLine("delegate invoked");
            Console.ReadLine();
        }
Exemple #34
0
        static void Main(string[] args)
        {
            AsyncCallback callback = MyCallBackMethod;

            MathOperation op = DelegateMethod;

            op.BeginInvoke(1000000, callback, null);
            Console.WriteLine("delegate invoked");
            Console.WriteLine("continuing in main thread while calculation runs in background");
            Console.ReadLine();
        }
        public static void FloatCalculations(float value, MathOperation operation)
        {
            float result = 0.0f;

            stopwatch.Start();
            switch (operation)
            {
                case MathOperation.SquareRoot: result = (float)Math.Sqrt(value);
                    break;
                case MathOperation.NaturalLogarithm: result = (float)Math.Log(value, Math.E);
                    break;
                case MathOperation.Sinus: result = (float)Math.Sin(value);
                    break;
                default: throw new ArgumentException("Invalid operation!");
            }

            stopwatch.Stop();
            Console.WriteLine("{0} time -> {1}", operation, stopwatch.Elapsed);
            stopwatch.Reset();
        }
        public static void DecimalCalculations(decimal value, MathOperation operation)
        {
            decimal result = 0.0m;

            stopwatch.Start();
            switch (operation)
            {
                case MathOperation.SquareRoot: result = (decimal)Math.Sqrt((double)value);
                    break;
                case MathOperation.NaturalLogarithm: result = (decimal)Math.Log((double)value, Math.E);
                    break;
                case MathOperation.Sinus: result = (decimal)Math.Sin((double)value);
                    break;
                default: throw new ArgumentException("Invalid operation!");
            }

            stopwatch.Stop();
            Console.WriteLine("{0} time -> {1}", operation, stopwatch.Elapsed);
            stopwatch.Reset();
        }
        private void PerfomMathOperation(MathOperation operation, String leftOperand, String rightOperation)
        {
            _view.ErrorMessage = "";
            _view.Result = "";
            try
            {
                if (!_converterLeft.ValidateFractionString(leftOperand))
                {
                    _view.ErrorMessage = "First field: " + _converterLeft.LastFormatError;
                    return;
                }
                if (!_converterRight.ValidateFractionString(rightOperation))
                {
                    _view.ErrorMessage = "Second field: " + _converterRight.LastFormatError;
                    return;
                }

                Fraction leftFraction = _converterLeft.ConvertFractionFromString(leftOperand);
                Fraction rightFraction = _converterLeft.ConvertFractionFromString(rightOperation);

                Fraction resultFraction = operation(leftFraction, rightFraction);

                switch (_view.ResultFormat)
                {
                    case ResultFormatTypes.Fractional:
                        _view.Result = _converterResult.ConvertStringFromFraction(resultFraction, FractionToStringFormatTypes.ToFractional);
                        break;
                    case ResultFormatTypes.Decimal:
                        _view.Result = _converterResult.ConvertStringFromFraction(resultFraction, FractionToStringFormatTypes.ToDecimal);
                        break;
                }
            }
            catch (Exception e)
            {
                _view.ErrorMessage = e.Message;
            }
        }
Exemple #38
0
 public Fact(int leftNum, int rightNum, MathOperation operation)
 {
     this.leftNum = leftNum;
     this.rightNum = rightNum;
     this.operation = operation;
 }
 public ExpressionTree(MathOperation l, MathOperation val, MathOperation r)
 {
     Value = val;
     Left = new ExpressionTree(l);
     Right = new ExpressionTree(r);
 }
 public void ReplaceWith(MathOperation l, MathOperation val, MathOperation r)
 {
     Value = val ;
     Left = new ExpressionTree(l);
     Right = new ExpressionTree(r);
 }
 private void button_Click(object sender, RoutedEventArgs e)
 {
     switch(((Button)sender).Name)
     {
         case "button0":
             textBox.Text += "0";
             CheckText();
             break;
         case "button1":
             textBox.Text += "1";
             CheckText();
             break;
         case "button2":
             textBox.Text += "2";
             CheckText();
             break;
         case "button3":
             textBox.Text += "3";
             CheckText();
             break;
         case "button4":
             textBox.Text += "4";
             CheckText();
             break;
         case "button5":
             textBox.Text += "5";
             CheckText();
             break;
         case "button6":
             textBox.Text += "6";
             CheckText();
             break;
         case "button7":
             textBox.Text += "7";
             break;
         case "button8":
             textBox.Text += "8";
             CheckText();
             break;
         case "button9":
             textBox.Text += "9";
             CheckText();
             break;
         case "buttonComma":
             textBox.Text += ",";
             break;
         case "buttonAdd":
             calc.number1 = GetNumber();
             textBox.Text = "0";
             mathOp = calc.Add;
             break;
         case "buttonSubtract":
             calc.number1 = GetNumber();
             textBox.Text = "0";
             mathOp = calc.Subtract;
             break;
         case "buttonDivide":
             calc.number1 = GetNumber();
             textBox.Text = "0";
             mathOp = calc.Divide;
             break;
         case "buttonMultiply":
             calc.number1 = GetNumber();
             textBox.Text = "0";
             mathOp = calc.Multiply;
             break;
         case "buttonSqrt":
             calc.number1 = GetNumber();
             mathOp = calc.Sqrt;
             textBox.Text = mathOp().ToString();
             break;
         case "buttonSign":
             textBox.Text = (-GetNumber()).ToString();
             break;
         case "buttonSquare":
             calc.number1 = GetNumber();
             mathOp = calc.Square;
             textBox.Text = mathOp().ToString();
             break;
         case "buttonEqual":
             if (mathOp != null)
             {
                 calc.number2 = GetNumber();
                 textBox.Text= mathOp().ToString();
             }
             break;
         case "buttonDelete":
             if (textBox.Text.Length > 0) textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1);
             if (textBox.Text.Length == 0) textBox.Text = "0";
             break;
     }
 }
        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            KeyValueSerializer keyValue = new KeyValueSerializer();
            string sKey = keyValue.ConvertToString(e.Key, null);
            Int32 input;
            bool parseResult=Int32.TryParse(sKey,out input);
            if(parseResult && (input<=9 && input>=0))
            {
                textBox.Text += sKey;
                CheckText();
            }

            switch (e.Key)
            {
                case Key.Escape:
                    Close();
                    break;
                case Key.OemPlus:
                    calc.number1 = GetNumber();
                    mathOp = calc.Add;
                    textBox.Text = "0";
                    break;
                case Key.OemMinus:
                    calc.number1 = GetNumber();
                    mathOp = calc.Subtract;
                    textBox.Text = "0";
                    break;
                case Key.OemQuestion:
                    calc.number1 = GetNumber();
                    mathOp = calc.Multiply;
                    textBox.Text = "0";
                    break;
                case Key.D7:
                    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                    {
                        calc.number1 = GetNumber();
                        mathOp = calc.Divide;
                        textBox.Text = "0";
                    }
                    break;

                case Key.Enter:
                    if (mathOp != null)
                    {
                        calc.number2 = GetNumber();
                        textBox.Text = mathOp().ToString();
                    }
                    break;
            }
        }
 private float Operate(float val1, float val2, MathOperation operation)
 {
     float retval = 0.0f;
     if (operation == MathOperation.Add)
     {
         retval = val1 + val2;
     }
     else if (operation == MathOperation.Subtract)
     {
         retval = val1 - val2;
     }
     else if (operation == MathOperation.Multiply)
     {
         retval = val1 * val2;
     }
     else if (operation == MathOperation.Divide)
     {
         retval = val1 / val2; // ok to throw exception.
     }
     else if (operation == MathOperation.Assign)
     {
         retval = val2;
     }
     return retval;
 }