Reputation: 87
I'm new here! I'm a newbie in C#. One of my friends challenged me to create scientific calculator. I searched and found an easy way to make it here - using the System.Data library. For an example we have this:
string math = "5 + 3 * 3";
string answer = new DataTable().Compute(math, null).ToString();
This worked pretty easy with basic expressions, but I can't seem to find a way how to make the calculator be able to calculate sin, cos e.t.c. Anybody has an idea?
PS: I'm not good in english so plz forgive me
Upvotes: 2
Views: 2651
Reputation: 87
Thanks to Mat J for telling me about mXparser! I found a way of calculating through it without changing alot of code!
Here is the mXparser: http://mathparser.org/
This is how the code has changed: It was:
string math = "5 + 3 * 3";
string answer = new DataTable().Compute(math, null).ToString();
Now it is:
string math = "5 + 3 * 3";
Expression e = new Expression(math);
math = e.calculate();
The only thing is that it works with Expressions instead of strings, but it still works really easy and simple!
I want to thanks for the fast answers of everybody! You really helped me out!
Upvotes: 3
Reputation: 174
You can use the System.Math library. But System.Data library is related to ado.net operation.
Example:
string input=90
var output =System.Math.Sin(Convert.ToDouble(input)));
output =System.Math.Cos(Convert.ToDouble(input)));
Upvotes: 1
Reputation: 108
Have a look at the Math Class, but something like this should work for you
string math = "5 + 3 * 3";
string answer = new DataTable().Compute(math, null).ToString();
answer = Math.Sin(Convert.ToDouble(answer)).ToString();
Upvotes: 1