Exemplo n.º 1
0
 // Sets current function in response to function key press
 private void setCurrentFunc(string s, FuncButton btn)
 {
     if (currentFunc != null)
     {
         try
         {
             previousValue      = (double)currentFunc.DynamicInvoke(previousValue, currentValue);
             lbCalcHistory.Text = previousValue.ToString() + " " + s;
             tbOutput.Text      = "0";
         }
         catch (TargetInvocationException ex)
         {
             if (ex.InnerException is DivideByZeroException)
             {
                 lbCalcHistory.Text += (" " + tbOutput.Text);
                 tbOutput.Text       = "INF";
                 previousValue       = 0;
                 currentFunc         = null;
             }
         }
     }
     else
     {
         previousValue      = currentValue;
         lbCalcHistory.Text = tbOutput.Text + " " + s;
         tbOutput.Text      = "0";
     }
     currentFunc    = (Func <double, double, double>)btn.func;
     newCalcStarted = false;
 }
Exemplo n.º 2
0
        // Add advanced functions and corresponding callbacks
        private void AddAdvancedFunctions()
        {
            int rowIdx = 0;

            foreach (string s in advOpList)
            {
                FuncButton btn = new FuncButton(advOps[s]);
                tlpAdvanced.Controls.Add(btn, 0, rowIdx);
                btn.Text = s;
                rowIdx++;
                btn.Click += (sender, e) =>
                {
                    if (Double.TryParse(tbOutput.Text, out currentValue))
                    {
                        if (currentFunc != null)
                        {
                            try
                            {
                                currentValue = (double)currentFunc.DynamicInvoke(previousValue, currentValue);
                            }
                            catch (TargetInvocationException ex)
                            {
                                if (ex.InnerException is DivideByZeroException)
                                {
                                    lbCalcHistory.Text += (" " + tbOutput.Text);
                                    tbOutput.Text       = "INF";
                                    previousValue       = 0;
                                    currentFunc         = null;
                                    return;
                                }
                            }
                        }
                        if (btn.Text == "1/x")
                        {
                            lbCalcHistory.Text = ("1/" + currentValue);
                        }
                        // square
                        else if (btn.Text == "x\xB2")
                        {
                            lbCalcHistory.Text = String.Format("{0} * {0}", currentValue.ToString());
                        }
                        else
                        {
                            lbCalcHistory.Text = String.Format("{0}({1})", btn.Text, currentValue);
                        }

                        try
                        {
                            currentValue  = (double)btn.func.DynamicInvoke(currentValue);
                            tbOutput.Text = currentValue.ToString();
                        }
                        catch (TargetInvocationException ex)
                        {
                            if (ex.InnerException is DivideByZeroException)
                            {
                                lbCalcHistory.Text += (" " + tbOutput.Text);
                                tbOutput.Text       = "INF";
                                previousValue       = 0;
                                currentFunc         = null;
                                return;
                            }
                        }
                        newCalcStarted = true;
                        currentFunc    = null;
                    }
                };
            }
        }
Exemplo n.º 3
0
        // Add arithmetical functions, erase all and equals buttons and corresponding callbacks
        private void AddArithmeticFunctions()
        {
            CalcButton eraseAll = new CalcButton();

            eraseAll.key = keyDict["Delete"];
            keysToButtons[keyDict["Delete"]] = eraseAll;
            tlpArithmetics.Controls.Add(eraseAll, 0, 0);
            eraseAll.Text   = "CE";
            eraseAll.Click += (sender, e) =>
            {
                currentFunc        = null;
                previousValue      = 0;
                currentValue       = 0;
                lbCalcHistory.Text = "";
                newCalcStarted     = true;
                tbOutput.Text      = "0";
            };

            int rowIdx = 1;

            foreach (string s in arOpsList)
            {
                FuncButton btn = new FuncButton(arOps[s]);
                if (keyDict.Keys.Contains(btn.func.Method.Name))
                {
                    Keys k = keyDict[btn.func.Method.Name];
                    keysToButtons[k] = btn;
                }
                tlpArithmetics.Controls.Add(btn, 0, rowIdx);
                btn.Text = s;
                rowIdx++;
                // The minus button is to be handled differently from the others to allow entry
                // of negative numbers
                if (s == "-")
                {
                    btn.Click += (sender, e) =>
                    {
                        // user entering negative number
                        if (tbOutput.Text == "0" || newCalcStarted)
                        {
                            if (newCalcStarted)
                            {
                                lbCalcHistory.Text = "";
                            }
                            tbOutput.Text  = "-";
                            newCalcStarted = false;
                        }
                        else if (Double.TryParse(tbOutput.Text, out currentValue))
                        {
                            //if (!(Double.IsNaN(currentValue))) performOp(s, btn);
                            setCurrentFunc(s, btn);
                        }
                    };
                }
                else
                {
                    btn.Click += (sender, e) =>
                    {
                        if (Double.TryParse(tbOutput.Text, out currentValue))
                        {
                            //if (!(Double.IsNaN(currentValue))) performOp(s, btn);
                            setCurrentFunc(s, btn);
                        }
                    };
                }
            }
            CalcButton equals = new CalcButton();

            tlpArithmetics.Controls.Add(equals, 0, rowIdx);
            equals.key = keyDict["Return"];
            keysToButtons[keyDict["Return"]] = equals;
            equals.Text   = "=";
            equals.Click += (sender, e) =>
            {
                if (currentFunc != null)
                {
                    if (Double.TryParse(tbOutput.Text, out currentValue))
                    {
                        try
                        {
                            double result = (double)currentFunc.DynamicInvoke(previousValue, currentValue);
                            lbCalcHistory.Text += (" " + tbOutput.Text);
                            tbOutput.Text       = result.ToString();
                            previousValue       = result;
                            currentFunc         = null;
                        }
                        catch (TargetInvocationException ex)
                        {
                            if (ex.InnerException is DivideByZeroException)
                            {
                                lbCalcHistory.Text += (" " + tbOutput.Text);
                                tbOutput.Text       = "INF";
                                previousValue       = 0;
                                currentFunc         = null;
                            }
                        }
                        newCalcStarted = true;
                    }
                }
            };
        }