Exemple #1
0
        void Compute(bool isInput, string msg)
        {
            if (isInput)
            {
                currentState = CalcState.Compute;

                if (operation.Length > 0)
                {
                    Calculate();
                    resultDelegate(resultNumber);
                }
                else
                {
                    resultNumber = tempNumber;
                }

                tempNumber = "";
                operation  = msg;
            }
            else
            {
                if (Rules.IsDigit(msg))
                {
                    AccumulateDigits(true, msg);
                }
            }
        }
Exemple #2
0
 void AccumulateDigits(string msg, bool isInput)
 {
     if (isInput)
     {
         calcState   = CalcState.AccumulateDigits;
         tempNumber += msg;
         changeTextDelegate.Invoke(tempNumber);
     }
     else
     {
         if (Rules.IsDigit(msg))
         {
             AccumulateDigits(msg, true);
         }
         else if (Rules.IsOperation(msg))
         {
             Operation(msg, true);
         }
         else if (Rules.IsResult(msg))
         {
             Result(msg, true);
         }
         else if (Rules.IsQuickOperation(msg))
         {
             QuickOperation(msg, true);
         }
         else if (Rules.IsPoint(msg) && isPoint)
         {
             isPoint = false;
             AccumulateDigits(msg, true);
         }
     }
 }
Exemple #3
0
        public void Process(string text)
        {
            if (text == "C")
            {
                tempNumber = "";
                resNumber  = "";
                operation  = "";
                calcState  = CalcState.ZERO;
                displayMsg("0");
            }
            switch (calcState)
            {
            case CalcState.ZERO:
                Zero(false, text);
                break;

            case CalcState.AC_DGTS:
                AccumulateDigits(false, text);
                break;

            case CalcState.AC_DGTS_WDCML:
                AccumulateDigitsWithDecimal(false, text);
                break;

            case CalcState.COMPUTE:
                Compute(false, text);
                break;

            case CalcState.RESULT:
                Result(false, text);
                break;
            }
        }
Exemple #4
0
        private void _calcButton_Click(object sender, System.EventArgs e)
        {
            switch (_state)
            {
            case CalcState.Pending:

                _state           = CalcState.Calculating;
                _calcButton.Text = "Cancel";

                CalcPiDelegate calcPi = new CalcPiDelegate(CalcPi);
                calcPi.BeginInvoke((int)_digits.Value, null, null);

                break;

            case CalcState.Calculating:

                _state = CalcState.Canceled;
                _calcButton.Enabled = false;

                break;

            case CalcState.Canceled:

                Debug.Assert(false);

                break;
            }
        }
Exemple #5
0
 private void Result(bool isInput, string msg)
 {
     if (isInput)
     {
         calcState = CalcState.RESULT;
         Calculate();
         displayMsg(resNumber);
     }
     else
     {
         if (Rules.IsZero(msg))
         {
             Zero(true, msg);
         }
         else if (Rules.IsNonZeroDigit(msg))
         {
             tempNumber = "";
             operation  = "";
             AccumulateDigits(true, msg);
         }
         else if (Rules.IsOperator(msg))
         {
             operation  = "";
             tempNumber = resNumber;
             Compute(true, msg);
         }
         else if (Rules.IsEqualSign(msg))
         {
             Calculate();
             displayMsg(resNumber);
         }
     }
 }
        private void DoSomethingTakingALongTime()
        {
            switch (state)
            {
            case CalcState.Pending:
                // Start a new calculation
                state      = CalcState.Calculating;
                btnDo.Text = "Cancel";

                pbResult.Maximum = Loops;
                ShowProgress(InitValue);
                Action <int> action = CalcSth;
                action.BeginInvoke(Loops, null, null);
                break;

            case CalcState.Calculating:
                // Cancel a running calculation
                state         = CalcState.Canceled;
                btnDo.Enabled = false;
                break;

            case CalcState.Canceled:
                Debug.Assert(false);
                break;

            default:
                throw new Exception("Invalid state.");
            }
        }
Exemple #7
0
        public void ApplySublimit(CalcState calcState)
        {
            double n = calcState.S - calcState.D;
            double a = Math.Min(this.m_nsl.value, n);

            calcState.X = Math.Max(calcState.X, (n - a));
        }
Exemple #8
0
 void QuickOperation(string msg, bool isInput)
 {
     if (isInput)
     {
         if (resultNumber != "")
         {
             tempNumber   = resultNumber;
             resultNumber = "";
         }
         isPoint   = true;
         calcState = CalcState.QuickOperation;
         operation = msg;
         QuickCalulation();
         changeTextDelegate(tempNumber);
     }
     else
     {
         if (Rules.IsNonZeroDigit(msg))
         {
             tempNumber   = "";
             resultNumber = "";
             changeTextDelegate.Invoke(tempNumber);
             AccumulateDigits(msg, true);
         }
         else if (Rules.IsOperation(msg))
         {
             Operation(msg, true);
         }
         else if (Rules.IsQuickOperation(msg))
         {
             QuickOperation(msg, true);
         }
     }
 }
Exemple #9
0
        void Compute(bool isInput, string msg)
        {
            if (isInput || operation == "f")
            {
                calcState = CalcState.Compute;

                if (operation.Length > 0)
                {
                    Calculate();
                    displayMsg(resultNumber);
                }
                else
                {
                    resultNumber = tempNumber;
                }

                tempNumber = "";
                operation  = msg;
            }
            else
            {
                if (Rules.IsDigit(msg))
                {
                    AccumulateDigit(true, msg);
                }
            }
        }
Exemple #10
0
        void AccumulateDigit(bool isInput, string msg) //Накапливать цифру
        {
            if (isInput)
            {
                calcState   = CalcState.AccumulateDigit;
                tempNumber += msg;      //tempnumber->entered msg
                displayMsg(tempNumber); //Delegate is used
            }
            else
            {
                if (Rules.IsDigit(msg))         //digit
                {
                    AccumulateDigit(true, msg); //Накапливать цифру
                }
                else if (Rules.IsMonoOperator(msg))
                {
                    CalculateMonoOperation(msg);
                }

                /*else if (Rules.AdditionalOperator(msg))
                 * {
                 *  CalculateAdditionalOperator(msg);
                 * }*/
                else if (Rules.IsOperator(msg))//+,- etc
                {
                    Compute(true, msg);
                }
                else if (Rules.IsEqualSign(msg))
                {
                    Result(true, msg);
                }
            }
        }
Exemple #11
0
 private void ActionClick(CalcState state)
 {
     if (output.Text.Contains('\n'))
     {
         output.Text = "Неверные данные!";
         WriteState  = CalcState.NeedToClear;
         return;
     }
     try
     {
         if (state == CalcState.Not)
         {
             output.Text = Calculator.Not(Convert.ToInt32(output.Text));
             WriteState  = CalcState.NeedToClear;
             return;
         }
         a           = Convert.ToInt32(output.Text);
         ActionState = state;
         WriteState  = CalcState.NeedToClear;
     }
     catch
     {
         output.Text = "Неверные данные!";
         WriteState  = CalcState.NeedToClear;
         return;
     }
 }
Exemple #12
0
        void Compute(bool isInput, string msg)
        {
            if (isInput)
            {
                calcState = CalcState.Compute;

                if (operation.Length > 0)
                {
                    Calculate(); // method that do calculation
                    displayMsg(resultNumber);
                }
                else
                {
                    resultNumber = tempNumber; //Просто выводит цифру
                }

                tempNumber = "";
                operation  = msg;
            }
            else
            {
                if (Rules.IsDigit(msg))
                {
                    AccumulateDigit(true, msg);
                }
            }
        }
