Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Molekula {
- public ArrayList<Atom> atomy = new ArrayList<Atom>();
- public int pridajAtom(Atom a, int[] susedneAtomy){
- a.id = atomy.size();
- for (int i = 0; i < susedneAtomy.length; i++){
- a.vazby.add(susedneAtomy[i]);
- for (Atom b: atomy){
- if (b.id == susedneAtomy[i]){
- a.ref.add(b);
- }
- }
- for (Atom nieco: atomy){
- if (nieco.id == susedneAtomy[i]){
- nieco.vazby.add(atomy.size());
- continue;
- }
- }
- }
- atomy.add(a);
- return a.id;
- }
- public int pocetAtomov(){
- int count = 0;
- for (Atom a:atomy){
- count++;
- }
- return count;
- }
- public boolean obsahujeNasobnuVazbu(int k){
- for (Atom a: atomy){
- for (int i: a.vazby){
- boolean raz = false;
- for (int u: a.vazby){
- if (i == u && raz == true){
- return true;
- }
- else if (i == u){
- raz = true;
- }
- }
- }
- }
- return false;
- }
- public String schematickaZnacka(){
- String vysl = "";
- String[] pole = new String[atomy.size()];
- int index = 0;
- for (Atom a: atomy){
- pole[index] = a.nazovPrvku;
- index++;
- }
- for(int j=0; j<pole.length;j++){
- for (int i=j+1 ; i<pole.length; i++){
- if(pole[i].compareTo(pole[j])<0){
- String temp= pole[j];
- pole[j]= pole[i];
- pole[i]=temp;
- }
- }
- }
- int count = 1;
- String predchadzajuci = "";
- for (String i: pole){
- if (i.equals(predchadzajuci)){
- count++;
- }
- else if (!predchadzajuci.equals("")){
- vysl = vysl + predchadzajuci + count;
- predchadzajuci = i;
- count = 1;
- }
- else{
- predchadzajuci = i;
- }
- }
- if (!predchadzajuci.equals("")){
- vysl = vysl + predchadzajuci + count;
- }
- return vysl;
- }
- public boolean pridajVazbu(int a1, int a2){
- for (Atom a: atomy){
- if (a.id == a1 && a.vazby.size() < a.pocetVazieb){
- for (Atom b: atomy){
- if (b.id == a2 && b.vazby.size() < b.pocetVazieb){
- a.vazby.add(a2);
- b.vazby.add(a1);
- return true;
- }
- }
- }
- else{
- return false;
- }
- }
- return false;
- }
- public boolean obsahujeCyklus(){
- HashSet<Atom> navstivene = new HashSet<Atom>();
- if (atomy.size() <= 2){
- return false;
- }
- return rekurz(atomy.get(0), null, navstivene);
- }
- public boolean rekurz(Atom start, Atom odkial, HashSet<Atom> navstivene){
- if (navstivene.contains(start)){
- return true;
- }
- Set<Integer> set = new HashSet<Integer>();
- for (int i: start.vazby){
- set.add(i);
- }
- for (int i: set){
- for (Atom a: atomy){
- if (a.id == i && a != odkial){
- navstivene.add(a);
- return rekurz(a, start, navstivene);
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement