Ein kleiner UND, ODER und NICHT Parser
February 12th, 2010 by darkleo
public class Parser { Dictionary<string,> status; public Parser(Dictionary<string,> status) { this.status = new Dictionary<string,>(status.Count * 2); foreach (var s in status) { this.status.Add(s.Key.Trim(), s.Value); this.status.Add("!" + s.Key.Trim(), s.Value ? false : true); } } public bool Parse(string input) { String[] or = new String[] { "||" }; String[] and = new String[] { "&&" }; string[] ors = input.Split(or, StringSplitOptions.RemoveEmptyEntries); for (int io = 0; io < ors.Length; io++) { string[] ands = ors[io].Split(and, StringSplitOptions.RemoveEmptyEntries); for (int ia = 0; ia < ands.Length; ia++) { if (this.status[ands[ia].Trim()] == false) break; if (ia == ands.Length-1) //last return true; } } return false; } public static void Test() { Dictionary<string,> status = new Dictionary<string,>(); status.Add("JA", true); status.Add("NEIN", false); status.Add("OK", true); Parser p = new Parser(status); bool result = p.Parse("JA && OK && !NEIN || NEIN"); result = p.Parse("NEIN || JA && OK && !NEIN"); result = p.Parse("NEIN"); result = p.Parse("JA"); result = p.Parse("!JA && NEIN"); } } </string,></string,></string,></string,></string,>
Der letzte Baustein einer UND-Verknüpfung ist auch TRUE somit ist der Gesammte Ausdruck der Einfachen Abfrage TRUE. Das ist der ganze Trick, der Quelltext kann vereinfacht werden, wird aber nicht mehr so verständlich.
Viel Spass und ein schönes Wochenende.