Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.util.*;
  2.    
  3. public class Molekula {
  4.     public ArrayList<Atom> atomy = new ArrayList<Atom>();
  5.     public int pridajAtom(Atom a, int[] susedneAtomy){
  6.         a.id = atomy.size();
  7.         for (int i = 0; i < susedneAtomy.length; i++){
  8.             a.vazby.add(susedneAtomy[i]);
  9.             for (Atom b: atomy){
  10.                 if (b.id == susedneAtomy[i]){
  11.                     a.ref.add(b);
  12.                 }
  13.             }
  14.             for (Atom nieco: atomy){
  15.                 if (nieco.id == susedneAtomy[i]){
  16.                     nieco.vazby.add(atomy.size());
  17.                     continue;
  18.                 }
  19.             }
  20.         }
  21.         atomy.add(a);
  22.         return a.id;
  23.        
  24.     }
  25.     public int pocetAtomov(){
  26.         int count = 0;
  27.         for (Atom a:atomy){
  28.             count++;
  29.         }
  30.         return count;
  31.     }
  32.     public boolean obsahujeNasobnuVazbu(int k){
  33.         for (Atom a: atomy){
  34.             for (int i: a.vazby){
  35.                 boolean raz = false;
  36.                 for (int u: a.vazby){
  37.                     if (i == u && raz == true){
  38.                         return true;
  39.                     }
  40.                     else if (i == u){
  41.                         raz = true;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         return false;
  47.     }
  48.     public String schematickaZnacka(){
  49.         String vysl = "";
  50.         String[] pole = new String[atomy.size()];
  51.         int index = 0;
  52.         for (Atom a: atomy){
  53.             pole[index] = a.nazovPrvku;
  54.             index++;
  55.         }  
  56.         for(int j=0; j<pole.length;j++){
  57.              for (int i=j+1 ; i<pole.length; i++){
  58.                  if(pole[i].compareTo(pole[j])<0){
  59.                      String temp= pole[j];
  60.                      pole[j]= pole[i];
  61.                      pole[i]=temp;
  62.                  }
  63.              }
  64.  
  65.          }
  66.         int count = 1;
  67.         String predchadzajuci = "";
  68.         for (String i: pole){
  69.             if (i.equals(predchadzajuci)){
  70.                 count++;
  71.             }
  72.             else if (!predchadzajuci.equals("")){
  73.                 vysl = vysl + predchadzajuci + count;
  74.                 predchadzajuci = i;
  75.                 count = 1;
  76.             }
  77.             else{
  78.                 predchadzajuci = i;
  79.             }
  80.         }
  81.         if (!predchadzajuci.equals("")){
  82.             vysl = vysl + predchadzajuci + count;
  83.         }
  84.         return vysl;
  85.     }
  86.     public boolean pridajVazbu(int a1, int a2){
  87.         for (Atom a: atomy){
  88.             if (a.id == a1 && a.vazby.size() < a.pocetVazieb){
  89.                 for (Atom b: atomy){
  90.                     if (b.id == a2 && b.vazby.size() < b.pocetVazieb){
  91.                         a.vazby.add(a2);
  92.                         b.vazby.add(a1);
  93.                         return true;
  94.                     }
  95.                 }
  96.             }
  97.             else{
  98.                 return false;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.     public boolean obsahujeCyklus(){
  104.         HashSet<Atom> navstivene = new HashSet<Atom>();
  105.         if (atomy.size() <= 2){
  106.             return false;
  107.         }
  108.         return rekurz(atomy.get(0), null, navstivene);
  109.     }
  110.     public boolean rekurz(Atom start, Atom odkial, HashSet<Atom> navstivene){
  111.         if (navstivene.contains(start)){
  112.             return true;
  113.         }
  114.         Set<Integer> set = new HashSet<Integer>();
  115.         for (int i: start.vazby){
  116.             set.add(i);
  117.         }
  118.         for (int i: set){
  119.             for (Atom a: atomy){
  120.                 if (a.id == i && a != odkial){
  121.                     navstivene.add(a);
  122.                     return rekurz(a, start, navstivene);
  123.                 }
  124.             }
  125.         }
  126.         return false;
  127.        
  128.        
  129.     }
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement