我希望能夠從用戶界面添加和測驗邏輯條件。邏輯條件可以是 ands / ors 的任意組合。像這兩個例子:
示例 1:(a AND b) OR c
示例 2: (a OR b OR c) AND ((d AND e) OR (f AND g))
我可以像下面這樣以編程方式創建邏輯條件,但我的問題是如何創建界面以便用戶可以創建條件。它不必非常用戶友好,但在 GUI(winform)中,我希望能夠在 textBox 或類似內容中輸入一些內容來描述條件。
因此,如果用戶在文本框中鍵入它(a && b) || c或這個(a || b || c) && ((d && e) || (f && g))。然后我以編程方式應該能夠確定條件是真還是假(ae 已經在代碼中定義)
我希望有人能理解我的意思:-)
public class And
{
public bool[] ands;
public And(bool[] _ands)
{
ands = _ands;
}
public bool result()
{
foreach (bool b in ands) {
if(b == false)
return false;
}
return true;
}
}
public class Or
{
public bool[] ors;
public Or(bool[] _ors)
{
ors = _ors;
}
public bool result()
{
foreach (bool b in ors)
{
if (b == true)
return true;
}
return false;
}
}
public class test{
public test() {
bool a = true;
bool b = false;
bool c = true;
bool d = true;
bool e = false;
bool f = true;
bool g = true;
bool example1 = new Or(new bool[] { new And(new bool[] {a, b}).result(), new And(new bool[] { c }).result() }).result();
bool abc = new Or(new bool[] { a, b, c }).result();
bool ce = new And(new bool[] { d,e }).result();
bool fg = new Or(new bool[] { f,g }).result();
bool de_fg = new Or(new bool[] { ce,fg }).result();
bool example2 = new And(new bool[] { abc, de_fg}).result();
}
}
uj5u.com熱心網友回復:
如果您希望用戶輸入任意布爾運算式,例如“(a && b) || c ”,那么您將不得不進行一些決議。這與您所顯示的完全不同,您在代碼中鍵入運算式以便編譯器決議它。
你需要接受一個字串并計算出其中的內容的邏輯,就像這樣
"aha 這里是一個左大括號 '(',需要開始子運算式,aha 這里是 'a' 表示 'a' 的值,aha 現在 '&' 可能是按位和或邏輯,接下來是什么,另一個 ' &' ok ,logical and, ')' 右大括號,子運算式結束....."
Irony.net 是 ac# 決議器構建器https://github.com/IronyProject/Irony。
uj5u.com熱心網友回復:
您可以使用Linq.Expression或 使用DataColumn.Expression
如果您使用Linq.Expression,那么您將需要實作自己的運算式提供程式或構建自己的運算式提供程式Expression并立即將其用于任何Linq擴展。因此,您將能夠轉換為 lambda 運算式,然后您可以在任何給定的 Linq 擴展上執行該運算式。
如果您使用DataColumn.Expression,您將需要使用DataColumnorDataTable或DataRow傳遞string要評估的運算式。有一個預設的DataColumn.Expression運算式,例如AND, OR, Between..etc。因此,您不能使用任何不支持的運算式DataColumn.Expression。
uj5u.com熱心網友回復:
我找到了解決方案。也許不是最優雅的,但它似乎作業。
public test() {
List<bool> resultObjects = new List<bool>();
List<testobject> tests = new List<testobject>();
tests.Add(new testobject(Operators.OR, new List<string> {"a","b","c"})); //0
tests.Add(new testobject(Operators.AND, new List<string> {"d","e"})); //1
tests.Add(new testobject(Operators.AND, new List<string> {"f","g"})); // 2
tests.Add(new testobject(Operators.OR, new List<int> {1,2})); //3
tests.Add(new testobject(Operators.AND, new List<int> {0,3})); //4
Evaluater ev = new Evaluater();
foreach (testobject test in tests) {
resultObjects.Add(ev.Evaluate(test.op,test.booleans(resultObjects)));
}
bool finalResult = resultObjects.Last();
}
上面的測驗物件串列可以很容易地從用戶界面創建,因為它們只包含純字串和整數。它們代表邏輯運算式(a || b || c) && ((d && e) || (f && g))
其余代碼:
public enum TestType { testOnValues, testOnPreviousResult }
public enum Operators { AND, OR }
public class Evaluater
{
private Operators andor;
private List<bool> values;
public bool Evaluate(Operators _andor, List<bool> _values)
{
andor = _andor;
values = _values;
if (andor == Operators.AND)
{
return And();
}
else
{
return Or();
}
}
public bool And()
{
foreach (bool value in values)
{
if (value == false)
return false;
}
return true;
}
public bool Or()
{
foreach (bool value in values)
{
if (value == true)
return true;
}
return false;
}
}
public class testobject
{
public Operators op { get; set; }
private List<bool> values;
private List<string> dictionaryKeys { get; set; }
private List<int> resultKeys { get; set; }
private TestType type;
public testobject(Operators _op, List<string> _tests)
{
op = _op;
dictionaryKeys = _tests;
type = TestType.testOnValues;
}
public testobject(Operators _op, List<int> _tests)
{
op = _op;
resultKeys = _tests;
type = TestType.testOnPreviousResult;
}
public List<bool> booleans(List<bool> _results) {
values = new List<bool>();
if (type == TestType.testOnValues)
{
foreach (string s in dictionaryKeys)
{
values.Add(Bools.bools[s]);
}
}
if (type == TestType.testOnPreviousResult)
{
foreach (int i in resultKeys)
{
values.Add(_results[i]);
}
}
return values;
}
}
public class Bools
{
public static Dictionary<string, bool> bools = new Dictionary<string, bool>();
static Bools()
{
bools.Add("a", true);
bools.Add("b", false);
bools.Add("c", true);
bools.Add("d", false);
bools.Add("e", false);
bools.Add("f", false);
bools.Add("g", false);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438741.html
上一篇:是否可以為.NET6.0應用程式同時啟用Winforms和WPF?
下一篇:從一個執行緒多次呼叫GUI
