Beispiel #1
0
        static void Main(string[] args)
        {
            CalcOperation calc = new CalcOperation();

            calc.Before    = 20;
            calc.Operation = SimpleOperation.Multiply;
            calc.Before    = 2;
            Console.WriteLine(calc.After);
            calc.Operation = SimpleOperation.Addition;
            calc.Before    = 2;
            Console.WriteLine(calc.After);
            calc.Operation = SimpleOperation.Divide;
            calc.Before    = 3;
            Console.WriteLine(calc.After);
            calc.Operation = SimpleOperation.Substraction;
            calc.Before    = 4;
            Console.WriteLine(calc.After);
            calc.Operation = SimpleOperation.Square;
            calc.Before    = 2;
            Console.WriteLine(calc.After);



            Console.ReadLine();
        }
        public Command GetOperationCommand(CalcOperation operation)
        {
            switch (operation)
            {
            case CalcOperation.ADD:
                return(new Command(Calculator, new Operator(CalcOperation.ADD), Calculator.Add));

            case CalcOperation.SUB:
                return(new Command(Calculator, new Operator(CalcOperation.SUB), Calculator.Sub));

            case CalcOperation.MUL:
                return(new Command(Calculator, new Operator(CalcOperation.MUL), Calculator.Mul));

            case CalcOperation.DIV:
                return(new Command(Calculator, new Operator(CalcOperation.DIV), Calculator.Div));

            case CalcOperation.CHS:
                return(new Command(Calculator, new Operator(CalcOperation.CHS), Calculator.ChangeSign));

            case CalcOperation.DROP:
                return(new Command(Calculator, new Operator(CalcOperation.DROP), Calculator.Drop));

            case CalcOperation.SWAP:
                return(new Command(Calculator, new Operator(CalcOperation.SWAP), Calculator.Swap));

            default:
                return(null);
            }
        }
Beispiel #3
0
        public override void CleanFormula(bool appendScope, StringBuilder sb)
        {
            if (appendScope)
            {
                sb.Append('(');
            }

            bool first = true;

            foreach (var operation in _calcs)
            {
                if (operation.Operation == CalcOperation.EOperation.Minus || !first)
                {
                    sb.Append(CalcOperation.GetStringOperation(operation.Operation));
                }

                operation.Calc.CleanFormula(true, sb);

                first = false;
            }

            if (appendScope)
            {
                sb.Append(')');
            }
        }
        public double Calc(double a, double b, CalcOperation operation)
        {
            double result = 0;

            switch (operation)
            {
            case CalcOperation.Addition:
                result = a + b;
                break;

            case CalcOperation.Difference:
                result = a - b;
                break;

            case CalcOperation.Multiplication:
                result = a * b;
                break;

            case CalcOperation.Division:
                if ((a == 0) && (b == 0))
                {
                    throw new ArithmeticException();
                }
                if (b == 0)
                {
                    throw new DivideByZeroException();
                }
                result = a / b;
                break;
            }
            return(result);
        }
Beispiel #5
0
        public void OperatorClicked(CalcOperation calcOperaiton)
        {
            switch (calcOperaiton)
            {
            case CalcOperation.ADD:
                Framework.CurrentInput += " + ";
                break;

            case CalcOperation.SUB:
                Framework.CurrentInput += " - ";
                break;

            case CalcOperation.MUL:
                Framework.CurrentInput += " *  ";
                break;

            case CalcOperation.DIV:
                Framework.CurrentInput += " /  ";
                break;

            case CalcOperation.ENTER:
                Framework.CurrentInput += " ";
                break;

            default:
                Framework.TriggerRefreshUI("You are in PROG state.. Can't use that here");
                break;
            }
            Framework.TriggerRefreshUI("Added to expression");
        }
        public void ToCalculation_MathExprStr_CorrectObjProperties()
        {
            Calculation testCalculation = _mathExpression.ToCalculaton();

            const double  expectedFirstOperand  = -1.5D;
            const double  expectedSecondOperand = 2D;
            const double  expectedResult        = -3D;
            CalcOperation expectedOperation     = CalcOperation.Multiplication;

            Assert.AreEqual(expectedFirstOperand, testCalculation.FirstOperand);
            Assert.AreEqual(expectedSecondOperand, testCalculation.SecondOperand);
            Assert.AreEqual(expectedResult, testCalculation.Result);
            Assert.AreEqual(expectedOperation, testCalculation.Operation);
        }
 private void Calc(CalcOperation operation)
 {
     try
     {
         this.view.SetResult(this.model.Calc(this.view.GetFirstArg(), this.view.GetSecondArg(), operation));
     }
     catch (DivideByZeroException)
     {
         this.view.ShowMessege("Sorry, but result of the operation can not be calculated: Division by zero");
     }
     catch (Exception)
     {
         this.view.ShowMessege("Sorry, but result of the operation can not be calculated");
     }
 }
