我建議拆分成執行階段:
1. Performs all `AND`s
2. Perform all `OR`s
例如
a & b & c | d | e & f & g | h == // put the right order
(a & b & c) | (d) | (e & f & g) | (h) == // perform ANDs
a_b_c | d | e_f_g | h == // perform ORs
final result
你的情況
{19, 23, 29} & {1, 4, 29} | {1, 5, 23} & {2, 4, 19} == // put the right order
({19, 23, 29} & {1, 4, 29}) | ({1, 5, 23} & {2, 4, 19}) == // perform ANDs
{29} | {} == // perform ORs
{29}
實施
private static IEnumerable<T> CombinatorOrAnd<T>(IEnumerable<IEnumerable<T>> sources,
IEnumerable<string> actions) {
List<IEnumerable<T>> orList = new List<IEnumerable<T>>();
// First, do all ANDs
bool isFirst = true;
IEnumerable<T> temp = null;
using (var en = actions.GetEnumerator()) {
foreach (var argument in sources) {
if (isFirst) {
temp = argument;
isFirst = false;
continue;
}
en.MoveNext();
if (en.Current == "AND")
temp = temp.Intersect(argument);
else {
orList.Add(temp);
temp = argument;
}
}
}
orList.Add(temp);
// Finally, perform all ORs
return orList.Aggregate((s, a) => s.Union(a));
}
測試
List<int> list1 = new List<int>(new int[] { 19, 23, 29 });
List<int> list2 = new List<int>(new int[] { 1, 4, 29 });
List<int> list3 = new List<int>(new int[] { 1, 5, 23 });
List<int> list4 = new List<int>(new int[] { 2, 4, 19 });
List<string> andOrList = new List<string>();
andOrList.Add("AND");
andOrList.Add("OR");
andOrList.Add("AND");
var result = CombinatorOrAnd(new List<int>[] { list1, list2, list3, list4}, andOrList);
Console.Write(string.Join(", ", result.OrderBy(item => item)));
成果
29
我認爲你將不得不編寫一種運算符優先級解析器。 – aloisdg
@aloisdg逆向波浪符號的不同版本? – jason
你可以依賴RPN,但它不是必須的 – aloisdg