Check whether the arithmetic expression is correct (including +-*/() Six expressions)

Source: Internet
Author: User

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;
}

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.