Combined (analysis) paradigm-to-main (analysis) taking Paradigm--"Java implementation

Source: Internet
Author: User

This time the teacher assigned the following machine work, not limited to language. After thinking, asked a few big gods, said a bunch of not know what Ghost algorithm name ....

After some Baidu, found that Java can contain libraries and then use some JavaScript functions, where the eval () function can calculate a string, and execute the JavaScript code in it. Http://www.w3school.com.cn/jsref/jsref_eval.asp thought of the following solution:

1 ImportJavax.script.ScriptEngine;2 ImportJavax.script.ScriptEngineManager;3 Importjavax.script.ScriptException;4 ImportJava.util.*;5 6 7 /**8 * creat by QJM9 */Ten  Public classTest { One  A     //A- z 65-90 -     //A- z 97-122 -      Public StaticMap<integer, integer> m =NewHashmap<integer, integer>(); the      Public Static intCount = 0; -      Public StaticList<string> List_true_value =NewArraylist<string>(); -  -     /** + * Get primary and Fetch (disjunction) Paradigm - * String@paramproblem The proposition formula to be converted + * int@paramflag Mark (1: And Fetch 2: disjunction) A      * at      * @return -      * @throwsscriptexception -      */ -      Public StaticString Getmainnormalform (String problem,intFlagthrowsscriptexception { -Scriptenginemanager Manager =NewScriptenginemanager (); -ScriptEngine engine = Manager.getenginebyname ("JS"); inProblem =Problem.trim (). toUpperCase (); -         Char[] Pro_arr =Problem.tochararray (); toSet<string> s_variable =NewLinkedhashset<string>(); +          for(CharTemp:pro_arr) { -             if(Temp >= && temp <= 90) { theS_variable.add (temp + ""); *                 //System.out.println (temp+ ""); $             }Panax Notoginseng         } -  the  +string[] variables = (string[]) S_variable.toarray (NewString[0]); A arrays.sort (variables); theSystem.out.println ("\ n Truth Assignment Order:" +arrays.tostring (variables)); + method (engine, variables, variables.length, problem); -String result1 = "Σ ("; $String result2 = "∏ ("; $          for(Integer i:m.keyset ()) { -             if(M.get (i) = = 1) { -RESULT1 + = "M" + i + ","; the}Else { -RESULT2 + = "M" + i + ",";Wuyi             } the         } -RESULT1 = result1.substring (0, Result1.length ()-1); WuRESULT2 = result2.substring (0, Result2.length ()-1); -RESULT1 + = ")"; AboutRESULT2 + = ")"; $  -Count = 0; - m.clear (); -  A         returnFlag = = 1?result1:result2; +     } the  -  $     /** the * Recursive function to assign truth value to each proposition, and to derive the result of each kind of truth assignment the * ScriptEngine@paramengine the * string[]@paramvariables the * int@paramthe number of times proposition arguments - * String@paramproblem The proposition formula to be converted in * Canceling the commented out printout, you can see the specific execution process of the algorithm the      * @throwsscriptexception the      */ About      Public Static voidMethod (ScriptEngine engine, string[] variables,intTimes, String problem)throwsscriptexception { the         if(Times < 1) { theInteger result =(Integer) engine.eval (problem); the              +             //System.out.println ("" + Problem + "); -             //System.out.println ("Result type:" + Result.getclass (). GetName () + ", calculation result:" + result + ""); the M.put (count, result);Bayicount++; the             //System.out.println (Transmaptostring (m)); the             return; -}Else { -              for(inti = 0; I < 2; i++) { theEngine.put (Variables[variables.length-Times ], i); the                 //System.out.println ("\ntimes:" + Times + "\nbool:" + variables[times-1] + "--->" + i); theMethod (engine, variables, times-1, problem); the             } -         } the  the     } the 94     /** the     /** the * Method Name: transmaptostring the * Incoming parameters: Map98 * return value: String shape username ' chenziwen^password ' 1234 About      */ -      Public StaticString transmaptostring (map map) {101Set s =Map.keyset ();102String temp = "";103          for(Object i:s) {104Temp + = "key:" + (Integer) i + "\t\tvalue:" + map.get (i) + "\ n"; the         }106         returntemp;107     }108 109  the      Public Static voidMain (string[] args)throwsscriptexception {111String problem = ""; the         intFlag = 0;113Scanner s =NewScanner (system.in); the          while(true) { theSystem.out.println ("Please enter the canonical form to be converted (&-> |-> extract!-> non):"); the             Try{117Problem =S.next ();118}Catch(Nosuchelementexception exception) {119System.out.println ("Mdzz"); -System.exit (0);121             }122SYSTEM.OUT.PRINTLN ("Proposition formula to be converted:" +problem);123System.out.println ("The following is the corresponding truth table");124System.out.println ("Main collection paradigm:" + getmainnormalform (problem, 1)); theSystem.out.println ("Main disjunction paradigm:" + getmainnormalform (problem, 2) + "\ n");126         }127  -     }129}

The basic ideas are:

    • First, engine.eval (problem); You can run the string directly as a mathematical expression.
    • And then
      Engine.put (Variables[variables.length-Times ], i); you can directly convert the specified characters in the string to the target data, so that you can implement the truth assignment for each propositional variable of the expression. 
    • Then the recursive function is used to realize the traversal of the truth-table method, and then the truth value of the expression of the truth assignment is recorded, and then the corresponding expression is converted.

The following is a run

Combined (analysis) paradigm-to-main (analysis) taking Paradigm--"Java implementation

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.