Check whether the arithmetic expression is correct (including +-*/() Six expressions)
The stack operation is used.
During the test of the strinspection parameter, I used a format similar to "1 + 9 + 9 + 9 + 9-* 7*8*9 ".
UsingSystem;
UsingSystem. collections;
Private Bool Checkinspection ( String Strinspection)
{
Stack Inspection = New Stack ();
Int Leftcount = 0 ; // Check matching between left and right brackets
Int Rightcount = 0 ; // Check matching between left and right brackets
Bool Haserror = False ; // Error mark
Foreach ( Char S In Strinspection)
{
If ( " + -*/ " . Contains (S. tostring ()))
{
If (Inspection. Count = 0 )
{
// When there are no elements in the stack
// The expression cannot start with + -*/.
Haserror = True ;
Break ;
}
Else
{
If ( " + -*/( " . Contains (inspection. Peek (). tostring ()))
{
// +-*/Already exists in the stack -*/
// (Not directly followed by + -*/
Haserror = True ;
Break ;
}
Else
{< br> inspection. pop (); // remove a
inspection. push (s); // put a
}
}
}
Else
{
If ( ' ( ' = S)
{< br> // appear (when displayed, place it directly to the top
leftcount ++ ;< br> inspection. push (s);
}
Else If ( ' ) ' = S)
{
If ( " + -*/ " . Contains (inspection. Peek (). tostring ()))
{
// At the top of the stack is + -*/
// ) Cannot be directly connected. + -*/
Haserror = True ;
Break ;
}
Else If (Inspection. Contains ( ' ( ' ))
{
// Exclude the above situation and place ')' directly to the top.
Inspection. Push (s );
Rightcount ++ ;
}
Else
{< br> haserror = true ;< br> Break ;< BR >}
}
Else
{
If (Inspection. Count ! = 0 )
{
If ( ! " + -*/ " . Contains (S. tostring ()) && ! " + -*/ " . Contains (inspection. Peek (). tostring ()))
{< br> haserror = true ;< br> Break ;< BR >}
}
Inspection. Push (s );
}
}
}
If (Leftcount ! = Rightcount | " + -*/ " . Contains (inspection. Peek (). tostring ()))
{
//The left and right parentheses do not match or +-*/occurs at last.
Haserror= True;
}
Return haserror;
}