Exemple #13
0
 void AccumulateDigitsWithDecimal(string msg, bool isInput)
 {
     if (isInput)
     {
         calcState   = CalcState.AccumulateDigitsWithDecimal;
         tempNumber += msg;
         changeTextDelegate.Invoke(tempNumber);
     }
     else
     {
         if (Rules.IsDigit(msg))
         {
             AccumulateDigitsWithDecimal(msg, true);
         }
         else if (Rules.IsOperation(msg) || Rules.IsOneOperation(msg) || Rules.IsPlusMinus(msg) || Rules.IsDelete(msg))
         {
             Compute(msg, true);
         }
         else if (Rules.IsEqualSign(msg))
         {
             Result(msg, true);
         }
         else if (Rules.IsC(msg))
         {
             Zero(msg, true);
         }
         else if (Rules.IsCE(msg))
         {
             tempNumber = "";
             AccumulateDigits(tempNumber, true);
         }
     }
 }
Exemple #14
0
 void Zero(string msg, bool isInput)
 {
     if (isInput)
     {
         calcState    = CalcState.Zero;
         tempNumber   = "";
         msg          = "";
         resultNumber = "";
         operation    = "";
         changeTextDelegate("0");
     }
     else
     {
         if (Rules.IsNonZeroDigit(msg))
         {
             AccumulateDigits(msg, true);
         }
         else if (Rules.IsSeparator(msg))
         {
             AccumulateDigitsWithDecimal(msg, true);
         }
         else if (Rules.IsOperation(msg) || Rules.IsOneOperation(msg) || Rules.IsPlusMinus(msg) || Rules.IsDelete(msg))
         {
             tempNumber = "0";
             Compute(msg, true);
         }
     }
 }
 private void GenerateImage()
 {
   if (rbMulti.Checked)
   {
     List<ManualResetEvent> events = new List<ManualResetEvent>();
     int offset = 0;
     foreach (muParser.Parser p in m_parserImg)
     {
       CalcState cs = new CalcState(new ManualResetEvent(false), p, offset);
       ++offset;
       events.Add(cs.Reset);
       ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateMultiThread), cs);
     }
     WaitHandle.WaitAll(events.ToArray());
   }
   else if (rbNormal.Checked)
   {
     CalculateSingleThread();
   }
   else if (rbMultiReturn.Checked)
   {
     CalculateMultiReturn();
   }
   else
   {
     CalculateBulk();
   }
 }
Exemple #16
0
 void Result(bool isInput, string msg)
 {
     if (isInput)
     {
         calcState = CalcState.Result;
         Calculate();
         displayMsg(resultNumber);
     }
     else
     {
         if (Rules.IsNonZeroDigit(msg))
         {
             AccumulateDigit(true, msg);
         }
         else if (Rules.IsZero(msg))
         {
             Zero(true, msg);
         }
         else if (Rules.IsOperator(msg))
         {
             operation  = "";
             tempNumber = resultNumber;
             Compute(true, msg);
         }
     }
 }
    /// <summary>
    /// Course correction calculations are Async on worker threads. In order to allow the rest of the game to
    /// run, the update runs as a state machine and should be polled periodically to check when the full
    /// correction calculation is done.
    ///
    /// A calculation is started with CalculationStart() and then this routine is polled until it returns true.
    ///
    /// Results are retrieved with GetCorrection();
    ///
    /// </summary>
    /// <returns></returns>
    public bool CalculationUpdate()
    {
        switch (calcState)
        {
        case CalcState.INITIAL_THREE:
            if (threadsPending == 0)
            {
                Debug.Log(CorrectionsLog());
                // results are in, figure out which direction is working and start a new thread to
                // see if we are not within the desired accuracy
                if (CorrectionInitialEstimate())
                {
                    calcState = CalcState.REFINING;
                }
                else
                {
                    // Correction estimation failed. Use the 0 correction path
                    correctionFinal = correctionData[1];
                    calcState       = CalcState.DONE;
                }
            }
            break;

        case CalcState.REFINING:
            if (threadsPending == 0)
            {
                Debug.Log(CorrectionsLog());
                // TODO: check the result, refine if necessary
                calcState = CalcState.DO_CALLBACK;
                Debug.LogFormat("correction={0} gives distance={1} for target={2}",
                                correctionData[0].correction, correctionData[0].distance, targetDistance);
                correctionFinal = correctionData[0];
            }
            break;

        case CalcState.CLOSEST_APPROACH:
            if (threadsPending == 0)
            {
                calcState = CalcState.DO_CALLBACK;
            }
            break;

        case CalcState.NOT_STARTED:
        case CalcState.DONE:
        case CalcState.DO_CALLBACK:
            break;

        default:
            Debug.LogError("Unsupported state");
            break;
        }

        if (calcState == CalcState.DO_CALLBACK)
        {
            calcCallback(this);
            calcState = CalcState.DONE;
        }

        return(calcState == CalcState.DONE);
    }
      private void CalculateMultiThread(object s)
      {
        CalcState cs = s as CalcState;
        double mult = 2 * Math.PI / 800;
        int index = 0;

        // Set up parser variables
        Parser p = cs.Parser;
        ParserVariable x = new ParserVariable(0);
        ParserVariable y = new ParserVariable(0);
        p.DefineVar("x", x);
        p.DefineVar("y", y);

        // Do the actual looping for a single colorplane
        double v;
        for (int yi = 0; yi < pbImage.Height; ++yi)
        {
          y.Value = (yi - 400) * mult;
          for (int xi = 0; xi < pbImage.Width; ++xi)
          {
            x.Value = (xi - 400) * mult;

            v = GetColorComponent(p.Eval());
            m_data[index + cs.Offset] = (byte)(v * 255);
            index += 3;
          }
        }

        cs.Reset.Set();
      }