Beispiel #8
0
        public static void ParseTwoStrings(
            object o1,
            object o2,
            object o3,
            string s4,
            out int i1,
            out int i2,
            out int i3,
            out CalcOperation op)
        {
            i1 = 0;
            if (o1 is string s1)    // pattern matching with is
            {
                int.TryParse(s1, out i1);
            }

            i2 = 0;
            switch (o2)
            {
            case int i:        // pattern Matching
                i2 = i;
                break;

            case bool b when b == true:        // pattern matching with when
                i2 = 1;
                break;

            case string s2:
                int.TryParse(s2, out i2);
                break;

            case null:
                i2 = 0;
                break;
            }
            i2 = i2 != 0 ? i2 : throw new ArgumentException("can't be 0");

            i3 = 0;
            if (o3 is string s3)    // pattern matching with is
            {
                int.TryParse(s3, out i3);
            }

            if (!Enum.TryParse(s4, true, out op))
            {
                op = CalcOperation.Unknown;
            }
        }
        public void OnPost(int NumberA, int NumberB, CalcOperation Calc)
        {
            switch (Calc)
            {
            case CalcOperation.Nothing: Result = "Invalid operation"; break;

            case CalcOperation.Addition: Result = "Result: " + (NumberA + NumberB); break;

            case CalcOperation.Subtraction: Result = "Result: " + (NumberA - NumberB); break;

            case CalcOperation.Multiplication: Result = "Result: " + (NumberA * NumberB); break;

            case CalcOperation.Division:
                Result = (NumberB == 0) ? "Result: Division by 0." : "Result: " + (NumberA / NumberB);
                break;
            }
        }
Beispiel #10
0
        public bool CanExecute(object parameter)
        {
            CalcOperation param = (CalcOperation)parameter;

            switch (param.Type)
            {
            case CalcOperation.OperationType.Digit:
            case CalcOperation.OperationType.EqualOp:
                if ((new[] { CalcOperation.DotOp, CalcOperation.NegOp }.Contains(param)))
                {
                    return(viewModel.IsValidState);
                }
                return(true);

            default:
                return(viewModel.IsValidState);
            }
        }
Beispiel #11
0
        public void OperatorClicked(CalcOperation calcOperaiton)
        {
            switch (calcOperaiton)
            {
            case CalcOperation.ADD:
                Strategy = new AddStrategy(Framework.CurrentInput);
                break;

            case CalcOperation.SUB:
                Strategy = new SubStrategy(Framework.CurrentInput);
                break;

            case CalcOperation.MUL:
                Strategy = new MulStrategy(Framework.CurrentInput);
                break;

            case CalcOperation.DIV:
                Strategy = new DivStrategy(Framework.CurrentInput);
                break;

            case CalcOperation.ENTER:
                Strategy = new EnterStrategy(Framework.CurrentInput);
                break;

            case CalcOperation.CHS:
                Strategy = new ChangeSignStrategy();
                break;

            case CalcOperation.DROP:
                Strategy = new DropStrategy();
                break;

            case CalcOperation.SWAP:
                Strategy = new SwapStrategy();
                break;
            }

            Strategy.ExecuteStrategy();
            Framework.CurrentInput = "";
            Framework.TriggerRefreshUI("Executed " + calcOperaiton);
        }
Beispiel #12
0
        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.Append('(');

            bool first = true;

            foreach (var operation in _calcs)
            {
                if (operation.Operation == CalcOperation.EOperation.Minus || !first)
                {
                    sb.Append(CalcOperation.GetStringOperation(operation.Operation));
                }

                sb.Append(operation.Calc.ToString());

                first = false;
            }

            sb.Append(')');

            return(sb.ToString());
        }
Beispiel #13
0
 public Operator(CalcOperation val)
 {
     Value = val;
 }
Beispiel #14
0
 public CalcOperationTest()
 {
     calculator = new CalcOperation();
 }
 public void OperatorClicked(CalcOperation calcOperaiton)
 {
     Framework.TriggerRefreshUI("You are in Recall state.. Can't use that here");
 }
Beispiel #16
0
 public Operation(CalcOperation operation) => _operation = operation;        // expression-bodied members
Beispiel #17
0
        static void LambdaExpressions()
        {
            int result0 = Add(1, 2);

            Console.WriteLine(result0);

            // delegate
            CalcOperation calc1   = Add;
            int           result1 = calc1(3, 4);

            Console.WriteLine(result1);

            // anonymous method C# 2.0
            CalcOperation calc2 = delegate(int a, int b)
            {
                return(a + b);
            };
            int result2 = calc2(5, 6);

            Console.WriteLine(result2);


            Func <int, int, int> calc3 = delegate(int a, int b)
            {
                return(a + b);
            };
            int result3 = calc3(7, 8);

            Console.WriteLine(result3);



            // lambda expressions C# 3.0
            // with statement body
            Func <int, int, int> calc4 = (a, b) =>
            {
                return(a + b);
            };
            int result4 = calc4(9, 10);

            Console.WriteLine(result4);

            // lambda expressions C# 3.0
            // with expression body
            Func <int, int, int> calc5 = (a, b) => a + b;
            int result5 = calc5(11, 12);

            Console.WriteLine(result5);

            Func <int>       x1 = () => 42;
            Func <int, bool> x2 = a => a >= 0;

            Console.WriteLine(x1().ToString());
            Console.WriteLine(x2(1).ToString());
            Console.WriteLine(x2(-1).ToString());


            var list = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            var filterdList = list.Filter(x => x > 7);

            filterdList.Dump();

            list.Filter(y => y <= 5).Dump();

            list.Filter(a => a < 6).Filter(a => a >= 2).Dump();
            list.Filter(a => a < 6 && a >= 2).Dump();
        }
 public void OperatorClicked(CalcOperation calcOperaiton)
 {
     CurrentState.OperatorClicked(calcOperaiton);
 }