简体   繁体   English

如何使用C#中的System.Data.Compute()计算sin

[英]How to calculate sin using System.Data.Compute() in C#

I'm new here! 我是新来的! I'm a newbie in C#. 我是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. 我搜索并找到了一种简单的方法来使用System.Data库。 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 etc Anybody has an idea? 这对于基本表达式来说非常简单,但我似乎找不到如何使计算器能够计算sin,cos等的方法。任何人都有想法?

PS: I'm not good in english so plz forgive me PS:我的英语不好,所以请原谅我

Thanks to Mat J for telling me about mXparser! 感谢Mat J向我讲述了mXparser! I found a way of calculating through it without changing alot of code! 我发现了一种通过它计算的方法而不需要改变很多代码!

Here is the mXparser: http://mathparser.org/ 这是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! 唯一的问题是它适用于Expressions而不是字符串,但它仍然非常简单易用!

I want to thanks for the fast answers of everybody! 我要感谢大家的快速答案! You really helped me out! 你真的帮帮我了!

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();
    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))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM