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
|
package org.unice.javaschool.actions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
public class Input {
/*
* Permet de definir un equivalent a scanf mais aussi de rendre la saisie de valeur par les lyceens
* plus simple.
*
*
* Explication du fonctionnement :
* Etape 1 : decomposition de la chaine en un tableau de caractere (exemple : s = "%s %d %f" t = {s,d,f}
* Etape 2 : verification qu'il existe pour chaque caractere un objet du bon type
* Etape 3 : faire le cast et assigner les resulats aux objets (fait en meme temps que l'étape 2)
*/
//constantes
private final char entier = 'i';
private final char flottant = 'f';
private final char decimal = 'd' ;
private final char caractere = 'c';
private final char string = 's';
//variables necessaires
private BufferedReader br ;
private String[] tokens;
private String[] entree;
public Input(){
br = new BufferedReader( new InputStreamReader( System.in ) );
}
private void decomposition(String s){
tokens = s.split("%");
try {
entree = br.readLine().split(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("tokens :"+tokens[0]);
System.out.println("tokens :"+tokens[1]);
System.out.println("entre : "+entree[0]);
}
private void association(Object a , Object ... laSuite){
/*
* On verifie que la taille du tableau de token vaut le nombre d'objet
* Si non concordence, on renvoie une exception
* Puis on verifie les types
*/
//-1 a cause du premier token qui est vide car split de "%i" donne " " puis "i"
if (tokens.length-1 == laSuite.length+1){
verification(tokens[1],a,entree[0]);
for (int i = 2 ; i < tokens.length ; ++i){
for (Object o : laSuite){
for (int j = 1 ; j < entree.length ; ++j){
verification(tokens[i],o,entree[j]);
}
}
}
}
}
private void verification(String s, Object a,String entree) {
// TODO Auto-generated method stub
System.out.println("Verification s : "+s);
switch(s.charAt(0)){
case entier :
if (a instanceof Integer){
a = Integer.parseInt(entree);
System.out.println("a : "+a);
}
break;
case flottant :
if (a instanceof Float){
a = Float.parseFloat(entree);
}
break;
case decimal :
if (a instanceof Double){
a = Double.parseDouble(entree);
}
break;
case caractere :
if (a instanceof Character){
a = entree.charAt(0) ;
}
break;
case string :
if (a instanceof String){
a = entree ;
}
break;
}
}
public void read(final String s, final Object a , final Object ... laSuite){
//la lecture s'effectue dans decomposition
decomposition(s);
association(a,laSuite);
}
} |
Partager