Bonjours à tous, aujourd'hui, j'ai reçus un exercice d'algorithmie, celui-ci consiste a traduire un algorithme écrit sous Algobox en Javascool;
Le code Algobox est ici :
1 VARIABLES
2 a EST_DU_TYPE NOMBRE
3 b EST_DU_TYPE NOMBRE
4 c EST_DU_TYPE NOMBRE
5 n EST_DU_TYPE NOMBRE
6 nb_disques EST_DU_TYPE NOMBRE
7 pile EST_DU_TYPE LISTE
8 sommet EST_DU_TYPE NOMBRE
9 msg EST_DU_TYPE CHAINE
10 tour1 EST_DU_TYPE CHAINE
11 tour2 EST_DU_TYPE CHAINE
12 tour3 EST_DU_TYPE CHAINE
13 i EST_DU_TYPE NOMBRE
14 disque EST_DU_TYPE CHAINE
15 compteur EST_DU_TYPE NOMBRE
16 DEBUT_ALGORITHME
17 LIRE nb_disques
18 SI (nb_disques>2 && nb_disques<8) ALORS
19 DEBUT_SI
20 n PREND_LA_VALEUR nb_disques
21 tour2 PREND_LA_VALEUR "Tour 2 : "
22 tour3 PREND_LA_VALEUR "Tour 3 : "
23 POUR i ALLANT_DE 1 A n
24 DEBUT_POUR
25 tour1 PREND_LA_VALEUR i.toString()+tour1
26 FIN_POUR
27 tour1 PREND_LA_VALEUR "Tour 1 : "+tour1
28 AFFICHER tour1
29 AFFICHER tour2
30 AFFICHER tour3
31 compteur PREND_LA_VALEUR 0
32 a PREND_LA_VALEUR 1
33 b PREND_LA_VALEUR 3
34 c PREND_LA_VALEUR 2
35 pile[1] PREND_LA_VALEUR a:b:c:n
36 sommet PREND_LA_VALEUR 4
37 TANT_QUE (sommet>0) FAIRE
38 DEBUT_TANT_QUE
39 n PREND_LA_VALEUR pile[sommet]
40 c PREND_LA_VALEUR pile[sommet-1]
41 b PREND_LA_VALEUR pile[sommet-2]
42 a PREND_LA_VALEUR pile[sommet-3]
43 sommet PREND_LA_VALEUR sommet-4
44 SI (n==1) ALORS
45 DEBUT_SI
46 AFFICHER "**********"
47 compteur PREND_LA_VALEUR compteur+1
48 msg PREND_LA_VALEUR "Déplacement N°"+compteur.toString()+" : Tour "+a.toString()+" -> Tour "+b.toString()
49 AFFICHER msg
50 SI (a==1) ALORS
51 DEBUT_SI
52 disque PREND_LA_VALEUR tour1.substr(tour1.length-1,1)
53 tour1 PREND_LA_VALEUR tour1.substr(0,tour1.length-1)
54 FIN_SI
55 SI (a==2) ALORS
56 DEBUT_SI
57 disque PREND_LA_VALEUR tour2.substr(tour2.length-1,1)
58 tour2 PREND_LA_VALEUR tour2.substr(0,tour2.length-1)
59 FIN_SI
60 SI (a==3) ALORS
61 DEBUT_SI
62 disque PREND_LA_VALEUR tour3.substr(tour3.length-1,1)
63 tour3 PREND_LA_VALEUR tour3.substr(0,tour3.length-1)
64 FIN_SI
65 SI (b==1) ALORS
66 DEBUT_SI
67 tour1 PREND_LA_VALEUR tour1+disque
68 FIN_SI
69 SI (b==2) ALORS
70 DEBUT_SI
71 tour2 PREND_LA_VALEUR tour2+disque
72 FIN_SI
73 SI (b==3) ALORS
74 DEBUT_SI
75 tour3 PREND_LA_VALEUR tour3+disque
76 FIN_SI
77 AFFICHER tour1
78 AFFICHER tour2
79 AFFICHER tour3
80 FIN_SI
81 SINON
82 DEBUT_SINON
83 pile[sommet+1] PREND_LA_VALEUR c:b:a:n-1:a:b:c:1:a:c:b:n-1
84 sommet PREND_LA_VALEUR sommet+12
85 FIN_SINON
86 FIN_TANT_QUE
87 FIN_SI
88 FIN_ALGORITHME
Mais, dans mon programme sous javascool, j'ai toujours une erreur concernant un tableau;
void main() {
/*********************************/
int a;
int b;
int c;
int n;
int nb_disques;
List pile;
int sommet;
String msg;
String tour1;
String tour2;
String tour3;
int i;
String disque;
int compteur;
/*********************************/
nb_disques = readInt();
if (nb_disques >2 && nb_disques <8) {
n = nb_disques;
tour2 = "Tour 2 : ";
tour3 = "Tour 3 : ";
for (i =0; i <24; i ++) {
tour1 = i.toString() + tour1;
}
tour1 = "Tour 1 : " + tour1;
println(tour1);
println(tour2);
println(tour3);
compteur = 0;
a = 1;
b = 3;
c = 2;
pile[1] = { a : b : c : n};
sommet = 4;
while (sommet >0) {
n = pile[sommet];
c = pile[sommet -1];
b = pile[sommet -2];
a = pile[sommet - 3 ];
sommet =sommet -4;
if (n ==1) {
println("**********");
compteur = compteur +1;
msg = "Déplacement N°" + compteur.toString() + " : Tour " + a.toString() + " -> Tour " + b.toString();
println(msg);
if (a ==1) {
disque = tour1.substr(tour1.length -1, 1);
tour1 = tour1.substr(0, tour1.length -1);
}
if (a ==2) {
disque = tour2.substr(tour2.length -1, 1);
tour2 = tour2.substr(0, tour2.length -1);
}
if (a ==3) {
disque = tour3.substr(tour3.length -1, 1);
tour3 = tour3.substr(0, tour3.length -1);
}
if (b ==1) {
tour1 = tour1 + disque;
}
if (b ==2) {
tour2 = tour2 + disque;
}
if (b ==3) {
tour3 = tour3 + disque;
}
println(tour1);
println(tour2);
println(tour3);
} else {
pile[sommet +1] = c : b : a : n -1 : a : b : c :1 : a : c : b : n -1;
sommet = sommet + 12;
}
}
}
La console m'indique que :
Erreur de syntaxe ligne 24 :
error: (illegal start of expression) L'instruction (ou la précédente) est tronquée ou mal écrite
pile[1] = {a,b,c,n};
^
Je ne comprend pas trop le problème, le tableau sous javascool n'est-il pas déclarer comme
tableau[]={} ?