The goal is to optimize big condition expression by finding values dependencies between variables in order to reduce the logical OR statements.
Let's say we have the following condition:
if((A == 0 && B == 0) || (A == 0 && B == 1) || (A == 0 && B == 2) ...
a certain number of time...
Is there a way to reduce this kind of example by having automatically the following condition:
if(A == 0 && (B >= 0 && B <= 2))
The numbers involved in the first condition are only known just before the condition, it can't be typed manually. There can be also hundreds of logical OR operators involved in the condition. There can be gap between values (maybe use of % operator is needed) but there is always a pattern.
Any library or existing algorithm which can find the dependencies between the variables ?
Let's have another example:
if((A == 0 && B == 0) || (A == 0 && B == 2) || (A == 0 && B == 4)
can be translated to:
if((A == 0 && B%2 == 0)
One more:
if((A == 0 && B == 0 && C == 0) || (A == 0 && B == 2 && C == 0) || (A == 0 && B == 4 && C == 0) || (A == 0 && B == 0 && C == 1) || (A == 0 && B == 2 && C == 1) || (A == 0 && B == 4 && C == 1))
would be transformed into:
if(A==0 && B%2==0 && C>=0 && C<=1)
For the variables involved, I have all the values per terms.
I have something like [[0,0,0],[0,2,0],[0,4,0],[0,0,1],[0,2,1],[0,4,1]] (e.g. the last example)
Thanks for your time and answers !
if((A == 0 && B == 0) || (A == 0 && B == 2) || (A == 0 && B == 4)
cannot be translated to
if((A == 0 && B%2 == 0)
cuz
B == 6
and etc
Related
I'm trying to do something like this in c++
If x is equal to either a or b or c
and y is equal to either d or e or f
and z is equal to either g or h or i, it would turn true and execute the code
I am a bit lost in this
if(x==a||x==b||x==c && y==d||y==e||y==f && z==g||z==h||z==i){
// Do x
}
Just use && and ||, with parentheses to make the grouping clear.
if ((x == 'a' || x == 'b' || x == 'c')
&& (y == 'd' || y == 'e' || y == 'f')
&& (z == 'g' || z == 'h' || z == 'i')) {
// execute code
}
If you look at C++ operator precedence you'll find that && has higher precedence than ||.
That means that your if statement
if(x==a||x==b||x==c && y==d||y==e||y==f && z==g||z==h||z==i)
is the same as
if (x == a || x == b || (x == c && y == d) ||
y == e || (y == f && z == g) ||
z == h || z == i)
By using parentheses, you can change it to work the way you want:
if ((x == a || x == b || x == c) &&
(y == d || y == e || y == f) &&
(z == g || z == h || z == i))
It is a straightforward translation from what you said to C++ code:
if ((x == a || x == b || x == c) &&
(y == d || y == e || y == f) &&
(z == g || z == h || z == i))
{
}
Most real programs don't have conditionals this complex. Split up your logic into logical tests.
From the previous answers, the important point is that in C++, you missed that the AND operator && has a higher operator precendence than the OR operator ||.
This can be compared to how the multiplicative operators *, /, % of numbers have higher precedence than additive operators + and - in algebra.
Regroup the parathetheses so you have these three requirements of what x, y, and z have to be, and the statement is true when all three are correct. (Technically, this is called a product of sums form, as the if statement requires the truth of all three smaller statements (the AND), where each of the smaller statement uses only OR statements.).
if((x==a||x==b||x==c) && (y==d||y==e||y==f) && (z==g||z==h||z==i)){
// Do x
}
Your original code would be equivalent to
if(x==a || x==b || (x==c && y==d) || y==e || (y==f && z==g) || z==h || z==i){
// Do x
}
For larger if-statements, you can also use newlines but of course that will take up more lines.
if((x==a||x==b||x==c)
&& (y==d||y==e||y==f)
&& (z==g||z==h||z==i)
&& ... other conditions)){
// Do x
}
If you do not want such cumbersome and long if-statement notation, you can consider breaking into logical tests (the if-condition is the "product" of the logical tests), namely
bool a1 = (x==a||x==b||x==c);
bool a2 = (y==d||y==e||y==f);
bool a3 = (z==g||z==h||z==i);
bool a4 ...
if (a1 && a2 && a3 && a4 && ..) {
// Do x
}
Try using parentheses to separate your questions:
if( (condA || condB || condC) && (condD || condE || condF) && (...)) and so on.
(condA = condition A)
Going a little overboard here (but just a bit):
if (element_of(x, {a, b, c}
&& element_of(y, {d, e, f})
&& element_of(z, {g, h, i}))
{
foo();
}
with element_of defined as:
template <class T>
auto element_of(const T& element, std::initializer_list<T> list) -> bool
{
return std::find(list.begin(), list.end(), element) != list.end();
}
Or a even a little bit more overboard (this is just for fun, I don't recommend it):
if (Elem{x}.element_of({a, b, c})
&& Elem{y}.element_of({d, e, f})
&& Elem{z}.element_of({g, h, i}))
{
foo();
}
with Elem:
template <class T> struct Elem
{
T element;
auto element_of(std::initializer_list<T> list) -> bool
{
return std::find(list.begin(), list.end(), element) != list.end();
}
};
template <class T> Elem(T) -> Elem<T>;
Hello I am trying to learn c++ and I wanted to give a little practice with a program. However I'm having trouble using cout within the loop.
This is the loop I'm trying to output text from. When the user enters a number that isn't valid it is supposed to say "Sorry try again!"
while (datecheck)
{
bool check(false);
if (check)
std::cout<<"Sorry try again!"<<std::endl;
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
check = true;
}
When it outputs and I purposely keep myself in the loop it doesn't output anything
Year: -20
-20
-20
You declare a fresh new variable check at every iteration. And you initialize that variable to false every time. So move that declaration before the while loop.
Change this:
while (datecheck)
{
bool check(false);
...
check = true;
}
to this:
bool check(false);
while (datecheck)
{
...
check = true;
}
The problem is with declaration of bool check(false);. This keeps on re-assigning value to false at beginning of each iteration.
A simple fix could be to get-rid of use of check variable and use only datecheck.
bool datecheck(true);
while (true)
{
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
if (datecheck)
{
std::cout<<"Sorry try again!"<<std::endl;
}
else
{
break;
}
}
We are trying to check a scenario before displaying a pop-up message. The code below works great from Visual Studio but not from Azure after being published. Both are pointing to the same Azure SQL database.
PTO objPTOExist = db.PTOes.Where(p =>
(p.UserId == model.UserId && p.StartDate <= model.StartDate && model.StartDate <= p.EndDate && p.PTOId != model.PTOId)
|| (p.UserId == model.UserId && p.StartDate <= model.EndDate && model.EndDate <= p.EndDate && p.PTOId != model.PTOId)
|| (p.UserId == model.UserId && model.StartDate <= p.StartDate && p.StartDate <= model.EndDate && p.PTOId != model.PTOId)
|| (p.UserId == model.UserId && model.StartDate <= p.EndDate && p.EndDate <= model.EndDate && p.PTOId != model.PTOId)
|| (p.Name == "Holiday" && p.StartDate <= model.StartDate && model.StartDate <= p.EndDate && p.PTOId != model.PTOId)
|| (p.Name == "Holiday" && p.StartDate <= model.EndDate && model.EndDate <= p.EndDate && p.PTOId != model.PTOId)
|| (p.Name == "Holiday" && model.StartDate <= p.StartDate && p.StartDate <= model.EndDate && p.PTOId != model.PTOId)
|| (p.Name == "Holiday" && model.StartDate <= p.EndDate && p.EndDate <= model.EndDate && p.PTOId != model.PTOId)
).FirstOrDefault();
I know I can make the code simpler but that also had bad results on Azure even though it works locally.
Thank you for any insight as to why Azure behaves differently.
I want to check whether a given char is a digit at compile time. In particular I need the implementation of the following function prototype:
template<char c>
constexpr bool IsDigit();
For clarification: I dont must use a custom implementation. If there is already a built-in way in the std, I would prefer that method.
This should work for ASCII:
constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; } // ASCII only
If you need to keep your prototype:
template<char C>
constexpr bool IsDigit() { return C >= '0' && C <= '9'; } // ASCII only
For wchar_t support you could try something like:
constexpr bool IsDigit(wchar_t c)
{
return (c >= L'0' && c <= L'9') ||
(c >= L'\u0660' && c <= L'\u0669') || // Arabic-Indic
(c >= L'\u06F0' && c <= L'\u06F9') || // Extended Arabic-Indic
(c >= L'\u07C0' && c <= L'\u07C9') || // NKO
(c >= L'\u0966' && c <= L'\u096F') || // Devanagari
(c >= L'\u09E6' && c <= L'\u09EF') || // Bengali
(c >= L'\u0A66' && c <= L'\u0A6F') || // Gurmukhi
(c >= L'\u0AE6' && c <= L'\u0AEF') || // Gujarati
(c >= L'\u0B66' && c <= L'\u0B6F') || // Oriya
(c >= L'\u0BE6' && c <= L'\u0BEF') || // Tamil
(c >= L'\u0C66' && c <= L'\u0C6F') || // Telugu
(c >= L'\u0CE6' && c <= L'\u0CEF') || // Kannada
(c >= L'\u0D66' && c <= L'\u0D6F') || // Malayalam
(c >= L'\u0E50' && c <= L'\u0E59') || // Thai
(c >= L'\u0ED0' && c <= L'\u0ED9') || // Lao
(c >= L'\u0F20' && c <= L'\u0F29'); // Tibetan
// Missing check for Myanmar, Khmer, Mongolian, Limbu, New Tai Lue,
// Tai Tham Hora, Tai Tham Tham, Balinese, Sundanese, Lepcha, Ol Chiki,
// Vai, Surashtra, Kayah, Javanese, Cham, Meetei Mayek, Osmanya, Brahmi,
// Sora, Chakma, Sharada, Takri, Mathematical.
// For codes see http://www.unicode.org/ucd/
}
if(location[0] <= 'H' || location[0] >= 'A'
&& location[1] <= 8 || location[1] >= 1)
I am checking to see if the first indice is between 'A' and 'H' and the second indice is between 1 - 8.
so the argument would = 1 if the user entered { A, 1 }
-1 if the user entered { J, 1 }
And (&&) has higher precedence than or (||), so to express what you want you need:
if ((location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
Otherwise what's happening is that it does the equivalent of this:
if (location[0] <= 'H' || (location[0] >= 'A' && location[1] <= 8) || location[1] >= 1)
Which is clearly not what you intended.
You can put as many expressions in the condition as you want, but you must remember the precedence rules; I suggest always using extra parentheses for clarity.
As Jeremiah has pointed out in the comments, the condition still wouldn't be doing what you wanted -- you are asking if something is in a given range by checking if it's either less than the upper bound or greater than the lower bound (this covers all input), when you intended to check whether the input was both larger than the lower bound and smaller than the upper:
if (location[0] <= 'H' && location[0] >= 'A' && location[1] <= 8 && location[1] >= 1)
In this case, since the operators are all &&, no extra parentheses are needed, though you can add them for extra clarity.
You need &&s not ||s. For example: 'Z' >= 'A' || 'Z' <= 'H' is true.
if(location[0] <= 'H' && location[0] >= 'A' && location[1] <= 8 && location[1] >= 1)
While not necessary in this case, you should group your logic together with parenthesis (and if they were ||s you would have to for it to work as expected):
if((location[0] <= 'H' && location[0] >= 'A') && (location[1] <= 8 && location[1] >= 1))
Add parenthesis to group the conditions:
if( (location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
If location[0] was equal to 'J' (definitely NOT between 'A' and 'H'), your if-clause would be TRUE because even though location[0] <= 'H' is FALSE, location[0] >= 'A' is TRUE and you are using || (OR). The problem is that you are using || (OR) instead of && (AND). You need to use all && (AND) in the if-clause.
&& has higher precedence than ||. So, you may want to write it as
if((location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
If you already tried this, please tell what error you got
Sorry guys, new here. I should have posted my entire method. Thank you for explaining the precedence of the operators, but my issue was that I was comparing a char with an int. All I needed to do was put ' ' around my numbers.
void GameBoard::enterShips()
{
char location[2];
int ships = 0;
int count = 1;
while( ships < NUM_SHIPS )
{
cout << "Enter a location for Ship " << count << ": ";
cin >> location[0];
cin >> location[1];
cout << endl;
if((location[0] <= 'H' && location[0] >= 'A') && (location[1] <= '8' && location[1] >= '1'))
{
location[0] = location[0] - 64;
location[1]--;
Grid[location[0]][location[1]] = SHIP;
ships++;
count++;
}
else
{
cout << "Wrong coordinates entered. Try again." << endl << endl;
}
}
}
As you can see in this operator precedence table, && is #13. It's higher up than ||, which is #14.
Therefore:
if(location[0] <= 'H' || location[0] >= 'A'
&& location[1] <= 8 || location[1] >= 1)
Is equivalent to:
if(location[0] <= 'H' || (location[0] >= 'A'
&& location[1] <= 8) || location[1] >= 1)
You want:
if((location[0] <= 'H' || location[0] >= 'A') &&
(location[1] <= 8 || location[1] >= 1))
Or even better:
if(((location[0] <= 'H') || (location[0] >= 'A')) &&
((location[1] <= 8) || (location[1] >= 1)))
I always put brackets around everything except when */ and +- are concerned. Even if you memorize the table, you shouldn't assume others do.
It helps code readability and helps prevent bugs... even compiler bugs! Triple bonus. :)
EDIT: Ah, you want ALL of them to be true. Just use all &&s, then! This works:
if(((location[0] <= 'H') && (location[0] >= 'A')) &&
((location[1] <= 8) && (location[1] >= 1)))