Exemple #19
0
 public void Operation(String msg, bool input)
 {
     if (input)
     {
         state = CalcState.Operation;
         if (operation.Length > 0)
         {
             Calculate();
             textDelegate.Invoke(resultNumber);
         }
         else
         {
             resultNumber = tempNumber;
         }
         tempNumber = "";
         operation  = msg;
     }
     else
     {
         if (Rules.IsDigit(msg))
         {
             AccumulateDigit(msg, true);
         }
     }
 }
Exemple #20
0
        private void SetFormState(CalcState newstate)
        {
            switch (newstate)
            {
            case CalcState.Calculating:
                // Allow canceling
                this.state                = newstate;
                this.btnCalculate.Text    = "Cancel";
                this.btnCalculate.Enabled = true;
                this.progressBar1.Visible = true;
                this.statusBarPanel1.Text = "Calculating ...";
                break;

            case CalcState.Canceled:
                this.state = newstate;
                this.btnCalculate.Enabled = false;
                break;

            case CalcState.Pending:
            default:
                this.state                = newstate;
                this.btnCalculate.Text    = "Calculate";
                this.btnCalculate.Enabled = true;
                this.progressBar1.Visible = false;
                this.statusBarPanel1.Text = "";
                break;
            }
        }
Exemple #21
0
      private void CalculateMultiThread(object s)
      {
        CalcState cs = s as CalcState;
        float mult = 2 * (float)Math.PI / (float)256.0;
        int index = 0;

        // Set up parser variables
        Parser p = cs.Parser;
        ParserVariable x = new ParserVariable(0);
        ParserVariable y = new ParserVariable(0);
        p.DefineVar("x", x);
        p.DefineVar("y", y);

        Parser.CompiledFunDelegate fun = p.Compile();

        // Do the actual looping for a single colorplane
        float v;
        for (int yi = 0; yi < pbImage.Height; ++yi)
        {
          y.Value = (yi - 128) * mult;
          for (int xi = 0; xi < pbImage.Width; ++xi)
          {
            x.Value = (xi - 128) * mult;

            //v = GetColorComponent(p.Eval());
            v = GetColorComponent(fun());
            m_data[index + cs.Offset] = (byte)(v * 255);
            index += 3;
          }
        }

        cs.Reset.Set();
      }
Exemple #22
0
 public void Clear()
 {
     Status   = CalcState.csFirst;
     Number   = "0";
     Sign     = ' ';
     Operator = '=';
 }
