/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2005 */ public class TracageAlgorithme2 { public static int cpt = 1; /* Fonction de generation d'une chaine de caracteres */ /* representant un entier avec formatage de texte */ /* justifie a gauche (complement avec des espaces) */ /* a concurence de n caracteres */ /* val : valeur a transformer en texte */ /* n : nombre de caracteres a generer */ /* Resultat de la fonction : String */ public static String formatEspaces(int val,int n) { String v =""+val; int l = v.length(); if ( l == n ) { return(v); } if ( l <= n ) { String s = new String(); for ( int i = 0 ; i < n-l ; i++ ) s += " "; return(s+v); } return(v.substring(0,n)); } /* Fonction d'affichage d'une ligne de la trace */ /* l : message a afficher */ /* k : variable k dont le contenu est a afficher */ /* i : variable i dont le contenu est a afficher */ /* j : variable j dont le contenu est a afficher */ public static void afficheTrace(String l,int k,int i,int j) { System.out.println(cpt+l+formatEspaces(k,3)+" "+ formatEspaces(i,4)+" "+ formatEspaces(j,2)); cpt++; } /* Fonction principale */ /* Algorithme avec trace */ /* sous forme d'affichage a l'ecran */ public static void main(String [] args) { int k,i,j = -100; k = -4; i = 0; afficheTrace(" 05 ",k,i,j); while ( k < 0 ) { afficheTrace(" 06 ",k,i,j); for ( j = 1,afficheTrace(" 07 ",k,i,j) ; j <= -k ; j = j+2,afficheTrace(" 07 ",k,i,j) ) { i = i+k*j; afficheTrace(" 08 ",k,i,j); afficheTrace(" 09 ",k,i,j); } k = k+1; afficheTrace(" 10 ",k,i,j); afficheTrace(" 11 ",k,i,j); } afficheTrace(" 06 ",k,i,j); afficheTrace(" 12 ",k,i,j); System.out.println(i); afficheTrace(" 13 ",k,i,j); } }