Exclusive or expression operation

Source: Internet
Author: User

I was going to participate in ZTE's competition. Unfortunately, there is no time to do this.

Usage: Like a normal expression.

Key1 and key2 and key3 if key1 is false, the following values are not calculated.

Key1 or key2 or key3 if key1 is true, the subsequent calculation is unnecessary.

Brackets are supported.

BM is used for searching and filtering.Algorithm.
# Include <stack>
# Include <iostream>

Using namespace STD;

Char Buf [] = "123456789 ";

/*
Function: int * makeskip (char *, INT)
Objective: To create a bad character table by preprocessing based on bad character rules
Parameters:
Ptrn => mode string P
Plen => pattern string P Length
Return Value:
Int *-bad orders table
*/
Int * makeskip (char * ptrn, int Plen)
{
Int I;
// Apply for 256 int space for creating a bad Partition Table
/* PS: the reason why 256 requests are required is that one character is 8 characters long,
Therefore, the characters may have the power of 2, that is, 256 different cases */
Int * Skip = (int *) malloc (256 * sizeof (INT ));

If (skip = NULL)
{
Fprintf (stderr, "malloc failed! ");
Return 0;
}

// Initialize the bad orders table. All the 256 units are initialized to Plen.
For (I = 0; I <256; I ++)
{
* (Skip + I) = Plen;
}

// Assign values to the units that need to be assigned in the table. No value is required for the characters that do not appear in the mode string.
While (Plen! = 0)
{
* (Skip + (unsigned char) * ptrn ++) = Plen --;
}

Return skip;
}

/*
Function: int * makeshift (char *, INT)
Objective: To create a suffix table by pre-processing according to the suffix rules
Parameters:
Ptrn => mode string P
Plen => pattern string P Length
Return Value:
Int *-suffix table
*/
Int * makeshift (char * ptrn, int Plen)
{
// Apply for Plen int space for a table with a better suffix
Int * shift = (int *) malloc (Plen * sizeof (INT ));
Int * sptr = Shift + Plen-1; // it is convenient to assign values to the suffix table.
Char * pptr = ptrn + Plen-1; // records the index of the boundary position of the suffix table.
Char C;

If (shift = NULL)
{
Fprintf (stderr, "malloc failed! ");
Return 0;
}

C = * (ptrn + Plen-1); // The last character in the Save mode string, because it must be used repeatedly

* Sptr = 1; // when the last character is the boundary, the distance from 1 is determined.

Pptr --; // move the boundary to the second-to-last character (this sentence is added by myself, because I always feel that there will be bugs if I do not add it

. Try "abcdd", that is, the last two duplicates)

While (sptr --! = Shift) // The outermost layer loops to assign values to each unit in the suffix table.
{
Char * P1 = ptrn + Plen-2, * P2, * P3;

// The distance to be moved when the do... while loop is completed based on the characters referred to by the current pptr
Do {
While (P1> = ptrn & * P1 --! = C); // This empty loop is used to search for matching with the last character C

Position pointed to by the character

P2 = ptrn + Plen-2;
P3 = p1;

While (P3> = ptrn & * P3 -- = * P2 -- & P2> = pptr); // This empty loop is determined in

Position of matching characters in the boundary

} While (P3> = ptrn & P2> = pptr );

* Sptr = Shift + Plen-sptr + P2-P3; // Save the suffix table and use the character of pptr as the boundary.

Location to be moved
/*
PS: Here I want to declare: * sptr = (SHIFT + Plen-sptr) + P2-P3;
As you can see, if you only need to calculate the distance of string movement,

Some are not required.
Because when the string is matched from left to right, the indicator is always shifted to left. Here * The content saved by sptr,

Actually, indicators need to be moved
Distance, rather than the distance of string movement. I think snort does this because of performance considerations.


*/

Pptr --; // The boundary moves forward
}

Return shift;
}

/*
Function: int * bmsearch (char *, Int, char *, Int, int *, int *)
Objective: To determine whether the text string t contains the pattern string P
Parameters:
Buf => text string t
Blen => text string T Length
Ptrn => mode string P
Plen => pattern string P Length
Skip => bad orders table
Shift => suffix table
Return Value:
INT-1 indicates success (text string contains mode string), 0 indicates failure (text string does not contain mode string ).
*/
Int bmsearch (char * ptrn, char * BUF)
{
Cout <"ptrn" <ptrn <Endl;
Cout <"Buf" <Buf <Endl;
Cout <"------------------" <Endl;
Int Plen = strlen (ptrn );
 
Int B _idx = Plen;
Int blen = strlen (BUF );
 

If (Plen = 0)
Return 0;
If (Plen = 1)
{
If (strchr (BUF, ptrn [0])
{
Return 1;
}
Else return 0;
}
Int * Skip = makeskip (ptrn, Plen );
Int * shift = makeshift (ptrn, Plen );

while (B _idx <= blen) // determines whether the computed string matches at the end
{< br> int p_idx = Plen, skip_stride, shift_stride;
while (BUF [-- B _idx] = ptrn [-- p_idx]) // start matching
{< br> If (B _idx <0)
return 0;
If (p_idx = 0)
{< br> return 1;
}< BR >}< br> skip_stride = skip [(unsigned char) BUF [B _idx]; // calculate the Hop Distance Based on the bad character rules
shift_stride = shift [p_idx]; // calculate the hop distance according to the suffix rules
B _idx + = (skip_stride> shift_stride )? Skip_stride: shift_stride; // whichever is greater
}< br> return 0;
}

/**
* 0 not found
*-1 expression illegal
* 1 found
*/
Int judeptrstr (string & target, char * BUF)
{
Stack <string> opnd; // operand
Stack <char> optr; // Operator
Stack <string> operate; // Operator
Bool restult = true;
Bool isuse = false;


While (! Target. Empty ())
{
// Cout <"the number of parentheses is" <optr. Size () <Endl;
Switch (target [0])
{
Case '':
{
Target. Erase (0, 1 );
Break;
}
Case '\"':
{
Target. Erase (0, 1 );
Size_t resultquote = target. Find ('\"');
If (string: NPOs! = Resultquote)
{
String tempopnd = target. substr (0, resultquote );
Target. Erase (0, resultquote + 1 );

Opnd. Push (tempopnd );


}
Else
{
Return-1;
}
Break;
}
Case 'A ':
{
String tempand = target. substr (0, 3 );

If (tempand = "and ")
{
Target. Erase (0, 3 );

If (! Opnd. Empty ())
{
String tempopnd = opnd. Top ();
Opnd. Pop ();

Char * P = new char [tempopnd. Size () + 1];
Strcpy (p, tempopnd. c_str ());

If (! Isuse)
{
Restult = bmsearch (p, Buf );
Isuse = true;
}
Else
{
If (! Operate. Empty ())
{
String tempoper =

Operate. Top ();
If (tempoper = "and ")
{
Restult = bmsearch

(P, Buf) & restult;
}
Else restult = bmsearch

(P, Buf) | restult;
}

}
Delete P;
}

Operate. Push ("and ");
If (! Restult)
{
// Delete the element until or (and or is not in quotation marks.
Int quotenum = 0;
Int bracketnum = 0;
While (! Target. Empty ())
{

If (target [0] = '\"')
{
++ Quotenum;
}
Else if (target [0] = '(')
{
++ Bracketnum;
Optr. Push ('(');
}
Else if (target [0] = ')')
{
Bracketnum --;
If (! Optr. Empty ())
{
Optr. Pop ();
}
Else return-1; // The parentheses do not match

}
Else if (target [0] =

'O' & quotenum % 2 = 0 & bracketnum = 0)
{
If (target [1] = 'R '&&

(target [2] = '\ "' | target [2] ='')
{< br> break;
}< BR >}< br> target. erase (0, 1);
}< BR >}< br>
}< br> else
return-1; // It must not be and
break;

}
Case 'O ':
{
String tempand = target. substr (0, 2 );
If (tempand = "or ")
{
Target. Erase (0, 2 );

If (! Opnd. Empty ())
{

String tempopnd = opnd. Top ();
Opnd. Pop ();

Char * P = new char [tempopnd. Size () + 1];
Strcpy (p, tempopnd. c_str ());

If (! Isuse)
{
Restult = bmsearch (p, Buf );
Isuse = true;
}
Else
If (! Operate. Empty ())
{
String tempoper =

Operate. Top ();
If (tempoper = "and ")
{
Restult = bmsearch

(P, Buf) & restult;
}
Else restult = bmsearch

(p, Buf) | restult;
}< br> Delete P;
}< br>
operate. push ("or");
If (restult)
{< br> // Delete the element until it is encountered), and) not enclosed in quotation marks
int quotenum = 0;
int bracketnum = 0;
while (! Target. Empty ()
{

If (target [0] = '\ "')
{< br> ++ quotenum;
}< br> else if (target [0] = '(')
{< br> bracketnum ++;
}< br> else if (target [0] ==

')' & Quotenum % 2 = 0)
{
// Target. Erase (0, 1 );
If (bracketnum = 0)
{
Break;
}
Else bracketnum --;

}
Target. Erase (0, 1 );
}
}

}
Else
Return-1; // not or illegal

Break;

}
Case '(':
{
Optr. Push ('(');
Target. Erase (0, 1 );
Break;
}
Case ')':
{
Target. Erase (0, 1 );
If (optr. Empty ())
{
Return-1;
}
Char c = optr. Top ();;

If (C! = '(')
{
Return-1; // The parentheses do not match
}
Else optr. Pop ();
Break;
}
Default:
Return-1; // The parameter is invalid. It is of unknown type.
}

}

If (! Opnd. Empty ())
{
String tempopnd = opnd. Top ();
Opnd. Pop ();

Char * P = new char [tempopnd. Size () + 1];
Strcpy (p, tempopnd. c_str ());

Restult = bmsearch (p, Buf); // you cannot directly return the case where the parentheses may not match.
Delete P;

}
If (! Optr. Empty ())
{
Return-1; // The parentheses do not match
}
Return restult;

}

Int main ()
{/*
String cc = "\" 7 \ "and \" 23 \ "and \" 1 \ "or \" \"";
String cc = "\" 7 \ "and \" A \ "and \" 1 \ "or \" \"";
String cc = "\" 7 \ "or \" A \ "and \" 1 \ "or \" \"";
String cc = "(\" 7 \ "or \" 1 \ ") and \" \"";
String cc = "\" 7 \ "and \" 1 \ "and \" \"";
String cc = "(\" 7 \ "and \" 1 \ ") and \" \"";
String cc = "\" 7 \ "and (\" 1 \ "or \" \")";
String cc = "\" A \ "and (\" 1 \ "or \" \")";
String cc = "\" A \ "or (\" 1 \ "or \" A \ ") and \" L \")";
String cc = "\" A \ "or (\" 1 \ "or \" A \ ") and \" 4 \")";
String cc = "\" A \ "or (\" 1 \ "or \" A \ ") and \" 4 \"";
String cc = "\" A \ "or (\" 1 \ "or \" A \ ") and \" 4 \ "and \" L \"";
String cc = "\" A \ "or (\" 1 \ "or \" \") and \ "4 \" and \ "2 \" and \ "3 \" and \ "5 \" and \ "6 \" and \ "7

\ "And \" 0 \"";
String cc = "\" A \ "or (\" 1 \ "or \" \") and \ "4 \" and \ "2 \" and \ "3 \" and \ "5 \" and \ "6 \" and \ "7

\ "And \" 8 \"";
String cc = "(\" A \ "or (\" 1 \ "or \" A \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "5 \")";
String cc = "(\" A \ "or (\" 1 \ "or \" A \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "5 \"))";
String cc = "(\" A \ "or (\" 1 \ "or \" A \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "0 \")";
String cc = "(\" A \ "or (\" 1 \ "or \" A \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "0 \") or \ "p \"";
String cc = "(\" A \ "or (\" 1 \ "or \" A \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "0 \"))

Or \ "p \")";
String cc = "(\" A \ "and (\" 1 \ "or \" C \ ") or \" 2 \") or \ "3 \") and (\ "4 \" and \ "0 \"))

Or \ "p \"";
String cc = ")))";
String cc = "())";*/
// String cc = "(\" A \ "and (\" 1 \ "or \" C \ ") or \" 2 \") or \ "3 \") and (\ "r \" and \ "0 \"))

Or \ "p \"";

/* String buf1 = "NCP recveive message from SMCC: callback code = 0x5006, lparamlen =

0x0 .";*/

String buf1 = "NCP send message to MCU: nmcuadrs = 0x10301 encoding code =

0x11ab, lparamlen = 0x12. \ n"
"--------- NCP to MCU ---------- the content of message buffer is: \ n"
"01 03 01 12 01 FF Fe 11 AB Fe 01 Fe 00

00 00 12 00 01 \ n"
"01 12 01 Fe 00 01 00 01 02 Fe \ n"
"Set the (slot: 3 Port: 1) device type: Ethernet \ n"
"Comm process (0) Send MSG to SMCC (192.192.1.5) successfully! Response Code:

0x5006 \ n ";

 

Char * P1 = new char [buf1.size () + 1];
Strcpy (P1, buf1.c _ STR ());
 
/* String ptrn = "\" NCP \ "and \" send \ "or \" core \"";
String ptrn = "\" NCP \ "and \" core \"";

String ptrn = "(\" NCP \ "or \" core \ ") and \" message \ "and \" core \"";

String ptrn = "(\" NCP \ "or \" core \ ") and \" message \ "or \" core \"";
String ptrn = "(\" NCP \ "or \" core \ ") and \" message \ "or \" core \ ") and \" core \""; */
String ptrn = "(\" NCP \ "and \" core \ ") and \" message \ "or \" core \ ") and \" core \"";
 

Int resutl = judeptrstr (ptrn, P1 );
Cout <"============= buf1 begin ========================" <Endl;
If (resutl = 1)
{
Cout <buf1 <Endl;
}
Cout <"============= buf1 end ====================" <Endl;

System ("pause ");

 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.