如何用数学运算符计算数字串 [英] How to calculate String Of Number with Mathematical Operators

查看:77
本文介绍了如何用数学运算符计算数字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我有下面像我需要使用c#.net来计算下面。


字符串数字=5 + 10 + 5 + 22 + 22.5-10;

可以指导或发送片段

解决方案

快速搜索:

Math Parser .NET C# [ ^ ]



https://csharpeval.codeplex.com/ [ ^ ]


除了Mehdi Gholam提到的那个,还有:

Calculator.Net [ ^ ]

NCalc [ ^ ]

< a href =http://muparser.beltoforion.de/> MuParser [ ^ ]

Math Parser .NET [< a href =http://www.codeproject.com/Articles/274093/Math-Parser-NETtarget =_ blanktitle =New Window> ^ ]

Google了解更多 [ ^ ]


解决方案由 Mehdi Gholam提供 CIDev 非常好,但我想向您展示一些不需要任何第三方库的替代和简单代码!让我们看看:

  class 程序
{
const string expressionColumn = 表达;

public static void Main( string [] args)
{
Console.WriteLine(Evaluate( 5 + 10 + 5 + 22 + 22.5-10));
}

private static double Evaluate( string expression)
{
DataTable table = new DataTable();
table.Columns.Add(expressionColumn, typeof string ),表达式);
DataRow row = table.NewRow();
table.Rows.Add(row);
return double .Parse(( string < /跨度>)行[expressionColumn]);
}
}





干杯!

Shuvro


HI,
I have below like i need to calculate below using c#.net

String Numbers="5+10+5+22+22.5-10";
Can U guide or send snippets

解决方案

A quick search :
Math Parser .NET C#[^]

https://csharpeval.codeplex.com/[^]


Besides the one mentioned by Mehdi Gholam there are:
Calculator.Net[^]
NCalc[^]
MuParser[^]
Math Parser .NET[^]
and Google knows lots more[^]


Solutions provided by Mehdi Gholam and CIDev is quite good but i would like to show you some alternative and simple code that would not require any 3rd party library! Let's see:

class Program
{
    const string expressionColumn = "expression";

    public static void Main(string[] args)
    {
        Console.WriteLine(Evaluate("5+10+5+22+22.5-10"));
    }

    private static double Evaluate(string expression)
    {
        DataTable table = new DataTable();
        table.Columns.Add(expressionColumn, typeof(string), expression);
        DataRow row = table.NewRow();
        table.Rows.Add(row);
        return double.Parse((string)row[expressionColumn]);
    }
}



Cheers!
Shuvro


这篇关于如何用数学运算符计算数字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