Cappuccino90

Untitled

Oct 11th, 2015
175
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. /**
  6.  * @author René Moll (188084), Philipp Mehlhorn (188308)
  7.  *
  8.  */
  9. public class Truthtable {
  10.     static ArrayList<String> array = new ArrayList<String>();
  11.     static int counter = 0;
  12.  
  13.     private static void printTruthTable(int n) {
  14.         //stopwatch start time
  15.         double zstVorher = System.currentTimeMillis();
  16.         n *= 2;
  17.  
  18.         int rows = (int) Math.pow(2, n);
  19.  
  20.         String tempString = "";
  21.  
  22.         // main for-loop. calculates every possible combination with Ds and Us for a specific length
  23.         for (int i = 0; i < rows; i++) {
  24.             for (int j = n - 1; j >= 0; j--) {
  25.                 // turns it into binary
  26.                 int temp = i / (int) Math.pow(2, j) % 2;
  27.                 // converts it into Ds and Us and saves it into a temporary string
  28.                 tempString = tempString.concat((temp == 1) ? "U" : "D");
  29.             }
  30.             // every temporary string gets sent to the checkstring method to get checked
  31.             if (tempString != "")
  32.                 checkString(tempString);
  33.             tempString = "";
  34.         }
  35.  
  36.         for (String s : array) {
  37.             System.out.println(s);
  38.         }
  39.  
  40.         //stopwatch end time
  41.         double zstNachher = System.currentTimeMillis();
  42.         System.out.println("\nZeit benötigt: " + ((zstNachher - zstVorher)) + " msec oder  "
  43.                 + ((zstNachher - zstVorher) / 1000) + " Sekunden");
  44.         System.out.println(counter + " Möglichkeiten für n=" + n / 2 + " (" + n + " Stellen)");
  45.     }
  46.    
  47.    
  48.     // checks the string for the rules that the excercise gave us
  49.     public static void checkString(String s) {
  50.         if ((s.contains("DUDUDU") && (countDUDU(s, "DUDU") <= 1))
  51.                 || (((countDUDU(s, "DUDU") == 2)) && !(s.contains("DUDUDU")))) {
  52.  
  53.             if (s.startsWith("U") && s.endsWith("D")) {
  54.                 if (checkAmountDU(s)) {
  55.                     if (countDUDU(s, "D") == countDUDU(s, "U")) {
  56.                         //if the path fullfills every rule, than it gets saved in an string array
  57.                         counter++;
  58.                         array.add(s);
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Counts the amount of search patterns in a string
  67.      * @param s the string that should be checked
  68.      * @param search the search pattern, that will be counted
  69.      * @return amount of the search string value in the string
  70.      */
  71.     public static int countDUDU(String s, String search) {
  72.  
  73.         Pattern p = Pattern.compile(search);
  74.         Matcher m = p.matcher(s);
  75.         int count = 0;
  76.         while (m.find()) {
  77.             count += 1;
  78.         }
  79.         return count;
  80.     }
  81.  
  82.    
  83.     /**
  84.      * @param s a DU path to check
  85.      * @return boolean true,if there are always more or equal Us than Ds
  86.      */
  87.     public static boolean checkAmountDU(String s) {
  88.         for (int i = 1; i <= s.length(); i++) {
  89.             if (!(countDUDU(s.substring(0, i), "D") <= countDUDU(s.substring(0, i), "U"))) {
  90.                 return false;
  91.             }
  92.         }
  93.         return true;
  94.     }
  95.  
  96.     public static void main(String[] args) {
  97.         // hier dein tatsächliches n angeben ! (n=5 hat 3 Möglichkeiten und 10 Stellen z.B.)
  98.         printTruthTable(15);
  99.     }
  100.  
  101. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment