Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
G2_MSAYIF_BASSEM_G2_LE_HAIDANG TP1-TP2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MSAYIF Bassem
G2_MSAYIF_BASSEM_G2_LE_HAIDANG TP1-TP2
Commits
03bf060e
Commit
03bf060e
authored
4 years ago
by
MSAYIF Bassem
Browse files
Options
Downloads
Patches
Plain Diff
Updated Crossword Class (Task 2) + Updated Main Class
-Implémentation de la recherche d’un mot dans le tableau Crossword
parent
450f277d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tp2/Crossword.java
+139
-96
139 additions, 96 deletions
tp2/Crossword.java
tp2/Main.java
+21
-12
21 additions, 12 deletions
tp2/Main.java
with
160 additions
and
108 deletions
tp2/Crossword.java
+
139
−
96
View file @
03bf060e
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Scanner
;
import
java.util.Scanner
;
import
java.io.*
;
import
java.io.*
;
...
@@ -6,8 +7,7 @@ import java.io.*;
...
@@ -6,8 +7,7 @@ import java.io.*;
* Class Crossword : Creates a 2D array of characters read from the input file
* Class Crossword : Creates a 2D array of characters read from the input file
*/
*/
public
class
Crossword
public
class
Crossword
{
{
private
char
array
[][];
// Tableaux 2D contenant les caractères
private
char
array
[][];
// Tableaux 2D contenant les caractères
private
int
rows
;
// Nombres de lignes de array
private
int
rows
;
// Nombres de lignes de array
private
int
columns
;
// Nombres de colonnes de array
private
int
columns
;
// Nombres de colonnes de array
...
@@ -58,30 +58,64 @@ public class Crossword
...
@@ -58,30 +58,64 @@ public class Crossword
*** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/
*** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/
public
boolean
search
(
String
word
)
{
public
boolean
search
(
String
word
)
{
// Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire.
// Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire.
if
(
word
.
length
()
<=
0
)
{
return
false
;
}
for
(
int
j
=
0
;
j
<
columns
;
++
j
)
{
for
(
int
i
=
0
;
i
<
rows
;
++
i
)
{
if
(
searchRow
(
j
,
i
,
word
))
{
PositionX
=
j
;
EndX
=
PositionX
+
word
.
length
()
-
1
;
PositionY
=
i
;
EndY
=
PositionY
;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return
true
;
}
else
if
(
searchColumn
(
j
,
i
,
word
))
{
PositionX
=
j
;
EndX
=
PositionX
;
PositionY
=
i
;
EndY
=
PositionY
+
word
.
length
()
-
1
;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return
true
;
}
// Chercher le premier caractère du mot "word" dans le tableau array.
}
// Si array[i][j] contient ce caractère, alors le mot peut apparaître dans le même ligne,
// ou dans le même colonne. Utiliser les methodes SearchRow() ou searchColumn() selon le cas.
}
return
false
;
// mot pas trouvé
return
false
;
// mot pas trouvé
}
}
/* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */
/* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchRow
(
int
y
,
int
x
,
String
word
)
{
private
boolean
searchRow
(
int
y
,
int
x
,
String
word
)
{
int
length
=
word
.
length
();
// Ecrire code ici ...
String
wordfound
=
""
;
return
false
;
for
(
int
z
=
0
;
z
<
length
;
++
z
)
{
if
(
y
<
this
.
columns
)
{
if
(
array
[
x
][
y
]
==
word
.
charAt
(
z
))
{
wordfound
=
wordfound
+
word
.
charAt
(
z
);
++
y
;
}
}
}
return
(
wordfound
.
equals
(
word
));
}
}
/* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */
/* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */
private
boolean
searchColumn
(
int
y
,
int
x
,
String
word
)
{
private
boolean
searchColumn
(
int
y
,
int
x
,
String
word
)
{
int
length
=
word
.
length
();
// Ecrire code ici ...
String
wordfound
=
""
;
return
false
;
for
(
int
z
=
0
;
z
<
length
;
++
z
)
{
if
(
x
<
this
.
rows
)
{
if
(
array
[
x
][
y
]
==
word
.
charAt
(
z
))
{
wordfound
=
wordfound
+
word
.
charAt
(
z
);
++
x
;
}
}
}
return
(
wordfound
.
equals
(
word
));
}
}
/*** Methode pour visualiser le tableau. (Déjà fourni) */
/*** Methode pour visualiser le tableau. (Déjà fourni) */
...
@@ -105,5 +139,14 @@ public class Crossword
...
@@ -105,5 +139,14 @@ public class Crossword
CrosswordGUI
.
display
(
array
,
PositionY
,
PositionX
,
EndY
,
EndX
);
CrosswordGUI
.
display
(
array
,
PositionY
,
PositionX
,
EndY
,
EndX
);
}
}
public
void
printArr
()
{
for
(
int
j
=
0
;
j
<
columns
;
j
++)
{
for
(
int
i
=
0
;
i
<
rows
;
i
++)
System
.
out
.
print
(
array
[
i
][
j
]
+
""
);
}
System
.
out
.
println
();
}
}
}
This diff is collapsed.
Click to expand it.
tp2/Main.java
+
21
−
12
View file @
03bf060e
import
java.io.*
;
import
java.io.*
;
import
java.lang.reflect.Array
;
import
java.util.Scanner
;
import
java.util.Scanner
;
/*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier.
/*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier.
...
@@ -10,16 +11,24 @@ public class Main {
...
@@ -10,16 +11,24 @@ public class Main {
public
static
void
main
(
String
args
[])
throws
IOException
{
public
static
void
main
(
String
args
[])
throws
IOException
{
String
filename
;
String
filename
;
Scanner
scan
=
new
Scanner
(
System
.
in
);
// Demander le nom de Fichier à l'utisateur
// Demander le nom de Fichier à l'utisateur
// Constriure une instance de la classe "Crossword"
filename
=
scan
.
next
();
// Crossword tableau = ...
File
file
=
new
File
(
filename
);
// Construire une instance de la classe "Crossword"
//tableau.display(
);
Crossword
tableau
=
new
Crossword
(
file
);
tableau
.
display
();
// Demander un mot à chercher ...
// Demander un mot à chercher ...
while
(
true
)
{
String
word
=
scan
.
next
();
// Utiliser la méthode Crossword.search() pour chercher le mot
// Utiliser la méthode Crossword.search() pour chercher le mot
tableau
.
search
(
word
);
// Ecrire un message sur le terminal pour informer l'utilisateur du resultat.
// Ecrire un message sur le terminal pour informer l'utilisateur du resultat.
if
(
tableau
.
search
(
word
))
{
System
.
out
.
println
(
"Trouvé!"
);
}
else
System
.
out
.
println
(
"Pas trouvé..."
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment