Created
June 1, 2015 15:12
-
-
Save jy95/f68e1f40d232153fb01f to your computer and use it in GitHub Desktop.
Suite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Classe Suite | |
*/ | |
import java.util.*; | |
import java.io.*; | |
public class Suite extends SuiteDeBase { | |
// valeur numérique de MAXELT | |
private static final int MAX = Elt.MAXELT.val(); | |
/** Constructeur vide */ | |
public Suite() { | |
super(); | |
} | |
/** Constructeur par recopie */ | |
public Suite(SuiteDeBase s) { | |
super(s); | |
} | |
/** Constructeur à partir d'une String */ | |
public Suite(String st) { | |
super(st); | |
} | |
/** Constructeur à partir d'un Elt et d'une Suite */ | |
public Suite(Elt t, SuiteDeBase c) { | |
super(t, c); | |
} | |
/** Construit la Suite réduite à (x) */ | |
public Suite(Elt x) { | |
this(); | |
this.ajouter(x); | |
} | |
public Suite corps() { | |
return (Suite) super.corps(); | |
} | |
/** Renvoie la longueur de la Suite courante */ | |
public int longueur() { | |
// Recursif | |
/*if (this.estVide()) { | |
return 0; | |
} | |
return 1+this.corps().longueur();*/ | |
// Iteratif | |
int result = 0; | |
for (Elt current : this) { | |
result++; | |
} | |
return result; | |
} | |
/** renvoie TRUE si e a au moins une occurrence | |
dans la Suite courante*/ | |
public boolean contient(Elt e){ | |
//Recursif | |
/*if (this.estVide()) { | |
return false; | |
} | |
if (this.tete().estEgalA(e)) { | |
return true; | |
} | |
return this.corps().contient(e);*/ | |
//Iteratif | |
for (Elt current : this) { | |
if (current.estEgalA(e)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** renvoie le nombre d'occurrences de e dans la Suite courante*/ | |
public int nombreOccur(Elt e){ | |
//Recursif | |
/*int result; | |
if (this.estVide()) { | |
result = 0; | |
}else { | |
result = nombreOccurSiNonNull(e); | |
} | |
return result; | |
} | |
private int nombreOccurSiNonNull(Elt e){ | |
if (this.tete().estEgalA(e)) { | |
return 1 + this.corps().nombreOccur(e); | |
} | |
return this.corps().nombreOccur(e);*/ | |
//Iteratif | |
int result= 0; | |
for (Elt current : this) { | |
if (current.estEgalA(e)) { | |
result++; | |
} | |
} | |
return result; | |
} | |
/** renvoie la position de la première occurrence de e dans la Suite courante ; renvoie 0 si e n'a pas | |
d'occurrence dans la Suite courante */ | |
public int position(Elt e){ | |
//Iteratif | |
int result = 1; | |
for (Elt current : this) { | |
if (current.estEgalA(e)) { | |
return result; | |
} | |
result++; | |
} | |
return 0; | |
//Recursif | |
/* | |
if (this.estVide()) { | |
return 0; | |
} | |
if (this.tete().estEgalA(e)) { | |
return 1; | |
} | |
int temp = this.corps().position(e); | |
if (temp==0) { | |
return 0; | |
} | |
return 1+temp; | |
*/ | |
} | |
/** renvoie la position de la première occurrence de e dans la Suite courante ; renvoie 0 si e n'a pas | |
d'occurrence dans la Suite courante */ | |
//Van der Meulen solution avec previous elt comptabilise | |
/* public int position(Elt e) { | |
return position(e, 0); | |
} | |
private int position(Elt e, int prev) { | |
if (estVide()) { | |
return 0; | |
} else if (tete().estEgalA(e)) { | |
return 1 + prev; | |
} else { | |
return corps().position(e, prev + 1); | |
} | |
}*/ | |
/** renvoie le i-ème élément de la Suite courante s'il existe ; renvoie null sinon*/ | |
public Elt i_eme(int i){ | |
if (this.estVide()) { | |
return null; | |
} | |
if (i <= 0) { | |
return null; | |
} | |
if (i == 1) { | |
return this.tete(); | |
} | |
return this.corps().i_eme(i-1); | |
} | |
/** renvoie le dernier élément de la Suite courante si elle est non vide, renvoie null sinon*/ | |
public Elt dernier() { | |
Elt result; | |
if (this.estVide()) { | |
result = null; | |
} else { | |
result = dernierSiNonVide(); | |
} | |
return result; | |
} | |
private Elt dernierSiNonVide() { | |
Elt result; | |
Suite corps = corps(); | |
if (corps.estVide()) { | |
result = tete(); | |
} else { | |
result = corps.dernierSiNonVide(); | |
} | |
return result; | |
} | |
/** renvoie true si la Suite courante est égale à s*/ | |
public boolean estEgaleA(Suite s){ | |
if (this.estVide() && s.estVide()) { | |
return true; | |
} | |
if (this.estVide() && !s.estVide()) { | |
return false; | |
} | |
if (!this.estVide() && s.estVide()) { | |
return false; | |
} | |
if (this.tete().estEgalA(s.tete())) { | |
return this.corps().estEgaleA(s.corps()); | |
}else { | |
return false; | |
} | |
} | |
/** renvoie true si la Suite courante est un préfixe de s*/ | |
public boolean prefixe(Suite s){ | |
if (this.estVide() && s.estVide()) { | |
return true; | |
} | |
if (this.estVide() && !s.estVide()) { | |
return true; | |
} | |
if (!this.estVide() && s.estVide()) { | |
return false; | |
} | |
if (s.tete().estEgalA(this.tete())) { | |
return this.corps().prefixe(s.corps()); | |
} | |
return false; | |
} | |
/** renvoie true si la Suite courante est une sous-suite de s*/ | |
public boolean sousSuite(Suite s){ | |
if (this.estVide() && s.estVide()) { | |
return true; | |
} | |
if (this.estVide() && !s.estVide()) { | |
return false; | |
} | |
if (!this.estVide() && s.estVide()) { | |
return true; | |
} | |
if (s.tete().estEgalA(this.tete())) { | |
return this.corps().sousSuite(s.corps()); | |
} | |
return this.sousSuite(s.corps()); | |
} | |
/** ajoute s à gauche de la Suite courante*/ | |
public void ajouter(Suite s){ | |
if (!s.estVide()) { | |
ajouter(s.corps()); | |
this.ajouter(s.tete()); | |
} | |
} | |
/** ajoute s à l'envers et à gauche de la Suite courante*/ | |
public void ajouterALEnvers(Suite s){ | |
if (!s.estVide()) { | |
this.ajouter(s.tete()); | |
ajouter(s.corps()); | |
} | |
} | |
/** renvoie la Suite courante inversée*/ | |
public Suite inverse(){ | |
Suite s = new Suite(); | |
if (this.estVide()) { | |
return s; | |
} | |
s.ajouter(this.tete()); | |
s.ajouter(this.corps().inverse()); | |
return s; | |
} | |
/** renvoie une Suite contenant, une et une seule fois, tous les éléments de la Suite courante*/ | |
public Suite reduite(){ | |
if (this.estVide()) { | |
return new Suite(); | |
} | |
if (!this.corps().contient(this.tete())) { | |
return new Suite(this.tete(), this.corps().reduite()); | |
}else { | |
return new Suite(this.corps().reduite()); | |
} | |
} | |
/** renvoie une copie de la Suite courante dans laquelle on a supprimé la première occurrence (éventuelle) de x*/ | |
public Suite moinsPremOcc(Elt x){ | |
if (this.estVide()) { | |
return new Suite(); | |
} | |
if (!this.tete().estEgalA(x)) { | |
return new Suite(this.tete(), this.corps().moinsPremOcc(x)); | |
}else { | |
return new Suite(this.corps().moinsPremOcc(x)); | |
} | |
} | |
/** renvoie une copie de la Suite courante dans laquelle on a supprimé toutes les occurrences de x*/ | |
public Suite moinsToutesOcc(Elt x){ | |
if (this.estVide()) { | |
return new Suite(); | |
} | |
if (!this.tete().estEgalA(x)) { | |
return new Suite(this.tete(), this.corps().moinsToutesOcc(x)); | |
}else { | |
return new Suite(this.corps().moinsToutesOcc(x)); | |
} | |
} | |
/** renvoie une copie de la Suite courante dans laquelle toutes les occurrences de x ont été remplacées par y*/ | |
public Suite substitut(Elt x, Elt y){ | |
if (this.estVide()) { | |
return new Suite(); | |
} | |
if (!this.tete().estEgalA(x)) { | |
return new Suite(this.tete(), this.corps().substitut(x, y)); | |
}else { | |
return new Suite(y, this.corps().substitut(x, y)); | |
} | |
} | |
/** renvoie une Suite constituee de tous les Elt ayant plus d'une occurrence dans la Suite courante */ | |
public Suite doublons(){ | |
if (this.estVide()) { | |
return new Suite(); | |
} | |
if (this.corps().contient(this.tete())) { | |
return new Suite(this.tete(), this.corps().doublons()); | |
}else { | |
return new Suite(this.corps().doublons()); | |
} | |
} | |
/** renvoie true ssi la Suite courante contient au moins k Elt distincts */ | |
public boolean auMoinsK(int k){ | |
if (k < 1) { | |
return true; | |
} | |
if (this.estVide()) { | |
return false; | |
} | |
if (this.corps().contient(this.tete())) { | |
return this.corps().auMoinsK(k-1); | |
} | |
return this.corps().auMoinsK(k); | |
} | |
/** renvoie true ssi tous les element de la Suite sont distincts */ | |
public boolean tousDistincts(){ | |
if (this.estVide()) { | |
return true; | |
} | |
if (this.corps().contient(this.tete())) { | |
return false; | |
} | |
return this.corps().tousDistincts(); | |
} | |
} // class Suite | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment