Advertisement
196040

APS labs 6 Staticko rutiranje

Dec 15th, 2020
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.78 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class SLLNode<E> {
  5.     protected E element;
  6.     protected SLLNode<E> succ;
  7.     public SLLNode(E elem, SLLNode<E> succ) {
  8.         this.element = elem;
  9.         this.succ = succ;
  10.     }
  11.     @Override
  12.     public String toString() {
  13.         return element.toString();
  14.     }
  15. }
  16. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  17.  
  18.     // Each MapEntry object is a pair consisting of a key (a Comparable
  19.     // object) and a value (an arbitrary object).
  20.     K key;
  21.     E value;
  22.  
  23.     public MapEntry (K key, E val) {
  24.         this.key = key;
  25.         this.value = val;
  26.     }
  27.  
  28.     public int compareTo (K that) {
  29.         // Compare this map entry to that map entry.
  30.         @SuppressWarnings("unchecked")
  31.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  32.         return this.key.compareTo(other.key);
  33.     }
  34.  
  35.     public String toString () {
  36.         return "<" + key + "," + value + ">";
  37.     }
  38. }
  39. class CBHT<K extends Comparable<K>, E> {
  40.  
  41.     // An object of class CBHT is a closed-bucket hash table, containing
  42.     // entries of class MapEntry.
  43.     private SLLNode<MapEntry<K,E>>[] buckets;
  44.  
  45.     @SuppressWarnings("unchecked")
  46.     public CBHT(int m) {
  47.         // Construct an empty CBHT with m buckets.
  48.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  49.     }
  50.  
  51.     private int hash(K key) {
  52.         // Translate key to an index of the array buckets.
  53.         return Math.abs(key.hashCode()) % buckets.length;
  54.     }
  55.  
  56.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  57.         // Find which if any node of this CBHT contains an entry whose key is
  58.         // equal
  59.         // to targetKey. Return a link to that node (or null if there is none).
  60.         int b = hash(targetKey);
  61.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  62.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  63.                 return curr;
  64.         }
  65.         return null;
  66.     }
  67.  
  68.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  69.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  70.         int b = hash(key);
  71.         //   for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  72.         //  if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  73.         // Make newEntry replace the existing entry ...
  74.         //    curr.element = newEntry;
  75.         //     return;
  76.         // }
  77.         // }
  78.         // Insert newEntry at the front of the 1WLL in bucket b ...
  79.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  80.     }
  81.  
  82.     public void delete(K key) {
  83.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  84.         int b = hash(key);
  85.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  86.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  87.                 if (pred == null)
  88.                     buckets[b] = curr.succ;
  89.                 else
  90.                     pred.succ = curr.succ;
  91.                 return;
  92.             }
  93.         }
  94.     }
  95.  
  96.     public String toString() {
  97.         String temp = "";
  98.         for (int i = 0; i < buckets.length; i++) {
  99.             temp += i + ":";
  100.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  101.                 temp += curr.element.toString() + " ";
  102.             }
  103.             temp += "\n";
  104.         }
  105.         return temp;
  106.     }
  107.  
  108. }
  109. public class RoutingHashJava {
  110.     public static void main(String[] args) throws IOException {
  111.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  112.         int N = Integer.parseInt(br.readLine()); // broj na ruteri
  113.         CBHT<String, String> harrystyles = new CBHT<String, String>(100);
  114.         for (int i = 1; i <= N; i++) {
  115.             String adresa1 = br.readLine();
  116.             String adresa2 = br.readLine();
  117.             String[] cutall = adresa2.split("[,]");
  118.             String[] cutone = new String[10];
  119.             String union = "";
  120.             if (cutall.length != 0) {
  121.                 for (int j = 0; j < cutall.length; j++) {
  122.                     cutone = cutall[j].split("\\.");
  123.                     union = cutone[0] + "." + cutone[1] + "." + cutone[2];
  124.                     harrystyles.insert(adresa1, union);
  125.                 }
  126.             } else {
  127.                 cutone = adresa2.split("\\.");
  128.                 union = cutone[0] + "." + cutone[1] + "." + cutone[2];
  129.                 harrystyles.insert(adresa1, union);
  130.             }
  131.         }
  132.         int M = Integer.parseInt(br.readLine()); // br obidi
  133.         for (int i = 1; i <= M; i++) {
  134.             String adresa1 = br.readLine();
  135.             String adresa2 = br.readLine();
  136.             String[] cutone = adresa2.split("\\.");
  137.             String union = "<" + adresa1 + "," + cutone[0] + "." + cutone[1] + "." + cutone[2] + ">";
  138.             SLLNode help = harrystyles.search(adresa1);
  139.             if (help != null) {
  140.                 while (help != null) {
  141.                     if (help.element.toString().equals(union)) {
  142.                         System.out.println("postoi");
  143.                         break;
  144.                     }
  145.                     if(help.succ!=null)
  146.                         help = help.succ;
  147.                     else {
  148.                          System.out.println("ne postoi");
  149.                         break;
  150.                     }
  151.                 }
  152.             }
  153.             else System.out.println("ne postoi");
  154.         }
  155.     }
  156. }
  157. /*
  158. Потребно е да се симулира рутирање преку хеш табела. Секој рутер претставува една кофичка
  159. од хеш табелата и притоа пакетите на влез ги прима преку еден интерфејс. Бидејќи рутерот,
  160. рутирањето на даден пакет го врши користејќи ги статичките рути што тој ги знае,
  161. кога ќе добие пакет преку влезниот интерфејс, тој треба да даде одговор дали може да го
  162. рутира пакетот до дадениот уред во таа мрежа (postoi или nepostoi). Важно е тоа што сите
  163. адреси имаат мрежна маска /24, што значи дека последните 8 бита се наменети за адресирање.
  164. Претпоставуваме дека сите адреси се зафатени во таа мрежа, така што до било кој уред од таа мрежа,
  165. доколку ја има во рутирачката табела, може да се достави пакетот. Така што доколку во рутирачката
  166. табела има 10.10.10.0, тоа значи дека рутерот може да го проследи пакетот до сите уреди во таа мрежа
  167. (10.10.10.1- 10.10.10.254).
  168. На влез најпрвин се внесува бројот на рутери, потоа најизменично IP адресата на влезниот интерфејс,
  169. па во следниот ред IP адресите на мрежите до кој рутерот има статички рути. Потоа се внесува бројот
  170. на обиди за рутирање на пакети. Во следните редови најизменично се внесува влезен интерфејс и IP
  171. адреса на уред за која треба да се даде одговор дали тој рутер ја познава или не.
  172. Име на класта :RoutingHashJava
  173. */
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement