Complex if another logical

Friends

How to implement the following complex logic?

flag1 can be "N" or "A" or "I"
flag2 can be "N" or "A" or "I"
flag3 can be "N" or "A" or "I"

function (string flag1, string flag2, string flag3) start

The function should return:

  • return "None" if flag1, flag2 and flag3 are "N"

  • else returns β€œActive” if flag1, flag2 and flag3 are β€œA”

  • else returns "Inactive" if flag1, flag2 and flag3 are "I"

  • else returns β€œboth” if flag1, flag2 and flag3 are β€œA” and β€œI” (OR β€œN”)

eg. 1) flag1 is β€œA” and flag2 is β€œI” and flag 3 is β€œI”
for example 2) flag1 is β€œI” and flag2 is β€œA” and flag3 is β€œI”
for example 2) flag1 is β€œA” and flag2 is β€œN” and flag 3 is β€œI”

search results

end

Thanks for the reply, but none of the posts give an answer. I know if something else is holding back and looking for logic to implement the above psedocode. All four conditions are particularly complex and must know how to implement them.

+6
c # logic
source share
5 answers

Your logic for point 4 is confusing ...

I would use the enum value for this, and not for strings - it is much safer for the type (for example, what if someone passed "WIBBLEWOBBLE" your method? What should it return?)

 enum Value { None, Active, Inactive, Both } private Value GetValue(Value flag1, Value flag2, Value flag3) { if (flag1 == flag2 && flag2 == flag3) // they are all the same return flag1; else return Value.Both; // there is a difference } 
+20
source share
 [Flags] enum SomeWierdReturn { Both = 0, None = 1, Active = 2, Inactive = 4 } public SomeWierdReturn DoSomething(SomeWierdReturn flag1, SomeWierdReturn flag2, SomeWierdReturn flag3) { return (SomeWierdReturn)(flag1 & flag2 & flag3); } 
+2
source share

I think it serves both readability and speed, if you first check if all 3 values ​​are equal.

 if ((flag1 == flag2) and (flag1 == flag3)) // use a switch or table to go 'I' -> Inactive etc else return "Both"; // as far as i understood 4) 
+1
source share

It will * work. I can’t say that I like it.

  string key = flag1 + flag2 + flag3; switch(key){ case "NNN": return "None"; case "AAA": return "Active"; case "III": return "Inactive"; default: break; } // ;) //Trying to make it as confusing as your requirement #4 var four = ( from s in key select s ).Distinct(); if(four.Count() > 1){ return "Both"; } } 
0
source share

Robaticus gave you the correct answer, but in the comment, not in the post, so I will expand it.

We have three flags that can take each of the three states. Thus, 3 * 3 * 3 = 27 possible options.

When faced with complex if .. then logic and such a small address space, it makes no sense to try to encode all if..then at all. Instead, one three-dimensional array contains a total of 27 elements.

const int None = 0; const int Inactive = 1; const int Active = 2;

private int ParseFlag (string flag) {switch (flag) {case "N": return None; case "I": return Inactive; case "A": return Active; default throw new Exception (string.Format ("The value of the flag {0} was received, but N, I or A is expected," flag)); }}

public FlagResult Lookup (string Flag1, string Flag2, string Flag3) {return FlagData [ParseFlag (Flag1), ParseFlag (Flag2), ParseFlag (Flag3)]; }

I will let you build an array.

0
source share

All Articles