1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class plateau {
int vecteur[];
int valeurs[];
int longueurs[];
int getNbCases() throws NumberFormatException, IOException
{
int nb;
do
{
System.out.println("Donnez le nombre de cases (entre 1 et 10):");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
nb = Integer.parseInt(in.readLine());
if (nb < 0 || nb > 10) System.out.println("Veuillez entrer un nombre non nul non supérieur à 10.");
} while (nb < 0 || nb > 10);
return nb;
}
int TailleValeursLongueurs()
{
int taille = 1;
for (int i = 0; i < this.vecteur.length - 1; i++)
{
if (this.vecteur[i + 1] != this.vecteur[i]) taille++;
}
return taille;
}
int [] initialisationVecteur(int nb) throws NumberFormatException, IOException
{
this.vecteur = new int[nb];
return vecteur;
}
int [] initialisationValeurs() throws NumberFormatException, IOException
{
this.valeurs = new int[this.TailleValeursLongueurs()];
return valeurs;
}
int [] initialisationLongueurs() throws NumberFormatException, IOException
{
this.longueurs = new int[this.TailleValeursLongueurs()];
return longueurs;
}
int [] remplirVecteur() throws NumberFormatException, IOException
{
this.vecteur = this.initialisationVecteur(this.getNbCases());
System.out.println("Donnez les valeurs (nb entiers) du vecteur (valeurs positifs et/ou négatives): ");
for (int i = 0; i < vecteur.length; i++)
{
System.out.println("Case " + (i + 1) + ":");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
vecteur[i] = Integer.parseInt(in.readLine());
}
return vecteur;
}
int addCumTabLongueur()
{
int somme = 0;
for (int i = 0; i < this.longueurs.length; i++)
{
if (longueurs[i] == 0) break;
somme += longueurs[i];
}
return somme;
}
int addCumTabLongueur2(int longueur)
{
int somme = 0;
for (int i = 0; i < longueur; i++)
{
somme += longueurs[i];
}
return somme;
}
void remplirLongueurs()
{
int compteurValeur = 0;
int compteurVecteur = 0;
for (int i = 0; i < this.longueurs.length; i++)
{
for (int j = compteurVecteur; j < this.vecteur.length - 1; j++)
{
if (this.vecteur[j] == this.vecteur[j + 1])
{
compteurValeur++;
compteurVecteur++;
}
if (this.vecteur[j] != this.vecteur[j + 1]) break;
}
this.longueurs[i] = (compteurValeur + 1);
compteurVecteur = addCumTabLongueur();
compteurValeur = 0;
}
}
void affichageVecteur()
{
for (int i = 0; i < this.vecteur.length; i++)
{
System.out.print(vecteur[i] + " ");
}
}
void affichageValeurs()
{
for (int i = 0; i < this.valeurs.length; i++)
{
System.out.print(valeurs[i] + " ");
}
}
void affichageLongueurs()
{
for (int i = 0; i < this.longueurs.length; i++)
{
System.out.print(longueurs[i] + " ");
}
System.out.println();
}
void affichageLineaire(){
System.out.print("Résultat compacté: ");
for (int i = 0; i < this.longueurs.length; i++){
System.out.print(this.longueurs[i] + " " + this.valeurs[i] + " | ");
}
}
void remplirValeurs()
{
int compteurVecteur = 0;
for (int i = 0; i < this.valeurs.length; i++)
{
if (compteurVecteur == (this.vecteur.length - 1))
{
this.valeurs[this.valeurs.length - 1] = this.vecteur[compteurVecteur];
break;
}
for (int j = compteurVecteur; j < this.vecteur.length - 1; j++)
{
if (this.vecteur[j] != this.vecteur[j + 1])
{
this.valeurs[i] = this.vecteur[j];
break;
}
}
compteurVecteur = this.addCumTabLongueur2(i + 1);
}
}
public static void main(String [] args) throws NumberFormatException, IOException
{
plateau p = new plateau();
p.remplirVecteur();
p.initialisationValeurs();
p.initialisationLongueurs();
p.remplirLongueurs();
p.remplirValeurs();
System.out.println();
System.out.print("Vecteur: ");
p.affichageVecteur();
System.out.println();
System.out.print("Tableau Valeurs: ");
p.affichageValeurs();
System.out.println();
System.out.print("Tableau Longueurs: ");
p.affichageLongueurs();
p.affichageLineaire();
}
} |