Exemple #23
0
 void Operation(string msg, bool isInput)
 {
     if (isInput)
     {
         calcState = CalcState.Operation;
         if (operation.Length != 0)
         {
             PerformCalculation();
             changeTextDelegate(resultNumber);
         }
         if (resultNumber == "")
         {
             resultNumber = tempNumber;
         }
         isPoint   = true;
         operation = msg;
         //changeTextDelegate.Invoke(msg);
         tempNumber = "";
     }
     else
     {
         if (Rules.IsDigit(msg))
         {
             AccumulateDigits(msg, true);
         }
         else if (Rules.IsOperation(msg))
         {
             operation = "";
             Operation(msg, true);
         }
     }
 }
Exemple #24
0
        public override void ApplyTermObject(CalcState calcState)
        {
            double n = calcState.S - calcState.D;
            double a = Math.Min(this.m_nsl.value, n);

            calcState.X = Math.Max(calcState.X, (n - a));
        }
Exemple #25
0
 void Result(string msg, bool isInput)
 {
     if (isInput)
     {
         isPoint   = true;
         calcState = CalcState.Result;
         PerformCalculation();
         operation = "";
         changeTextDelegate.Invoke(resultNumber);
     }
     else
     {
         if (Rules.IsOperation(msg))
         {
             Operation(msg, true);
         }
         else if (Rules.IsNonZeroDigit(msg))
         {
             tempNumber   = "";
             resultNumber = "";
             changeTextDelegate.Invoke(tempNumber);
             AccumulateDigits(msg, true);
         }
         else if (Rules.IsResult(msg))
         {
             Result(msg, true);
         }
         else if (Rules.IsQuickOperation(msg))
         {
             QuickOperation(msg, true);
         }
     }
 }
Exemple #26
0
 public Calculator(ChangeTextDelegate changeText)
 {
     this.CTD       = changeText;
     this.tempN     = "";
     this.result    = "";
     this.operation = "";
     this.state     = CalcState.Zero;
 }
Exemple #27
0
 public Calculator(ChangeTextDelegate textDelegate)
 {
     state             = CalcState.Zero;
     this.textDelegate = textDelegate;
     tempNumber        = "";
     resultNumber      = "";
     operation         = "";
 }
Exemple #28
0
 public Calculator(TextBox textBox)
 {
     this.textBox = textBox;
     tempNumber   = 0;
     resultNumber = 0;
     operation    = "";
     state        = CalcState.None;
 }
        public CalcForm()
        {
            InitializeComponent();

            history = new List <KeyValuePair <string, DateTime> >();
            val     = new Operation("", history);
            cs      = new CalcState(val, val.result);
        }
Exemple #30
0
 public void Equal_Clicked(object sender, EventArgs e)
 {
     if (state != CalcState.Equal)
     {
         tempNumber = int.Parse(textBox.Text);
     }
     Calculate();
     state = CalcState.Equal;
 }
Exemple #31
0
        void ShowProgress(string pi, int totalDigits, int digitsSoFar, out bool cancel)
        {
            if (_pi.InvokeRequired == false) // UI thread running
            {
                cancel = _calcState == CalcState.Canceled;
                if (cancel)
                {
                    _calcState = CalcState.Pending;
                    _calcButton.Enabled = true;
                    _calcButton.Text = "Calc";
                }
                _pi.Text = pi;
                _piProgress.Maximum = totalDigits;
                _piProgress.Value = digitsSoFar;
            }
            else
            {
                ShowProcessDelegate del = new ShowProcessDelegate(ShowProgress);
                object[] args = { pi, totalDigits, digitsSoFar, null };
                this.Invoke(del, args);
                cancel = (bool)args[3];

            }
        }
Exemple #32
0
        private void _calcButton_Click(object sender, System.EventArgs e)
        {
            if(_calcState == CalcState.Pending)
            {
                _calcState = CalcState.Calcultating;
                _calcButton.Text = "Cancel";

                CalcPiDelegate c = new CalcPiDelegate(calcWork);
                c.BeginInvoke((int)_digits.Value, null, null);
            }
            else if(_calcState == CalcState.Calcultating)
            {
                _calcState = CalcState.Canceled;
                _calcButton.Enabled = false;
            }else if(_calcState == CalcState.Canceled)
            {
                Debug.Assert(false); //shouldnt be able to press button if it is disabled
            }
        }