DeMorgan's law and C++ - c++

For each of the following write the equivalent C++ expressions, without any unary negation operators (!). (!= is still permitted)
Use DeMorgan's law
!( P && Q) = !P || !Q
!( P || Q) = !P && !Q
For
!(x!=5 && x!=7)
!(x<5 || x>=7)
!( !(a>3 && b>4) && (c != 5))
My answers:
(x>5 || x<5) || (x>7 || x<7)
x>=5 && x < 7
(a>3 && b > 4) && (c!=5)
Are these correct? If not, can you give me answers and explain why they are wrong?
I am a beginner in C++ so take it easy.

Check this out:
!(x!=5 && x!=7) --> x==5 || x==7
!(x<5 || x>=7) --> x>=5 && x<7
!( !(a>3 && b>4) && (c != 5)) --> (a>3 && b>4) || c==5
So, just #2 from your solutions is correct.

Related

Nil check in `||`, `!a || a.method` can't be done with multiple variables

For example, if we want to check an array is either nil or empty, we can write as follows:
if !a || a.empty?
puts "nil or empty!"
end
#-> OK
however, if we want to check two arrays in the same way, an error occurs:
if !a || !b || a.empty? || b.empty?
puts "nil or empty!"
end
#-> Error, `undefined method 'empty?' for Nil`
# in the expression `a.empty?`
Swapping the position of !b and a.empty? doesn't help, but different place is pointed to as an error:
if !a || a.empty? || !b || b.empty?
puts "nil or empty!"
end
#-> Still error, `undefined method 'empty?' for Nil`
# but this error is in the expression `b.empty?`
Why in this (multiple variable's) case compiler can't infer that a and b are non-nil when a.empty? and b.empty? are called, respectively?
The whole reproducible code is following.
def foo (flag)
if flag
[] of Int32
else
nil
end
end
# note, foo returns `Array(T) | Nil`
a = foo(true)
b = foo(true)
if !a || a.empty?
puts "nil or empty!"
end
#-> OK
if !a || !b || a.empty? || b.empty?
puts "nil or empty!"
end
#-> Error, `undefined method 'empty?' for Nil`
# in the expression `a.empty?`
if !a || a.empty? || !b || b.empty?
puts "nil or empty!"
end
#-> Still error, `undefined method 'empty?' for Nil`
# but this error is in the expression `b.empty?`
It's not a bug.
a || b || c || d
is parsed as:
(((a || b) || c) || d)
so in your example you have:
!a || !b || a.empty? || b.empty?
which means it's
(((!a || !b) || a.empty?) || b.empty?)
so from:
!a || !b
you can't deduce anything about a or b.
It works if you add parentheses:
if !a || (!b || (a.empty? || b.empty?))
It seems like a compiler bug.
But you can just put the conditions in parenthesis to make it work: (!a || a.empty?) || (!b || b.empty?)

What's wrong with this logical operation work?

I'm trying to create an if statement that validates that the user's bet is either exactly 100, 300 or 500. What am i doing wrong?
if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))
{
cout << "Incorrect input";
// Call round again
newRound();
}
if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))
This will evaluates as true for all roundBet, because a number is either not 100(roundBet != 100 true) or 100 (which is not 300, roundBet != 300 true)
What you need is:
if ((roundBet != 100) && (roundBet != 300) && (roundBet != 500))
One of the alternatives will always be true, since if roundBet is, say, 100, then it will be different from 300 and 500.
Use a logical AND &&

C++ operators precedence

