Determine whether an expression is written as a proposition formula Java

Source: Internet
Author: User

1 ImportJava.util.Scanner;2 3  Public classExpress {4          Public Static voidMain (string[] args)throwsexception{5System.out.println ("Please Enter expression:");6Scanner exp =NewScanner (system.in);7String expressnew =exp.nextline ();8             9             BooleanAnswer =Verify (expressnew);Ten              One             if(Answer = =true){ ASystem.out.println ("This expression is a proposition formula. "); -             } -             ElseSystem.out.println ("This expression is not a proposition formula. "); the Exp.close ();  -         } -          -          Public Static BooleanVerify (String exp) { +             inti,j; -             Charsign1,sign2; + String exp1,exp2; A              ati =exp.length (); -             //Single character -             if(i = = 1){ -SIGN1 = Exp.charat (i-1); -                 if(Sign1 >= ' a ' && sign1 <= ' z ' | | sign1 >= ' a ' && sign1 <= ' z '){ -                     return true; in                 } -                 Else return false; to             } +             if(I ==2 ){ -SIGN1 = Exp.charat (0); theSIGN2 = Exp.charat (1); *                 if(Sign1 = = '! ' && (sign2 >= ' a ' && sign2 <= ' z ' | | sign2 >= ' a ' && sign2 <= ' z ' )){ $                     return true;Panax Notoginseng                 } -                 Else return false; the             } +             if(i = = 3){ A                 if(Exp.charat (0) = = ' (' && exp.charat (2) = = ') '){ the                     returnVerify (String.valueof (Exp.charat (1))); +                 } -                 Else if(Exp.charat (1) = = ' & ' | | Exp.charat (1) = = ' | ' | | Exp.charat (1) = = ' ~ '){ $                     returnVerify (string.valueof (Exp.charat (0)) &&verify (String.valueof (Exp.charat (2))); $                 } -                 Else{ -                     return false; the                 } -             }Wuyi             if(I >= 4)//the length of the formula cannot be 2 and 3, so it is judged directly from a string of length 4 the             {  -                 if((Exp.charat (0) = = ' (') && (Exp.charat (i-1) = = ') '))//when the first and last symbol of a string is ' (' and ') ', it is possible that a formula Wu                 {    -                     if(Exp.charat (1) = = '! ')//when the second symbol is a negative sign, the case About                     {    $EXP1 = exp.substring (2, i-1);//take out the substring of the string sub1, recursively determine whether it is a formula -                         if(Verify (EXP1)) { -                             return true;  -}Else {    A                             return false;  +                         }    the}Else if(Exp.charat (1) = = ' (')//when the second symbol is ' (' the condition of the symbol -                     {    $j = Find (exp, 1, i-2);//Find (String,int,int) is used to determine the location of the ' (' matching ') ' the                         if(j==-1 | | j = = I-2 | | j = i-3) {//after matching the parentheses, you must leave two characters behind.  the                             return false;  the}Else {    theEXP1 = exp.substring (1, j+1);  -EXP2 = exp.substring (j + 2, i-1);  in         the                             if((Exp.charat (j + 1) = = ' & ' | | Exp.charat (j) = = ' | ' | | Exp.charat (1) = = ' ~ ')    the&& Verify (EXP1) &&Verify (EXP2)) {     About                                 return true;  the}Else {    the                                 return false;  the                             }    +                         }    -}Else if(Exp.charat (1) >= ' a ' && exp.charat (1) <= ' Z ' | | exp.charat (1) >= ' a ' && exp.charat (1) <= ' z ')//when the second symbol is atomic, take the remaining part the{//Divided into sub1, judge whether Sub2 is a formula,Bayi                         if(i = = 4) {    the                             return false;//This situation must be greater than or equal to 5 the}Else {    -                             if(Exp.charat (2) = = ' & ' | | Exp.charat (2) = = ' | ' | | Exp.charat (1) = = ' ~ ')//The string is the formula -                             {    theEXP1 = exp.substring (3, I-1);  the                                 if(Verify (EXP1)) { the                                     return true;  the}Else {    -                                     return false;  the                                 }    the}Else {    the                                 return false; 94                             }    the                         }    the}Else {    the                         return false; 98                     }    About}Else {    -                     return false; 101}//End >=4102}Else {   103                 return false;//Strings of other lengths and strings of other cases are not formulas104             }    the         }   106        107          Public Static intFind (String s,intBeginintend) {   108             intN1 = 0, N2 = 0, i =begin; 109              while(I <=end) {   the                 if(S.charat (i) = = ' (')   111n1++;  the                 Else if(S.charat (i) = = ') ')   113n2++;  the                 Else{    the                 }              the                 if(N1 = =n2)117                      Break; 118i++; 119             }    -             if(I > Begin && I <=end)121                 returni; 122             Else   123                 return-1; 124         }    the         126}

Determine whether an expression is written as a proposition formula Java

Related Article

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.