public void Calculate() { try { org.mariuszgromada.math.mxparser.Expression calculation; calculation = new org.mariuszgromada.math.mxparser.Expression(FormulaLbl); //5*60 Result = calculation.calculate().ToString(); //300 } catch (Exception e) { } }
private void Entrée_Click(object sender, RoutedEventArgs e) { // gérer les caractères spéciaux resultatAffiche = true; string calculUtilisateur = mvm.NbInUser; string calcul = mvm.NbInUser; calcul = calcul.Replace(" ", ""); calcul = calcul.Replace("x", "*"); calcul = calcul.Replace("√", "sqrt"); calcul = calcul.Replace("²", "^2"); calcul = calcul.Replace("%", "mod("); Console.WriteLine(calcul); org.mariuszgromada.math.mxparser.Expression exp = new org.mariuszgromada.math.mxparser.Expression(calcul); string resultat = Convert.ToString(exp.calculate()); if (resultat.Equals("NaN")) { resultat = "Erreur de syntaxe"; // resultat = "Erreur de syntaxe dans le calcul :\n" + calcul; } else { historique.Add(new KeyValuePair <string, string>(calculUtilisateur, resultat)); mvm.AffichagesHistoriqueCalcAD = historiqueToString(historique)[0]; mvm.AffichagesHistoriqueResAD = historiqueToString(historique)[1]; mvm.AffichagesHistoriqueCalcD = historiqueToString(historique)[2]; mvm.AffichagesHistoriqueResD = historiqueToString(historique)[3]; Console.WriteLine(mvm.historiqueObservable); mvm.historiqueObservable.Add(calculUtilisateur); mvm.historiqueObservable.Add(resultat); // Console.WriteLine(historique); } resultat = resultat.Replace(",", "."); mvm.StrAffichageTbx = resultat; // Console.WriteLine(calcul); }
private static async Task CmdMath(DiscordCommandArguments args) { string math = args["math"]; org.mariuszgromada.math.mxparser.Expression expr = new org.mariuszgromada.math.mxparser.Expression(math); StringBuilder sb = new StringBuilder(); if (!expr.checkSyntax()) { sb.AppendLine($"{args.User.Mention} - Error in mathematical expression:"); sb.AppendLine(expr.getErrorMessage()); } else { sb.AppendLine($"{args.User.Mention} - {math} = **{expr.calculate()}**"); } await args.Channel.SendMessage(sb.ToString()); }
/// <summary> /// Evaluate the Result and display text /// </summary> /// <returns>The history to display if length is 0 should be discarded</returns> public String EvaluateResult() { string evaluatedText = _display.Text; //have to use fullnamespace as it conflicts with another windows defined expression:( org.mariuszgromada.math.mxparser.Expression expression = new org.mariuszgromada.math.mxparser.Expression(evaluatedText); double result = expression.calculate(); _OverWrite = true; if (Double.IsNaN(result)) { _display.Text = "Error"; } else { _display.Text = result.ToString(); return(evaluatedText + "=" + result.ToString()); } return(""); }
public Task solve([Remainder] string equation) { org.mariuszgromada.math.mxparser.Expression expression = new org.mariuszgromada.math.mxparser.Expression(equation); return(Context.Channel.SendMessageAsync(expression.calculate().ToString())); }
static void FormulaGenerator() { char[] chars = new char[] { '+', '-', '*', '/', '^' }; char[] letters = new char[] { 'x', 'y', 'z' }; string generatedFormula = ""; string generateFormulaWithLetters = ""; StringBuilder build = new StringBuilder(); Random r; string result = ""; string resultWithLetter = ""; for (int i = 0; i < 10; i++) { r = new Random(); generatedFormula = r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + "(" + r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString() + ")" + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString(); generateFormulaWithLetters = r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + letters[r.Next(letters.Length)] + chars[r.Next(chars.Length)] + "(" + r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString() + ")" + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString() + chars[r.Next(chars.Length)] + r.Next(1, 100).ToString(); try { org.mariuszgromada.math.mxparser.Expression formula = new org.mariuszgromada.math.mxparser.Expression(generatedFormula); org.mariuszgromada.math.mxparser.Expression formulaWithLetters = new org.mariuszgromada.math.mxparser.Expression(generateFormulaWithLetters); result = formula.calculate().ToString(); resultWithLetter = formulaWithLetters.calculate().ToString(); //if (!File.Exists(path)) //{ // File.Create(path).Dispose(); // using (TextWriter tw = new StreamWriter(path)) // { // tw.WriteLine("The very first line!"); // tw.Close(); // } //} //else if (File.Exists(path)) //{ FileStream fs = new FileStream("Results.txt", FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("Number " + i.ToString()); sw.WriteLine("\n" + "Formula : " + generatedFormula); sw.WriteLine("Result : " + result + "\n"); sw.WriteLine(); sw.Close(); fs.Close(); //} Console.WriteLine("Number " + i.ToString() + "\n" + "Formula : " + generatedFormula + "\n" + "Result : " + result + "\n" + "Condition " + true.ToString()); Console.WriteLine("Number " + i.ToString() + "\n" + "Formula : " + generateFormulaWithLetters + "\n" + "Result : " + resultWithLetter + "\n" + "Condition " + true.ToString()); } catch (Exception e) { generateFormulaWithLetters = e.ToString() + "false"; } Thread.Sleep(1000); } }