Example #1
0
 public static void Calculate(string cmd)
 {
     try
     {
         Complex res = 0.0;
         string op = parser.DeCleanUp(cmd).Trim();
         if (op.Contains("="))
         {
             if (!op.StartsWith("="))
             {
                 var splash = op.Split('=');
                 if (splash.Count() == 2)
                 {
                     string varname = splash[0];
                     if (Constant.cs.Any(x => x.Equals(varname, StringComparison.OrdinalIgnoreCase)))
                         throw new ParseException(op, 0,
                             $"Une constante nommée '{varname}' existe déjà.");
                     if (Funcs.Any(x => x.Equals(varname, StringComparison.OrdinalIgnoreCase)))
                         throw new ParseException(op, 0,
                             $"Une fonction nommée '{varname}' existe déjà.");
                     var fullop = splash[1];
                     Parallel.Invoke(() => res = parser.Calculate(fullop, Vars.Values.ToArray()));
                     if (Vars.ContainsKey(varname)) Vars[varname] = new Var(varname, res);
                     else Vars.Add(varname, new Var(varname, res));
                     Console.WriteLine("  " + res);
                 }
             }
         }
         else
         {
             Parallel.Invoke(() => res = parser.Calculate(op, Vars.Values.ToArray()));
             Console.WriteLine("  " + parser.CleanUp(res.ToString()).Replace("÷", "/"));
             oldop = parser.CleanUp(cmd);
             if (!Complex.IsNaN(res) && !Complex.IsInfinity(res) && !Complex.IsIndeterminate(res))
                 Vars["ans"] = new Var("ans", res);
             else Vars["ans"] = new Var("ans", 0);
         }
     }
     catch (Exception e)
     {
         Throw(e);
     }
 }
Example #2
0
 private void Calculate()
 {
     try
     {
         Complex res = 0.0;
         string op = parser.DeCleanUp(txtOperation.Text).Trim();
         if (op.Contains("="))
         {
             if (!op.StartsWith("="))
             {
                 var splash = op.Split('=');
                 if (splash.Count() == 2)
                 {
                     string varname = splash[0];
                     if(Constant.cs.Any(x => x.Equals(varname, StringComparison.OrdinalIgnoreCase)))
                         throw new ParseException(op, 0, $"Une constante nommée '{varname}' existe déjà.");
                     if (Funcs.Any(x => x.Equals(varname, StringComparison.OrdinalIgnoreCase)))
                         throw new ParseException(op, 0, $"Une fonction nommée '{varname}' existe déjà.");
                     var fullop = splash[1];
                     Stopwatch sw = null;
                     if (Stopwatch)
                     {
                         sw = new Stopwatch();
                         sw.Start();
                     }
                     Parallel.Invoke(() => res = parser.Calculate(fullop, Vars.Values.ToArray()));
                     if (Stopwatch)
                     {
                         sw.Stop();
                         MessageBox.Show(sw.Elapsed.ToString());
                     }
                     if (Vars.ContainsKey(varname)) Vars[varname] = new Var(varname, res);
                     else Vars.Add(varname, new Var(varname, res));
                     txtOperation.ItemsSource = Funcs;
                     txtResult.Text = res.ToString();
                     lblOldOp.Text = parser.CleanUp(txtOperation.Text);
                     txtOperation.Text = "";
                 }
             }
         }
         else
         {
             Stopwatch sw = null;
             if (Stopwatch)
             {
                 sw = new Stopwatch();
                 sw.Start();
             }
             Parallel.Invoke(() => res = parser.Calculate(op, Vars.Values.ToArray()));
             if (Stopwatch)
             {
                 sw.Stop();
                 MessageBox.Show(sw.Elapsed.ToString());
             }
             txtResult.Text = parser.CleanUp(res.ToString()).Replace("÷", "/");
             lblOldOp.Text = parser.CleanUp(txtOperation.Text);
             txtOperation.Text = "";
             if (!Complex.IsNaN(res) && !Complex.IsInfinity(res) && !Complex.IsIndeterminate(res))
                 Vars["ans"] = new Var("ans", res);
             else Vars["ans"] = new Var("ans", 0);
             txtOperation.ItemsSource = Funcs;
         }
         FormattedText formattedText = new FormattedText(txtResult.Text, CultureInfo.CurrentCulture,
             FlowDirection.LeftToRight,
             new Typeface(txtResult.FontFamily, txtResult.FontStyle, txtResult.FontWeight, txtResult.FontStretch),
             txtResult.FontSize, Brushes.Black, null, TextOptions.GetTextFormattingMode(txtResult));
         double wd = txtResult.Width - formattedText.Width - 30;
         if (wd <= 0) wd = 78;
         lblOldOp.Width = wd;
     }
     catch (Exception e)
     {
         e.Throw();
     }
 }