Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #669
| From | cri@tiac.net (Richard Harter) |
|---|---|
| Newsgroups | alt.comp.lang.learn.c-c++, comp.programming |
| Subject | Re: question on a generic program logic issue (redux) |
| Date | 2011-07-16 21:28 +0000 |
| Message-ID | <4e21e7e2.297896514@text.giganews.com> (permalink) |
| References | <nhc2275ivjf8jcbf8lkjjsifui29n0u6g0@4ax.com> |
Cross-posted to 2 groups.
On Sat, 16 Jul 2011 02:41:20 -0400, NoName <null@null.com> wrote:
>Hate it when I do this... send a message, immediately realize I forgot
>something. Repost, with an important edit. See second of two
>pseudocode examples, below....
>
>I'm trying to figure out how to do something in code, fairly
>efficiently, using fairly standard programming constructs. The exact
>language doesn't matter, could be C, C++, Java, Visual Basic, C#, etc.
>
>The idea is this. I want to have some code that basically says:
>
>if (condition1 and/or condition2 and/or condition3) then….
>
>where the "and/or" is determined by some kind of flag that can be set
>dynamically. Viewed another way, in pseudocode, I want:
>
>if
>(c1
>use "and" if and_flag1=true else use "or" if or_flag1=true
>c2
>use "and" if and_flag2=true else use "or" if or_flag2=true
>c3) then...
>
>I'm trying to figure out how to code this efficienty using standard
>kinds of procedural constructs: if/then, if/then/else, case or switch
>statements, etc.
I'm sorry but I'm having problems understanding exactly what you want,
i.e., what the code is supposed to do. The statement
if (condition1 and/or condition2 and/or condition3) then
seems fairly clear - we have three (n) conditions with a mystery
operator called "and/or". By the way, "and/or" had better be
associative or else you have a mess.
The pseudo code is unclear, at least to me. What does it mean to 'use
"and"' and 'use "or"'? I'm assuming that your c1, c2, and c3 are the
condition1, condition2, and condition3 from the first statement and
that they are boolean values. Now where do "and_flag1" etc come from?
One issue is that you don't index your mystery operators. That is,
what you actually want is something like
"if (c1 op1 c2 op2 c3) then"
where c1 and c2 are boolean values and op1 and op2 are conditionally
operators. This brings up something else; as far as I can tell there
is nothing to "use" if and_flag and or_flag are both false. Let's
call this case bogus. We have four cases for op1:
and_flag op_flag operator
T T and
T F and
F T or
F F bogus
I would think that "c1 op1 c2" would be false when op1 is bogus but
that's just speculation. Ignoring the "bogus" case we still have to
check whether your operators are associative; otherwise you need a
definite order of application rule. IIANM they are not associative.
Consider the expression c1 && c2 || c3. It is an abbreviation either
for (c1 && c2) || c3 or for c1 && (c2 || c3); the two expressions must
evaluate the same if your and/or is associative. They do not; when c1
is false and c2 and c3 are true the first expression evaluates true
and the second one false.
What this comes down to is this you should apply the conditions and
operators in a defined order. Odds are you are assuming left to right
parsing but that's your pick.
Now as to code. If you aren't averse to using functions I suggest a
function that looks like this:
bool andor (bool c1, bool c2, bool and_flag, bool or_flag)
{
if (and_flag) return c1 && c2;
if (or_flag) return c1 || c2;
return false; /* Whatever you want for this case */
}
Then your code looks like this:
c = andor(c1,c2,and_flag1,or_flag1);
c = andor(c ,c2,and_flag2,or_flag2);
/* etc */
if (c) {/* then do it */}
Note: given that you are doing the and/or trick you must examine all
conditions.
If I misunderstood your intent, my apologies.
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
question on a generic program logic issue (redux) NoName <null@null.com> - 2011-07-16 02:41 -0400 Re: question on a generic program logic issue (redux) Francis Glassborow <francis.glassborow@btinternet.com> - 2011-07-16 12:09 +0100 Re: question on a generic program logic issue (redux) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-16 12:42 +0100 Re: question on a generic program logic issue (redux) "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2011-07-16 13:49 +0100 Re: question on a generic program logic issue (redux) NoName <null@null.com> - 2011-07-16 11:43 -0400 Re: question on a generic program logic issue (redux) Paul N <gw7rib@aol.com> - 2011-07-16 09:37 -0700 Re: question on a generic program logic issue (redux) cri@tiac.net (Richard Harter) - 2011-07-16 21:28 +0000 Re: question on a generic program logic issue (redux) "BartC" <bc@freeuk.com> - 2011-07-18 10:35 +0100
csiph-web