2012-02-17 5 views
1

文字列方程式を実行できるどこかの関数がオンラインで見つかりました。たとえば、「1 + 5」などの文字列を使用できます。私はそれを関数に渡すと、答えを返します。私のアプリケーションでは、データベースから数式を取り出し、未知数(x、y、zなど)をユーザーがアプリケーションを使用するときに入力した実際の数(1,2,3,4,5など)で置き換えています。しかし、私が実行する必要がある数式の中には平方根が必要です。 Math.Sqrt()関数が含まれている文字列を渡そうとすると、定義されていない関数であるというエラーが表示されます。この関数を認識するための関数を変更する方法はありますか?データテーブルの認識関数を作成する方法

この関数はデータテーブルを使用します。コードは次のとおりです。

public static double executeFormula(string expression) 
{ 
    System.Data.DataTable table = new System.Data.DataTable(); 
    table.Columns.Add("expression", string.Empty.GetType(), expression); 
    System.Data.DataRow row = table.NewRow(); 
    table.Rows.Add(row); 
    return double.Parse((string)row["expression"]); 
} 

ありがとうございました!

+0

「オンラインのどこかから」正確に何を使用していますか? – Randy

+0

あなたが何を求めているのか正確にはわかりません。私はどこでそれを得るのかということを意味するならば、私は文字列方程式を解析する能力に関する質問に答えるこのようなフォーラムから得ました。 私が使っているものを聞いているのなら、それは私が学校に持っているプロジェクトのためです。データベースから引き出された数式を解析する必要があります。 – Cityonhill93

+0

'string.Emptyの代わりに。GetType() 'あなたは' typeof(string) 'を書くことができます。 –

答えて

3

nCalcを使用してください。

これは、あなたが話していることを正確にサポートしています。あなたがまだヒットしていないものに対しては拡張可能です。

+0

+1 - nCalcへのポインタのおかげで。便利ですね! – dash

+0

@ダッシュ:そうです。私たちは2つの主な問題を解決するためにそれを使用します。ランタイムまで知られていない複雑な数学関数を最初に実行する。もう1つは、ページ処理のための単純なルールエンジンを実装するために使用したことです。非常に非常に使いやすく、かなり素早く起動できます。 – NotMe

+0

最高のライブラリー!本当にありがとう! – Cityonhill93

0

人が認識して使用できる「テキスト機能」を作成し、プログラムで解析することができます。 WolframAlphaまたはMS Math 4.0をご覧ください。たとえば、次のようなことができます:

if(str.IndexOf("sqrt")>-1) 
{ 
    //replace sqrt for the actual Math.Sqrt() function 
} 

幸運!

2

私はあなたがそれを書いたように、答えはいいえです。あなたが表示している関数は、データテーブルのDataColumnの機能であるExpressionのプロパティを利用しています。

効果的に、それはあなたがSQLに見つけるだろう演算子の多くをサポートしています。

ドキュメントから:

OPERATORS 

Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use parentheses to group clauses and force precedence. The AND operator has precedence over other operators. For example: 

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John' 

When creating comparison expressions, the following operators are allowed: 

< 

> 

<= 

>= 

<> 

= 

IN 

LIKE 

The following arithmetic operators are also supported in expressions: 

+ (addition) 

- (subtraction) 

* (multiplication) 

/(division) 

% (modulus) 

STRING OPERATORS 

To concatenate a string, use the + character. Whether string comparisons are case-sensitive or not is determined by the value of the DataSet class's CaseSensitive property. However, you can override that value with the DataTable class's CaseSensitive property. 

WILDCARD CHARACTERS 

Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ([]). If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example: 

"ItemName LIKE '*product*'" 

"ItemName LIKE '*product'" 

"ItemName LIKE 'product*'" 

Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed. 

PARENT/CHILD RELATION REFERENCING 

A parent table may be referenced in an expression by prepending the column name with Parent. For example, the Parent.Price references the parent table's column named Price. 

A column in a child table may be referenced in an expression by prepending the column name with Child. However, because child relationships may return multiple rows, you must include the reference to the child column in an aggregate function. For example, Sum(Child.Price) would return the sum of the column named Price in the child table. 

If a table has more than one child, the syntax is: Child(RelationName). For example, if a table has two child tables named Customers and Orders, and the DataRelation object is named Customers2Orders, the reference would be: 

Avg(Child(Customers2Orders).Quantity) 

AGGREGATES 

The following aggregate types are supported: 

Sum (Sum) 

Avg (Average) 

Min (Minimum) 

Max (Maximum) 

Count (Count) 

StDev (Statistical standard deviation) 

Var (Statistical variance). 

Aggregates are usually performed along relationships. Create an aggregate expression by using one of the functions listed above and a child table column as detailed in PARENT/CHILD RELATION REFERENCING above. For example: 

Avg(Child.Price) 

Avg(Child(Orders2Details).Price) 

残念ながら、平方根はあなたが使用できる機能の一つではありません。ただし、DataTableを使用しているときは、単に行をループし、列の値から直接SquareRootを計算することができます。 「MyNumber」という列があるとします。あなたが望んだ場合は、Extensionメソッドに機能のこの種を置くことができ

myDataTable.Columns.Add(new DataColumn() { Name = "MySquareRoot", DataType = typeof(Double)}); 

foreach(DataRow row in myDataTable.Rows) 
{ 
    myDataTable["MySquareRoot"] = Math.Sqrt(Convert.ToDouble(myDataTable["MyNumber"])); 
} 

:あなたは、その後のような何かを行うことができます。

また、数式を別の場所で処理することもできます。 DataTableを使用してデータをモデル化するのではなく、クラスを使用できます。クラス自体は、データを表すプロパティ(またはリストまたはコレクション)と、データに作用するメソッドを持つことができます.C#で必要な関数を使用できます。

0

CodePlexプロジェクトfleeをご覧ください。 Fleeを使用すると、実行時に"sqrt(a^2 + b^2)"などの文字列式を評価できます。

関連する問題