示例#1
0
        public CalcExpression(CalcMethod calcMethod, IList <IExpression> expressions)
        {
            _expressions = expressions;
            switch (calcMethod)
            {
            case CalcMethod.Addition:
                _calc = (x, y) => x + y;
                break;

            case CalcMethod.Subtraction:
                _calc = (x, y) => x - y;
                break;

            case CalcMethod.Multiplication:
                _calc = (x, y) => x * y;
                break;

            case CalcMethod.Division:
                _calc = (x, y) => x / y;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(calcMethod), calcMethod, null);
            }
        }
示例#2
0
    static void Main(string[] args)
    {
        CalcMethod calc = new CalcMethod(Calc.Cumsum);

        calc.BeginInvoke(1, 100, calcCompleted, calc);
        Console.ReadLine();
    }
示例#3
0
        static void Main(string[] args)
        {
            CalcMethod calc   = new CalcMethod(Calc.Cumsum);
            long       result = calc(1, 100);

            Console.WriteLine(result);
        }
示例#4
0
        private static void CalcCompleted(IAsyncResult _async)
        {
            CalcMethod calc   = _async.AsyncState as CalcMethod;
            int        result = calc.EndInvoke(_async);

            Console.WriteLine("1 ~ 100 더한값 : " + result);
        }
示例#5
0
        static void Main(string[] args)
        {
            Func <int, int> square = x => x * x;

            Console.WriteLine(square(5));
            int[] numbers        = { 2, 3, 4, 5 };
            var   squaredNumbers = numbers.Select(x => x * x);

            Console.WriteLine(string.Join(" ", squaredNumbers));
            Action line = () => Console.WriteLine();

            line();
            CalcMethod add      = (a, b) => a + b;
            CalcMethod subtract = (a, b) => a - b;

            Console.WriteLine(add(10, 20));
            Console.WriteLine(subtract(10.5, 20));
            IsTeenAger isTeenAger = delegate(Student s) { return(s.Age > 12 && s.Age < 20); };
            Student    s1         = new Student()
            {
                Name = "John", Age = 18
            };

            Console.WriteLine("{0}은 {1}.", s1.Name, isTeenAger(s1) ? "청소년입니다" : "청소년이 아닙니다");
            IsAdult isAdult = (s) => {
                int adultAge = 18;
                return(s.Age >= adultAge);
            };
            Student s2 = new Student()
            {
                Name = "Robin", Age = 20
            };

            Console.WriteLine("{0}은 {1}.", s2.Name, isAdult(s2) ? "성인입니다" : "성인이 아닙니다");
        }
示例#6
0
    static void calcCompleted(IAsyncResult ar)
    {
        CalcMethod calc   = ar.AsyncState as CalcMethod;
        long       result = calc.EndInvoke(ar);

        Console.WriteLine(result);
    }
示例#7
0
        public static double GetPI(CalcMethod method)
        {
            _calcMethod = method;
            int groups = (N % CountOfThreads == 0) ? CountOfThreads : CountOfThreads + 1;

            Thread[] threads = new Thread[groups];
            _values = new double[groups];
            for (int k = 0; k < groups; k++)
            {
                threads[k] = new Thread(Body);
                threads[k].Start(k);
            }
            for (int k = 0; k < groups; k++)
            {
                threads[k].Join();
            }
            if (_calcMethod == ASinMethod)
            {
                return(_values.Average());
            }
            if (_calcMethod == LeibnizMethod)
            {
                return(_values.Sum() * 4);
            }
            if (_calcMethod == IntegralMethod)
            {
                return(_values.Sum());
            }
            if (_calcMethod == MonteCarloMethod)
            {
                return(4 * _values.Sum() / N);
            }
            return(-1);
        }
示例#8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="name"></param>
 /// <param name="items"></param>
 /// <param name="method"></param>
 /// <param name="round">計算到小數第幾位。</param>
 public CalculationScoreParser(string name, ItemWeightCollection items, CalcMethod method, int round, string token)
 {
     Name          = name;
     Items         = items;
     Method        = method;
     RoundPosition = round;
     ThisToken     = token;
 }
示例#9
0
        public static void RunWithCallBack()
        {
            Console.WriteLine("DelegateAsyncEx.RunWithCallBack Start");
            CalcMethod calc = new CalcMethod(Calc.StartEndAdder);

            calc.BeginInvoke(1, 100, CalcCompleted, calc);
            Console.WriteLine("DelegateAsyncEx.RunWithCallBack End");
            Console.WriteLine();
        }
示例#10
0
        static void Main(string[] args)
        {
            CalcMethod   calc = new CalcMethod(Calc.Cumsum);
            IAsyncResult ar   = calc.BeginInvoke(1, 100, null, null);

            ar.AsyncWaitHandle.WaitOne();
            long result = calc.EndInvoke(ar);

            Console.WriteLine(result);
        }
示例#11
0
        public static void RunWithInvoke()
        {
            Console.WriteLine("DelegateAsyncEx.RunWithInvoke Start");
            CalcMethod   calc  = new CalcMethod(Calc.StartEndAdder);
            IAsyncResult async = calc.BeginInvoke(1, 100, null, null);

            async.AsyncWaitHandle.WaitOne();
            int result = calc.EndInvoke(async);

            Console.WriteLine("1 ~ 100 더한값 : " + result);
            Console.WriteLine("DelegateAsyncEx.RunWithInvoke End");
            Console.WriteLine();
        }
