public void InitDefaults() { this.Add("about", new StaticFunction("About", delegate(object[] ps) { return("@Calculator - a Tiny Expression Evaluator v1.0\r\nby Herre Kuijpers - Copyright © 2011 under the CPOL license"); }, 0, 0)); this.Add("help", new StaticFunction("Help", Help, 0, 0)); // high precision functions this.Add("abs", new StaticFunction("Abs", delegate(object[] ps) { return(Math.Abs(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("acos", new StaticFunction("Acos", delegate(object[] ps) { return(Math.Acos(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("asin", new StaticFunction("Asin", delegate(object[] ps) { return(Math.Asin(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("atan", new StaticFunction("Atan", delegate(object[] ps) { return(Math.Atan(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("atan2", new StaticFunction("Atan2", delegate(object[] ps) { return(Math.Atan2(Convert.ToDouble(ps[0]), Convert.ToDouble(ps[1]))); }, 2, 2)); this.Add("ceiling", new StaticFunction("Ceiling", delegate(object[] ps) { return(Math.Ceiling(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("cos", new StaticFunction("Cos", delegate(object[] ps) { return(Math.Cos(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("cosh", new StaticFunction("Cosh", delegate(object[] ps) { return(Math.Cosh(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("exp", new StaticFunction("Exp", delegate(object[] ps) { return(Math.Exp(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("int", new StaticFunction("int", delegate(object[] ps) { return((int)Math.Floor(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("fact", new StaticFunction("Fact", Fact, 1, 1)); // factorials 1*2*3*4... this.Add("floor", new StaticFunction("Floor", delegate(object[] ps) { return(Math.Floor(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("log", new StaticFunction("Log", Log, 1, 2)); // log allows 1 or 2 parameters this.Add("ln", new StaticFunction("Ln", delegate(object[] ps) { return(Math.Log(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("pow", new StaticFunction("Pow", delegate(object[] ps) { if ( (Convert.ToInt64(ps[0]) != Convert.ToDouble(ps[0])) || (Convert.ToInt64(ps[1]) != Convert.ToDouble(ps[1])) ) { return(Math.Pow(Convert.ToDouble(ps[0]), Convert.ToDouble(ps[1]))); } else // use integral maths instead to avoid silent overflow { long number = Convert.ToInt64(ps[0]); long power = Convert.ToInt64(ps[1]); long result = 1L; for (long i = 0; i < power; i++) { result *= number; } return(result); } }, 2, 2)); this.Add("round", new StaticFunction("Round", delegate(object[] ps) { return(Math.Round(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("sign", new StaticFunction("Sign", delegate(object[] ps) { return(Math.Sign(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("sin", new StaticFunction("Sin", delegate(object[] ps) { return(Math.Sin(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("sinh", new StaticFunction("Sinh", delegate(object[] ps) { return(Math.Sinh(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("sqr", new StaticFunction("Sqr", delegate(object[] ps) { return(Convert.ToDouble(ps[0]) * Convert.ToDouble(ps[0])); }, 1, 1)); this.Add("sqrt", new StaticFunction("Sqrt", delegate(object[] ps) { return(Math.Sqrt(Convert.ToDouble(ps[0]))); }, 1, 1)); this.Add("trunc", new StaticFunction("Trunc", delegate(object[] ps) { return(Math.Truncate(Convert.ToDouble(ps[0]))); }, 1, 1)); // array functions this.Add("avg", new StaticFunction("Avg", Avg, 1, int.MaxValue)); this.Add("stdev", new StaticFunction("StDev", StDev, 1, int.MaxValue)); this.Add("var", new StaticFunction("Var", Var, 1, int.MaxValue)); this.Add("max", new StaticFunction("Max", Max, 1, int.MaxValue)); this.Add("median", new StaticFunction("Min", Median, 1, int.MaxValue)); this.Add("min", new StaticFunction("Min", Min, 1, int.MaxValue)); //boolean functions this.Add("not", new StaticFunction("Not", delegate(object[] ps) { return(!Convert.ToBoolean(ps[0])); }, 1, 1)); this.Add("if", new StaticFunction("If", delegate(object[] ps) { return(Convert.ToBoolean(ps[0]) ? ps[1] : ps[2]); }, 3, 3)); this.Add("and", new StaticFunction("And", delegate(object[] ps) { return(Convert.ToBoolean(ps[0]) && Convert.ToBoolean(ps[1])); }, 2, 2)); this.Add("or", new StaticFunction("Or", delegate(object[] ps) { return(Convert.ToBoolean(ps[0]) || Convert.ToBoolean(ps[1])); }, 2, 2)); // string functions this.Add("left", new StaticFunction("Left", delegate(object[] ps) { int len = Convert.ToInt32(ps[1]) < ps[0].ToString().Length ? Convert.ToInt32(ps[1]) : ps[0].ToString().Length; return(ps[0].ToString().Substring(0, len)); }, 2, 2)); this.Add("right", new StaticFunction("Right", delegate(object[] ps) { int len = Convert.ToInt32(ps[1]) < ps[0].ToString().Length ? Convert.ToInt32(ps[1]) : ps[0].ToString().Length; return(ps[0].ToString().Substring(ps[0].ToString().Length - len, len)); }, 2, 2)); this.Add("mid", new StaticFunction("Mid", delegate(object[] ps) { int idx = Convert.ToInt32(ps[1]) < ps[0].ToString().Length ? Convert.ToInt32(ps[1]) : ps[0].ToString().Length; int len = Convert.ToInt32(ps[2]) < ps[0].ToString().Length - idx ? Convert.ToInt32(ps[2]) : ps[0].ToString().Length - idx; return(ps[0].ToString().Substring(idx, len)); }, 3, 3)); this.Add("hex", new StaticFunction("Hex", delegate(object[] ps) { return(String.Format("0x{0:X}", Convert.ToInt32(ps[0].ToString()))); }, 1, 1)); this.Add("format", new StaticFunction("Format", delegate(object[] ps) { return(string.Format(ps[0].ToString(), ps[1])); }, 2, 2)); this.Add("len", new StaticFunction("Len", delegate(object[] ps) { return(Convert.ToDouble(ps[0].ToString().Length)); }, 1, 1)); this.Add("lower", new StaticFunction("Lower", delegate(object[] ps) { return(ps[0].ToString().ToLowerInvariant()); }, 1, 1)); this.Add("upper", new StaticFunction("Upper", delegate(object[] ps) { return(ps[0].ToString().ToUpperInvariant()); }, 1, 1)); this.Add("val", new StaticFunction("Val", delegate(object[] ps) { return(Convert.ToDouble(ps[0])); }, 1, 1)); }
private void enter_pressed(object sender, EventArgs e) { indicator.Text = ""; //saves calculation checker.Text = save + " " + textdisp.Text + " = "; string inputvalue = textdisp.Text; switch (operation) { case "+": textdisp.Text = (results + Double.Parse(textdisp.Text)).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "-": textdisp.Text = (results - Double.Parse(textdisp.Text)).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "÷": if (textdisp.Text == "0") { textdisp.Text = "invalid"; MessageBox.Show("invalid"); checker.Text = checker.Text + textdisp.Text; } else { textdisp.Text = (results / Double.Parse(textdisp.Text)).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); } break; case "×": textdisp.Text = (results * Double.Parse(textdisp.Text)).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "%": textdisp.Text = (results % Double.Parse(textdisp.Text)).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "^": if (results == 0 && Double.Parse(textdisp.Text) == -1) { textdisp.Text = "invalid"; checker.Text = checker.Text + textdisp.Text; } else { textdisp.Text = (Math.Pow(results, Double.Parse(textdisp.Text))).ToString(); checker.Text = checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); } break; case "√": if (results <= 0) { textdisp.Text = "invalid"; checker.Text = "√" + checker.Text + textdisp.Text; } else { textdisp.Text = (Math.Sqrt(results)).ToString(); checker.Text = "√" + checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); } break; case "^-1": if (results == 0) { textdisp.Text = "invalid"; checker.Text = checker.Text + "^-1" + " = " + textdisp.Text; } else { textdisp.Text = (1 / results).ToString(); checker.Text = checker.Text + "^-1" + Math.Round(Convert.ToDecimal(textdisp.Text), 3); } break; case "sin": inputvalue = textdisp.Text; textdisp.Text = (Math.Sin(results * (Math.PI) / 180)).ToString(); checker.Text = "sin " + inputvalue + " = " + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "cos": inputvalue = textdisp.Text; textdisp.Text = (Math.Cos(results * (Math.PI) / 180)).ToString(); checker.Text = "cos " + inputvalue + " = " + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "tan": inputvalue = textdisp.Text; textdisp.Text = (Math.Tan(results * (Math.PI) / 180)).ToString(); checker.Text = "tan " + inputvalue + " = " + Math.Round(Convert.ToDecimal(textdisp.Text), 3); break; case "ln": if (results <= 0) { textdisp.Text = "invalid"; checker.Text = "ln" + checker.Text + textdisp.Text; } else { textdisp.Text = (Math.Log(results)).ToString(); checker.Text = "ln" + checker.Text + Math.Round(Convert.ToDecimal(textdisp.Text), 3); } break; } using (MySqlConnection mysqlCon = new MySqlConnection(connectionString)) { mysqlCon.Open(); MySqlCommand cmd = new MySqlCommand("calcAdd", mysqlCon); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("_ID", ID); cmd.Parameters.AddWithValue("_calculation", checker.Text); cmd.Parameters.AddWithValue("_Calcdate", DateTime.Now.ToString("MM-dd-yyyy")); cmd.Parameters.AddWithValue("_Calctime", DateTime.Now.ToString("HH:mm:ss")); cmd.ExecuteNonQuery(); connection.Close(); } //MySqlCommand cmd = new MySqlCommand("insert into calculations (calculation)Values('" + checker.Text + "')", connection); //cmd.ExecuteNonQuery(); populategrid(); }
private void operation_without_Click(object sender, EventArgs e) { Button btnn = sender as Button; operation = btnn.Text; firstNumber = double.Parse(display.Text); if (operation == "e^x") { double t = 1; for (int i = 0; i < firstNumber; i++) { t = t * Math.E; } label1.Text = "(e)^" + firstNumber.ToString(); result = t; } if (operation == "√") { result = Math.Sqrt(firstNumber); label1.Text = "sqr(" + firstNumber.ToString() + ")"; } if (operation == "+/-") { if (firstNumber > 0) { label1.Text = "-" + firstNumber.ToString(); } if (firstNumber == 0) { } else { label1.Text = ((-1) * firstNumber).ToString(); } firstNumber = (-1) * firstNumber; result = firstNumber; } if (operation == "x!") { double c = firstNumber; double t = 1; while (c != 0) { t = t * c; c--; } label1.Text = "fact(" + firstNumber.ToString() + ")"; result = t; } if (operation == "1/x") { label1.Text = "1/" + firstNumber.ToString(); result = 1 / firstNumber; } if (operation == "sin") { label1.Text = "sin(" + firstNumber.ToString() + ")"; result = Math.Sin(firstNumber * Math.PI / 180); } if (operation == "cos") { label1.Text = "cos(" + firstNumber.ToString() + ")"; result = Math.Cos(firstNumber * Math.PI / 180); } if (operation == "tan") { label1.Text = "tan(" + firstNumber.ToString() + ")"; result = Math.Tan(firstNumber * Math.PI / 180); } if (operation == "x^2") { label1.Text = "(" + firstNumber.ToString() + ")^2"; result = firstNumber * firstNumber; } if (operation == "3^x") { double t = 1; for (int i = 0; i < firstNumber; i++) { t = t * 3; } label1.Text = "(3)^" + "(" + firstNumber.ToString() + ")"; result = t; } if (operation == "2^x") { double t = 1; for (int i = 0; i < firstNumber; i++) { t = t * 2; } label1.Text = "(2)^" + "(" + firstNumber.ToString() + ")"; result = t; } if (operation == "10^x") { double t = 1; for (int i = 0; i < firstNumber; i++) { t = t * 10; } label1.Text = "(10)^" + "(" + firstNumber.ToString() + ")"; result = t; } display.Text = result + ""; }