Пример #1
0
        public static void Main(string[] args)
        {
            PrintMessage("Example with decimal values:");
            DecimalFactory decimalFactory = DecimalFactory.GetInstance();

            Value <decimal> V1 = new Value <decimal>(1.3M);
            Value <decimal> V2 = new Value <decimal>(3.8M);

            INode <decimal> AddOp = decimalFactory.GetAddOperation(V1, V2);

            Console.WriteLine($"compute {AddOp} = {AddOp.Compute()}");

            INode <decimal> SubOp = decimalFactory.GetSubtractOperation(V1, V2);

            Console.WriteLine($"compute {SubOp} = {SubOp.Compute()}");

            INode <decimal> MulOp = decimalFactory.GetMultiplyOperation(AddOp, SubOp);

            Console.WriteLine($"compute {MulOp} = {MulOp.Compute()}");

            PrintMessage("Calculator example with double values:");
            User <double> user = new User <double>();

            // user.SetValue(3.14);
            user.Apply(Operation.ADD, 0.14);
            user.Apply(Operation.ADD, 1.06);
            user.Apply(Operation.MULTIPLY, 3.14);

            PrintMessage("Check trigonometric evaluation:");
            IFactory <double> doubleFactory = DoubleFactory.GetInstance();

            INode <double> V     = new Value <double>(Math.PI / 3.0);
            INode <double> SinOp = (doubleFactory as ITrigonometryFactory <double>).GetSinOperation(V);

            Console.WriteLine($"compute {SinOp} = {SinOp.Compute()}");

            INode <double> Pow2SinOp = (doubleFactory as IFunctionFactory <double>).GetPow2Operation(SinOp);

            Console.WriteLine($"compute {Pow2SinOp} = {Pow2SinOp.Compute()}");

            INode <double> CosOp = (doubleFactory as ITrigonometryFactory <double>).GetCosOperation(V);

            Console.WriteLine($"compute {CosOp} = {CosOp.Compute()}");

            INode <double> Pow2CosOp = (doubleFactory as IFunctionFactory <double>).GetPow2Operation(CosOp);

            Console.WriteLine($"compute {Pow2CosOp} = {Pow2CosOp.Compute()}");

            INode <double> TrigAddOp = (doubleFactory as INumericFactory <double>).GetAddOperation(Pow2CosOp, Pow2SinOp);

            Console.WriteLine($"compute {TrigAddOp} = {TrigAddOp.Compute()}");
        }
Пример #2
0
        public User()
        {
            _calculator = new Calculator <T>();
            _history    = new LinkedList <ICommand>();
            _level      = 0;

            // Guess the correct factory from type
            string typeName = typeof(T).Name.ToLower();

            switch (typeName)
            {
            case "double":
                _factory = DoubleFactory.GetInstance() as IFactory <T>;
                break;

            case "decimal":
                _factory = DecimalFactory.GetInstance() as IFactory <T>;
                break;

            default:
                throw new NotSupportedException($"No factory found for type name '{typeName}'");
            }
        }