示例#12
0
        static void Main(string[] args)
        {
            #region 456

            /*
             * FileStream fs = new FileStream(@"C:\Windows\System32\drivers\etc\hosts", FileMode.Open);
             *
             * FileState state = new FileState();
             * state.Buffer = new byte[fs.Length];
             * state.File = fs;
             *
             * fs.BeginRead(state.Buffer, 0, state.Buffer.Length, readCompleted, state);
             *
             * // BeginRead 비동기 메서드 호출은 스레드로 곧바로 제어를 반환하기 때문에
             * // 이곳에서 자유롭게 다른 연산을 동시에 진행할 수 있다.
             *
             * Console.ReadLine();
             * fs.Close();*/
            #endregion 456
            #region 458

            /*
             * ThreadPool.QueueUserWorkItem(readCompleted);
             *
             * // QueueUserWorkItem 메서드 호출은 곧바로 제어를 반환하기 때문에
             * // 이곳에서 자유롭게 다른 연산을 동시에 진행할 수 있다.
             * Console.ReadLine();*/
            #endregion 458
            CalcMethod calc = new CalcMethod(Calc.Cumsum);

            //long result = calc(1, 100);
            //Console.WriteLine(result);  // 출력 결과: 5050

            // Delegate 타입의 BeginInvoke 메서드를 호출한다.
            // 이 때문에 Calc.Cumsum 메서드는 ThreadPool의 스레드에서 실행된다.
            IAsyncResult ar = calc.BeginInvoke(1, 100, null, null);

            // BeginInvoke로 반환받은 IAsyncResult 타입의 AsyncWaitHandle 속성은 EventWaitHandle 타입이다.
            // AsyncWaitHandle 객체는 스레드 풀에서 실행된 Calc.Cumsum의 동작이 완료했을 때 Signal 상태로 바뀐다.
            // 따라서 아래의 호출은 Calc.Cumsum 메서드 수행이 완료될 때까지 현재 스레드를 대기시킨다.
            ar.AsyncWaitHandle.WaitOne();

            // Calc.Cumsum의 반환값을 얻기 위해 EndInvoke 메서드를 호출한다.
            // 반환값이 없어도 EndInvoke는 반드시 호출하는 것을 권장한다.
            long result = calc.EndInvoke(ar);
            Console.WriteLine(result);
        }
示例#13
0
        /// <summary>
        /// Expand selection to the index where the specified method calculates
        /// (selection anchor will not be changed).
        /// </summary>
        public static void SelectTo(CalcMethod calculator, IUserInterface ui)
        {
            var doc  = ui.Document;
            var view = ui.View as IViewInternal;
            int nextIndex;

            // calculate where to expand selection
            nextIndex = calculator(view);
            if (nextIndex == doc.CaretIndex)
            {
                // notify that the caret not moved
                Plat.Inst.MessageBeep();
            }

            // set new selection
            doc.SetSelection(doc.AnchorIndex, nextIndex, view);
            view.ScrollToCaret();
        }
示例#14
0
    static void Main(string[] args)
    {
        //public delegate long CalcMethod(int start, int end);
        CalcMethod calc = new CalcMethod((s, e) => s + e);

        // Delegate 타입의 BeginInvoke 메서드를 호출한다.
        // 이 때문에 Calc.Cumsum 메서드는 ThreadPool의 스레드에서 실행된다.
        IAsyncResult ar = calc.BeginInvoke(1, 100, null, null);

        // BeginInvoke로 반환받은 IAsyncResult 타입의 AsyncWaitHandle 속성은 EventWaitHandle 타입이다.
        // AsyncWaitHandle 객체는 스레드 풀에서 실행된 Calc.Sum의 동작이 완료됐을 때 Signal 상태로 바뀐다.
        // 따라서 아래의 호출은 Calc.Cumsum 메서드 수행이 완료될 때까지 현재 스레드를 대기시킨다.
        ar.AsyncWaitHandle.WaitOne();

        // Calc.Cumsum의 반환값을 얻기 위해 EndInvoke 메서드를 호출한다.
        // 반환값이 없어도 EndInvoke는 반드시 호출하는 것을 권장한다.
        long result = calc.EndInvoke(ar);

        Console.WriteLine(result);
    }
示例#15
0
        /// <summary>
        /// Moves caret to the index where the specified method calculates.
        /// </summary>
        public static void MoveCaret(CalcMethod calculator, IUserInterface ui)
        {
            var doc  = ui.Document;
            var view = ui.View as IViewInternal;

            int nextIndex = calculator(view);

            if (nextIndex == doc.CaretIndex)
            {
                // notify that the caret not moved
                Plat.Inst.MessageBeep();
            }
            else
            {
                // set new selection and scroll to caret
                doc.SetSelection(nextIndex, nextIndex);
                ui.SelectionMode = TextDataType.Normal;
            }
            view.ScrollToCaret();
        }
示例#16
0
 /// <summary>
 /// Возвращает реализацию алгоритма по заданному методу расчета
 /// </summary>
 /// <param name="methodType"></param>
 /// <returns></returns>
 private AbstractCalculationMethod GetImplementation(CalcMethod methodType)
 {
     return(CalcMethods.Where(x => x.MethodType == methodType).FirstOrDefault());
 }
 public ColLoadCaseDirection(ColLoadCase lc, Axis direction, CalcMethod calcMethod)
 {
     _lc        = lc;
     _direction = direction;
     CalcMethod = calcMethod;
 }