From bc48a28596ee8602a6872379df217cc83a306470 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Feb 2018 22:53:36 +0100 Subject: [PATCH] Add Entiers code exercice. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .gitignore | 6 ++- Entiers/Entiers.java | 126 +++++++++++++++++++++++++++++++++++++++++++ Entiers/Makefile | 92 +++++++++++++++++++++++++++++++ Piles/Pile.java | 76 ++++++++++++++++++++------ 4 files changed, 284 insertions(+), 16 deletions(-) create mode 100644 Entiers/Entiers.java create mode 100644 Entiers/Makefile diff --git a/.gitignore b/.gitignore index b4b4310..eb1fb4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Compiled class file *.class -# Package Files # +# Package files *.jar *.war *.ear @@ -11,3 +11,7 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# editor trash +*.swp +*~ diff --git a/Entiers/Entiers.java b/Entiers/Entiers.java new file mode 100644 index 0000000..752546b --- /dev/null +++ b/Entiers/Entiers.java @@ -0,0 +1,126 @@ + +class Entiers { + private int int_array[]; + private int array_size; + private int current_size; + + private void setSize(int size) { + array_size = size; + } + + private int getSize() { + return array_size; + } + + private void setCurrentSize(int index) { + current_size = index; + } + + private int getCurrentSize() { + return current_size; + } + + Entiers(int size) { + int_array = new int[size]; + setSize(size); + setCurrentSize(0); + } + + public boolean inserer(int value) { + if (is_full()) { + System.out.println("Tableau plein"); + return false; + } + //FIXME: Avoid duplicated values + int i; + for (i = getCurrentSize() - 1; (i >= 0 && int_array[i] > value); i--) { + int_array[i + 1] = int_array[i]; + } + int_array[i + 1] = value; + current_size++; + return true; + } + + private int binarySearch(int first, int last, int value) { + if (last < first) + return -1; + int middle = (first + last) / 2; + if (value == int_array[middle]) + return middle; + if (value > int_array[middle]) + return binarySearch((middle + 1), last, value); + return binarySearch(first, (middle -1), value); + } + + public boolean supprimer(int value) { + if (is_empty()) { + System.out.println("Aucune valeur à supprimer"); + return false; + } + + // Find position of element to be deleted + int pos = binarySearch(0, getCurrentSize(), value); + + if (pos == -1) + { + System.out.println("Valeur à supprimer inexistante"); + return false; + } + + // Deleting element + for (int i = pos; i < getCurrentSize() - 1; i++) { + int_array[i] = int_array[i + 1]; + } + + current_size--; + return true; + } + + private boolean is_full() { + return (getCurrentSize() >= getSize()); + } + + private boolean is_empty() { + return (getCurrentSize() == 0); + } + + public void afficher() { + for (int i = 0; i < getSize(); i++) { + System.out.println("element " + i + " " + int_array[i]); + } + } + + public static void main(String[] args) { + Entiers integer = new Entiers(5); + + integer.inserer(1); + + integer.afficher(); + + integer.inserer(12); + + integer.afficher(); + + integer.inserer(3); + + integer.afficher(); + + integer.inserer(0); + + integer.inserer(1); + + integer.afficher(); + + integer.supprimer(12); + + integer.afficher(); + System.out.println("Current size " + integer.getCurrentSize()); + + integer.supprimer(1); + + integer.afficher(); + System.out.println("Current size " + integer.getCurrentSize()); + + + } +} diff --git a/Entiers/Makefile b/Entiers/Makefile new file mode 100644 index 0000000..532f851 --- /dev/null +++ b/Entiers/Makefile @@ -0,0 +1,92 @@ +# define compiler and compiler flag variables +# define a variable for compiler flags (JFLAGS) +# define a variable for the compiler (JC) +# define a variable for the Java Virtual Machine (JVM) +# define a variable for a parameter. When you run make, you could use: +# make run FILE="Algo.csv" para sobre escribir el valor de FILE. + +JFLAGS = -g +JC = javac +JVM = java +FILE = + +# +# Clear any default targets for building .class files from .java files; we +# will provide our own target entry to do this in this makefile. +# make has a set of default targets for different suffixes (like .c.o) +# Currently, clearing the default for .java.class is not necessary since +# make does not have a definition for this target, but later versions of +# make may, so it doesn't hurt to make sure that we clear any default +# definitions for these +# + +.SUFFIXES: .java .class + + +# +# Here is our target entry for creating .class files from .java files +# This is a target entry that uses the suffix rule syntax: +# DSTS: +# rule +# DSTS (Dependency Suffix Target Suffix) +# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency +# file, and 'rule' is the rule for building a target +# '$*' is a built-in macro that gets the basename of the current target +# Remember that there must be a < tab > before the command line ('rule') +# + +.java.class: + $(JC) $(JFLAGS) $*.java + + +# +# CLASSES is a macro consisting of N words (one for each java source file) +# When a single line is too long, use \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + Entiers.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Entiers + +# +# the default make target entry +# for this example it is the target classes + +default: classes + + +# Next line is a target dependency line +# This target entry uses Suffix Replacement within a macro: +# $(macroname:string1=string2) +# In the words in the macro named 'macroname' replace 'string1' with 'string2' +# Below we are replacing the suffix .java of all words in the macro CLASSES +# with the .class suffix +# + +classes: $(CLASSES:.java=.class) + + +# Next two lines contain a target for running the program +# Remember the tab in the second line. +# $(JMV) y $(MAIN) are replaced by their values + +run: $(MAIN).class + $(JVM) $(MAIN) + +# this line is to remove all unneeded files from +# the directory when we are finished executing(saves space) +# and "cleans up" the directory of unneeded .class files +# RM is a predefined macro in make (RM = rm -f) +# + +clean: + $(RM) *.class diff --git a/Piles/Pile.java b/Piles/Pile.java index b82dd7e..4e240d3 100644 --- a/Piles/Pile.java +++ b/Piles/Pile.java @@ -1,31 +1,58 @@ +/** + * + */ class Pile { private int int_array[]; private int array_size; private int stack_head_index; + /** + * [setSize description] + * @param int size [description] + */ private void setSize(int size) { array_size = size; } + /** + * [getSize description] + * @return [description] + */ private int getSize() { return array_size; } + /** + * [setHeadIndex description] + * @param int index [description] + */ private void setHeadIndex(int index) { stack_head_index = index; } + /** + * [getHeadIndex description] + * @return [description] + */ private int getHeadIndex() { return stack_head_index; } + /** + * [Pile description] + * @param int size [description] + */ Pile(int size) { int_array = new int[size]; setSize(size); setHeadIndex(0); } + /** + * [empiler description] + * @param int value [description] + */ public void empiler(int value) { if (!plein()) { int_array[stack_head_index] = value; @@ -35,6 +62,10 @@ class Pile { } } + /** + * [depiler description] + * @return [description] + */ public int depiler() { if (!vide()) { stack_head_index--; @@ -44,20 +75,35 @@ class Pile { } } - public boolean plein() { + /** + * [plein description] + * @return [description] + */ + private boolean plein() { return (getHeadIndex() >= getSize()); } - public boolean vide() { + /** + * [vide description] + * @return [description] + */ + private boolean vide() { return (getHeadIndex() == 0); } + /** + * [afficher description] + */ public void afficher() { for (int i = 0; i < getSize(); i++) { System.out.println("element " + i + " " + int_array[i]); } } + /** + * The main() function + * @param String[] args main() function arguments + */ public static void main(String[] args) { Pile stack = new Pile(5); @@ -69,19 +115,19 @@ class Pile { stack.afficher(); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); - System.out.println("stack head value " + stack.depiler()); - System.out.println("stack index " + stack.getHeadIndex()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); stack.afficher(); } -- 2.34.1