2011-03-23 20 views
0

我需要在C#中編寫一個程序,以便於輸入邏輯表達式。Expression Creator程序 - C#

有一個操作符集,其中包括>,<,> =,< =,=,!= 有AND,OR和括號。

有將是一個菜單,用戶可以選擇運營商和輸入的值進行比較,以便輸出將是這樣的:

(A > 5) OR (B = 10 AND C != 50) 

你怎麼會建議做的方式,用戶總是必須輸入有效的值。你知道關於它的任何文章嗎?

+2

你可以問問你的導師以獲得更多幫助。 – Lazarus 2011-03-23 14:35:42

+0

沒有導師。這是我的工作 – 2011-03-23 14:39:43

+0

正則表達式?字符串子字符串? – 2011-03-23 14:40:38

回答

1

這聽起來像你正在做的是建立一個應用程序,允許用戶建立表達式樹(在一些限制內)。

Here is a really good article by Charlie Calvert describing what expression trees are,您將如何使用它們以及如何構建它們。

我希望這能讓你朝着正確的方向前進。

+0

這是我在過去爲一個簡單的邏輯表達式創建一個解析器,並結合遞歸體面的解析器來構建和遍歷表達式樹。工作得非常好,最後也很容易閱讀代碼。 – asawyer 2011-03-23 15:03:55

0

我過去曾使用Windows工作流活動規則引擎。 它給你一個Excel/Access風格的GUI來定義基於對象的規則,但可能會提供比你需要的更多。
看看here作爲開始。

0

可能運算符重載是你所尋找的?

喜歡的東西:

internal class Person 
{ 
    public int Age { get; set; } 

    public static bool operator >(Person thisPerson, int compareValue) 
    { 
     return thisPerson.Age > compareValue; 
    } 

    public static bool operator <(Person thisPerson, int compareValue) 
    { 
     return thisPerson.Age < compareValue; 
    } 

}