for my word game i'm comparing chars in words, 6 in the first word, 6 in the random word.
Now I want it to do things when certain letters are on there place.
for example if
(aChar1 == aChar7 && aChar2 == aChar8){
//do something
}
but later in my code there is
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 == aChar9){
//do something
}
Now I just want the second code line to happen and not the first since that is only if the first 2 are in place. I need to add code like :
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 isnotequalto aChar9){
//do something
}
What code should I use for saying not equal?
use this and tell me if it works
achar3!=achar9
Related
I use Pcre2 lib that can be found here.
As you can see here Pcre2 \w matches only L and N categories and underscore and not matches M - marks (see here). However .Net Regex matches marks (see here).
I want to change the source code of PCRE2 to behave like .Net Regex, only I'm not sure I'm doing right.
What I want to do is find in the code where PT_WORD is referenced, like this:
case PT_WORD:
if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
fc == CHAR_UNDERSCORE) == (Fop == OP_NOTPROP))
And add another line like that:
case PT_WORD:
if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
PRIV(ucp_gentype)[prop->chartype] == ucp_M || // <-- new line
fc == CHAR_UNDERSCORE) == (Fop == OP_NOTPROP))
Is it right to do so? Are there other things to consider? What else do I need to change elsewhere in the code?
A .NET \w construct matches
Category Description
Ll Letter, Lowercase
Lu Letter, Uppercase
Lt Letter, Titlecase
Lo Letter, Other
Lm Letter, Modifier
Mn Mark, Nonspacing
Nd Number, Decimal Digit
Pc Punctuation, Connector. This category includes ten characters, the most commonly used of which is the LOWLINE character (_), u+005F.
Note the differences: .NET \w does not match all numbers, only those from the Nd category, and as for the M category, it only matches Mn subset.
Make sure you match these Unicode categories within your code and \w will behave as in .NET regex.
Use
case PT_WORD:
if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Ll ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Lu ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Lt ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Lo ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Lm ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Mn ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Nd ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Lm ||
PRIV(ucp_gentype)[prop->chartype] == ucp_Pc) == (Fop == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
break;
Note you do not need to care about fc == CHAR_UNDERSCORE as it is part of \p{Pc} and you can't use just ucp_L as it also includes \p{LC}.
I started studying C a week ago and decided to write my own tic-tac-toe game for practise.
I have a game loop in main:
for(int i = 1; player1.isWinner!=1 || player2.isWinner!=1 || noWinner!=1; i++){...}
Where i - counts turns and condition of end of the game is one of players has won, or no one has won (draw).
For now, it quits executing only if all conditions are 1.
How can I make it work right?
Is a value of 1 where someone won?
If so, then you would need check any of those conditions is true and loop if they are not:
!(player1.isWinner==1 || player2.isWinner==1 || noWinner==1)
Or using AND, check and loop when none are set:
(player1.isWinner!=1 && player2.isWinner!=1 && noWinner!=1)
Consider extracting the condition to a well-named function in order to aid readability and maintanability:
int hasWinner(/*...*/)
{
return player1.isWinner == 1 || player2.isWinner == 1 || noWinner == 1;
}
It then becomes obvious what the condition should be:
for(int i = 1; !hasWinner(/*...*/); i++){ /*...*/ }
You seem to be using some sort of backwards boolean logic. If 1 represents the boolean value true, then the condition should be
!(player1.isWinner || player2.isWinner || noWinner)
This assumes that you set player1.isWinner to 1 when player1 has won.
It would probably be easier to use bool with values true or false from stdbool.h.
The code below shows the rowtotal[0], which is the return value I'm getting from an infinite loop for every iteration. I'm trying to break the loop when all three returned values from the costcheck array are the same. This is my code:
do
{
.
.
.
/*do loop body*/
.
.
costcheck[counter3]=rowtotal[0];
if(costcheck[counter3-2]==costcheck[counter3] &&
costcheck[counter3-1]==costcheck[counter3] )
{
response=1;
}
counter3++;
printf("\t\t\t Number of iterations: %d \r", stop++);
}
while(response!=1);
Just get rid of all strange, superfluous variables. You only need to save the result of the previous iteration, together with a counter which you increase each time you find a match, rather than every time in the loop.
int counter=0;
const int COUNT_N = 3;
data_t prev=FORBIDDEN; // a value that rowdata[0] can never have
while(counter != COUNT_N)
{
...
if(prev == rowdata[0])
{
counter++;
}
else
{
counter=0;
}
prev = rowdata[0];
}
just to elaborate on Lundins Answer wich is the way to go in my opinion (would have posted as a comment, but lacking reputation...)
Only thing missing is the actual loop advancement counter (counter3 in your example):
int quitCounter=0; // Counter for quiting the loop on 3 consecutive identical values
int loopCounter=0; // Your normal rowdata index
const int QUIT_COUNT_N = 3;
#define FORBIDDEN 0xffffff // or some other value that rowdata[0] can never have
data_t prev=FORBIDDEN; // a value
do
{
...
/* do loop body, provide new value for rowtotal[0] on each iteration */
/* if you need to store the consecutive values returned in rowtotal[0] in costcheck array,
make sure that it will be big enough - if you only need to break on 3 identical values,
you can skip the entire costcheck array as Lundin proposes. */
...
costcheck[counter3]=rowtotal[0];
if(prev == costcheck[counter3])
{
quitCounter++;
}
else
{
quitCounter=0;
}
prev = costcheck[counter3];
counter3++;
} while(quitCounter!= QUIT_COUNT_N )
If you really want an infinite loop, a if(costcheck[counter-1] == costcheck[counter-2] && costcheck[counter-2] == costcheck[counter-3]) will lead to failure of program, if costcheck array has less than 3 elements. You have to be sure that it does have at least 3 elemets in this array.
BUT!!!! counter does not need to be more than 3 because as far as i get it, you want to check 3 most reciently read elements. Which means for comparison, you only need to remember 3 last values that were read.
The exapmple below stores up to 3 rowtotal[0] values, and checks if they are equal. If they are, progarm exits, if not, program gets new rowtotal[0] to the "end" of costcheck array, also the oldest value: here it's costcheck[0] is lost.
I can post the code to the example which i made, to show how the logic should work.
NOTE!!! I strongly think Lundin's and Morphine's solutions are by far better than mine.
do
{
.............
if(counter < 3)
{
costcheck[counter] = rowtotal[0];
counter++;
continue;
}
else
{
if(costcheck[counter-1] == costcheck[counter-2] && costcheck[counter-2] == costcheck[counter-3])
{
response=1;
}
else
{
costcheck[counter-3] = costcheck[counter-2];
costcheck[counter-2] = costcheck[counter-1];
costcheck[counter-1] = rowtotal[0];
}
}
}
while(response!=1);
}
i have a problem in c like this:
I have a problem in the code below. The problem is this that i doing some addition part after both if conditions break.And that addiion is repeatative while loop until count>0 The problem is if i put braces in for loop then it repeats the the part inside the braces until it's condition is not false. Buti have to do addition which is like this:
We have an array data[i].freq={0,1,2,3,4,5} and data[i].next represents the next member to be added.Suppose i add 0 and 1 first i got"1" as a result and i put the result in last index of my array , like this ({0 1 2 3 4 5 1})now 0 and 1 cann not be added because they are already added(i don't have to repeat the addition on same elements), so next time the addition will be between the last index element and smallest element before the last element (but not those elements who are already added). so here the addition will be between the "1" in last index+ smallest element in right to it which is "2" Note here we have not taken in acount 0 and 1 because they are already added, the same way this 2 will not be added next time because it is being added this time and their addition wil be {0 1 2 3 4 5 1 3} we have to repeat the same until there left & element at last.
data[data_size].freq=data[i].freq+data[p].freq; // here i add the first 2 elements "0" and "1" in the example i given below.
int count=5;
do
{
for(i=0;data[i].next!=-1;i=data[i].next)//the problem is here if i put bracesit dont't do the ask which i expect it to do.
if(data[data[i].next].freq>data[data_size].freq && data[data[i].next].flag==0)
break;
data[data_size+1].freq= data[data_size].freq+ data[data[i].next].freq;
data[data_size].next=data[i].next;
data[i].next=data_size;
data_size++;
if(data[data[i].next].freq<data[data_size].freq && data[data[i].next].flag==0)
break;
data[data_size+1].freq= data[data_size].freq + data[i].freq
data[data_size].next=data[i].next;
data[i].next=data_size;
data_size++;
count--;
} while(count>0)
could any one please help me in desiging the code for what i want to achieve.
If you write
for(i=0 ..any condition)
if(condition1)
break;
if(condition2)
break;
you don't only have an unreadable mess, but you also have only the first if clause in the for loop.
If you want the for loop to extend over both, you must put them into {}:
for(...) {
if(condition1) break;
if(condition2) break;
}
My personal preference is to always use braces when the statements are in the next line. S don't write
if(condition1)
break;
as someone could be tempted to insert an andditional statement and be surprised that it doesn't work as it should, but either do
if(condition1) break;
in one line or
if(condition1) {
break;
}
add braces which are not needed for functionality, but for readability.
This is how the compiler sees your code:
int count=5;
do
{
for (i = 0; data[i].next != -1; i = data[i].next)
{
if (data[data[i].next].freq > data[data_size].freq && data[data[i].next].flag == 0)
break; // break out of the enclosing 'for' loop (goes to point A)
}
// Point A
data[data_size].next = data[i].next;
data[i].next = data_size;
data_size++;
if (data[data[i].next].freq < data[data_size].freq && data[data[i].next].flag == 0)
{
break; // break out of the outer 'do' loop (goes to point B)
}
data[data_size].next = data[i].next;
data[i].next = data_size;
data_size++;
count--;
} while (count > 0);
// Point B
Based on your code comments, I think your problem is that your 'break' statements are not taking you where you think they're taking you.
This seems basic but I need to add numbers whether or not they are the condition is "on" (i'll probably change this to boolean). So my question is how to do this in C code if it is possible. I tried something of this sort and various renditions:
dfTotalTaxOwed[nIndex] = dfFedTaxOwed[nIndex] + if(arrNYStateTaxStatus[nIndex] == 1){dfNYStateTaxOwed[nIndex];}
+ if(arrNDStateTaxStatus[nIndex] == 1){dfNDStateTaxOwed[nIndex];}
+ if(arrNHStateTaxStatus[nIndex] == 1){dfNHStateTaxOwed[nIndex];}
+ if(arrOHStateTaxStatus[nIndex] == 1){dfOHStateTaxOwed[nIndex];}
+ if(arrPAStateTaxStatus[nIndex] == 1){dfPAStateTaxOwed[nIndex];}
+ if(arrNJStateTaxStatus[nIndex] == 1){dfNJStateTaxOwed[nIndex];}
+ dfFicaTaxOwed[nIndex];
thanks
You can use the ternary operator.
expr ? true value : false value
I.e. replace if(arrNYStateTaxStatus[nIndex] == 1){dfNYStateTaxOwed[nIndex];} with (arrNYStateTaxStatus[nIndex] == 1) ? dfNYStateTaxOwed[nIndex] : 0.
On a side note, you might want to consider redesigning your program to use a dictionary instead of having an array for each state.
C control statements don't have return values so this approach won't work. Is there a reason you don't want to do a series of if statements like
if(arrNYStateTaxStatus[nIndex] == 1) {
dfTotalTaxOwed[nIndex] += dfNYStateTaxOwed[nIndex];
}
?