I am using this statement
if ((pm && pn) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
So now it is acting like this:
0 0 = 1
0 1 = 0
1 0 = 1
1 1 = 1
but I need it to work like this:
0 0 = 1
0 1 = 0
1 0 = 0
1 1 = 1
can you tell me where am I making mistake?
What you want is simply:
if (pm == pn)
You are checking if pm is true twice. You also need to check if both are the same, not whether they are both true. So,
if ((pm == pn)
^^ ^^
pm && pm
should be
pm && pn
^
The whole expression can be simplified to
pm == pn
if the variables already have bool type.
Why not try xor?
if (!(pm ^ pn)) { /*...*/ }
Or simply equal?
if (pm == pn) { /*...*/ }
if ((pm && pm) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
Because you made a typo. You meant pm && pn.
Instead just write if (pm == pn), which is equivalent along as the only semantic values are indeed true and false for both variables.
Plus, consider making your variable names clearer and more distinct.
Note that operator precedence has nothing to do with this.
Since the question's title asks about precedence, note that || has lower precedence than &&. So the two sets of inner parentheses are redundant, and the original expression is just a longer way of saying
if (pm && pm || pm == false && pn == false)
Now, fixing the obvious typo:
if (pm && pn || pm == false && pn == false)
Removing the unneeded explicit comparisons:
if (pm && pn || !pm && !pn)
And, finally, a less obvious transformation, which others have suggested:
if (pm == pn)

RegExp for dates Extjs 3.4

I have problem to make regExp for search panel
&& (item.get('prodAddDate') >= dateStartValue.format("Y-m-d"))
&& (item.get('prodAddDate') <= dateEndValue.format("Y-m-d"));
I'm not sure if this is task for refExp but don't have any other idea
Problem is that when I'm don't fill field with date I can't filtr data with other conditions
I was trying something like this but don't working
&& ((item.get('prodAddDate') >= dateStartValue.format("Y-m-d")) || (new RegExp(dateStartValue)).test(item.get('prodAddDate'))) &&
((item.get('prodAddDate') <= dateEndValue.format("Y-m-d")) || (new RegExp(dateEndValue)).test(item.get('prodAddDate')));
Not entirely sure what you're trying to do, but here are some pieces of information that I hope can help you to solve your problem.
Comparison operators priority
The && operator has priority over ||, which means that:
A || B && C || D
Is equivalent to:
A || (B && C) || D
Not to:
(A || B) && (C || D)
Date comparison
You can compare Date objects directly:
// example data
var d1 = new Date('2012-12-12'),
d2 = new Date('2012-12-12'),
d3 = new Date('2013-01-01');
And get the result you expect with <, >, <=, and >=:
// logical results
d1 < d3 // true
d1 < d2 // false
d2 > d3 // false
d1 <= d2 // true
d1 => d2 // true
But not with equality comparison ==:
d1 == d2 // false
// yet...
d1 <= d2 && d1 => d2 // true
Conclusion: to test if one date is before or after another one, direct comparison is OK. But, in order to test if two dates are identical, use string comparisons:
// is this the same day?
d1.format('Y-m-d') === d2.format('Y-m-d') // true
// is this the same day and hour?
d1.format('Y-m-d H:i:s') === d2.format('Y-m-d H:i:s') // true
Ext.isEmpty()
Ext.isEmpty returns true for: null, undefined and empty strings '' only.
Ext.isEmpty(null) // true
Ext.isEmpty(undefined) // true
Ext.isEmpty('') // true
Ext.isEmpty(' ') // false
Ext.isEmpty(false) // false
// etc.
That may be useful to address your empty field case:
...
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') >= dateStartValue)
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') <= dateEndValue)
Finally
What's this obsession about regex? They're useful for complex string testing/extracting, but that's all. You should probably forget about them for a while, dude ;p

C++ Ternary operator logic

I'm having trouble figuring out what this if statement is doing. This is not my code so I am simply trying to understand what the Ternary operator is doing.
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride &&
(model[NUMVERTS].isLogging == true ? model[NUMVERTS].value : NumVertices) == NumVertices &&
(model[PRIMCOUNT].isLogging == true ? model[PRIMCOUNT].value : primCount) == primCount &&
(model[STARTINDEX].isLogging == true ? model[STARTINDEX].value : startIndex) == startIndex)
{
First,
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
could be written:
(model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
the ternary
model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride
checks to see if model[STRIDE].isLogging is true. If it is, it takes the value model[STRIDE].value. If not, it takes the value g_uiStride. This is then compared to g_uiStride.
So, if it isn't logging, then this portion is automatically true because g_uiStride is compared to itself. If it is logging, it is true if mode[STRIDE].value == g_uiStride
and
#1.
if (model[STRIDE].isLogging is true then
RESULT1 = (model[STRIDE].value == g_uiStride) else
RESULT1 = (g_uiStride == g_uiStride)
)
#2.
if (model[NUMVERTS].isLogging is true then
RESULT2 = (model[NUMVERTS].value == NumVertices) else
RESULT2 = (mVertices == NumVertices)
)
#3.
if (model[PRIMCOUNT].isLogging is true then
RESULT3 = (model[PRIMCOUNT].value == primCount) else
RESULT3 = (primCount == primCount)
}
#4.
if (model[STARTINDEX].isLogging is true then
RESULT4 = (model[STARTINDEX].value == startIndex) else
RESULT4 = (startIndex == startIndex)
)
if (RESULT1 && RESULT2 && RESULT3 && RESULT4) {
/* yay */
} else {
/* damn */
}
In general the ternary conditional operator uses a condition to choose between two alternatives:
condition ? first_alternative : second_alternative
In this case it is very unnecessarily complicated by comparing to true and one object to itself
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
This can be reduced to
if((model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
which is also equivalent to
if (model[STRIDE].value == g_uiStride || !model[STRIDE].isLogging
telling us that either value is equal to some global value, or we don't care because we are not logging anyway.
blah = (model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride)
is the same as
if (model[STRIDE].isLogging) {
blah = model[STRIDE].value ;
} else {
blah = g_uiStride;
}
The ternary operator is as follows:
(condition) ? value_for_true_condition : value_for_false_condition
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) first checks to see if the isLogging == true, the (condition). If the condition is true the model[STRIDE].value value is used, if not true the g_uiStride value is used.
The statement as a whole checks the values on all those members of model, but only if the member .isLogging == true. Otherwise it uses the default value. Note that this statement will always be true if all members have .isLogging variable set to false.