diff --git a/oop_rand_array_to_file.php b/oop_rand_array_to_file.php new file mode 100755 index 0000000000000000000000000000000000000000..6f2aac54d8949094bf5efeeec116693fea5b8aaf --- /dev/null +++ b/oop_rand_array_to_file.php @@ -0,0 +1,227 @@ +<?php + class RandArrayClass + { + private $array = array(); + private $sorted = false; + private $filepath; + + # A l'instanciation de l'objet, initialise un tableau de valeurs aléatoires + function __construct ($size = 20, $min = 1, $max = 1000, $path = './oop_rand_array_to_file.txt') { + $this->filepath = $path; + for ($i=0; $i < $size; $i++) { + $this->array[] = rand($min, $max); + } + } + + # A la destruction de l'objet, sauvegarde les valeurs du tableau dans un fichier + function __destruct () { + if ($file = fopen($this->filepath, 'a')) { + try { + fputs($file, date("H:i:s") . PHP_EOL); + for ($i=0; $i < count($this->array); $i++) { + fputs($file, $this->array[$i] . chr(10)); + } + } catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; + } finally { + fclose($file); + } + } + } + + # Retourne le tableau + public function getArray() { + return $this->array; + } + + # Définit le tableau + public function setArray($array) { + $this->array = $array; + } + + # Calcule la moyenne des valeurs du tableau + public function calculateAverageValue() : int { + $int = (int)0; + $count = count($this->array); + for ($i=0; $i < $count; $i++) { + $int = $int + $this->array[$i]; + } + $int = $int / count($this->array); + return $int; + } + + # Trouve la valeur minimale du tableau + public function findMinimalValue() : int { + $int = $this->array[0]; + $count = count($this->array); + for ($i=0; $i < $count; $i++) { + if ($this->array[$i] < $int) { + $int = $this->array[$i]; + } + } + return $int; + } + + # Trouve la valeur maximale du tableau + public function findMaximalValue() : int { + $int = $this->array[0]; + $count = count($this->array); + for ($i=0; $i < $count; $i++) { + if ($this->array[$i] > $int) { + $int = $this->array[$i]; + } + } + return $int; + } + + # Trie le tableau en ordre ascendant + public function bubbleSort() { + $temp = (int)0; + $count = count($this->array); + $swapped = (bool)true; + while ($swapped) { + $swapped = false; + $count = $count - 1; + for ($i=0; $i < $count; $i++) { + if ($this->array[$i] > $this->array[$i + 1]) { + $swapped = true; + $temp = $this->array[$i + 1]; + $this->array[$i + 1] = $this->array[$i]; + $this->array[$i] = $temp; + } + } + } + $this->sorted = true; + } + + # Trouve l'index d'une valeur par recherche dichotomique + # Trie le tableau s'il ne l'est pas encore + public function searchValue($value) : int { + if (!$this->sorted) { + $this->bubbleSort(); + } + $min = (int)0; + $max = count($this->array) - 1; + while ($min <= $max) { + $index = floor(($min + $max) / 2); + if ($this->array[$index] == $value) { + return $index; + } elseif ($this->array[$index] < $value) { + $min = $index + 1; + } elseif ($this->array[$index] > $value) { + $max = $index - 1; + } + } + return -1; + } + } + + function displayGenerateForm() : string { + $string = + '<form method="POST" action="oop_rand_array_to_file.php"> + <input type="number" name="size" placeholder="number of entries"/><br> + <input type="number" name="min" placeholder="minimum values"/><br> + <input type="number" name="max" placeholder="maximal values"/><br> + <input type="submit" name="generate" value="Generate array"/> + </form>'; + return $string; + } + + function displaySearchForm($array) : string { + $string = + '<form method="POST" action="oop_rand_array_to_file.php"> + <input type="number" name="input" placeholder="value to find"/><br> + <input type="submit" name="search" value="Search in array"/>'; + foreach($array as $value) { + $string .= '<input type="hidden" name="array[]" value="' . htmlspecialchars($value) . '">'; + } + $string .= '</form>'; + return $string; + } + + function displayArray($array) : string { + $string = "<table><tr><th>Index</th><th>Value</th></tr>"; + $count = count($array); + for ($i=0; $i < $count; $i++) { + $string .= "<tr><td>" . $i . "</td>"; + $string .= "<td>" . $array[$i] . "</td></tr>"; + } + $string .= "</table>"; + return $string; + } +?> + +<html> + <!-- + trier les valeurs par tri à bulle et les écrire dans un fichier + en programmation orientée objet + --> + <head> + <title>M2GPI</title> + <style> + body { + display: flex; + flex-direction: column; + } + #forms { + margin: auto; + display: flex; + align-items: flex-end; + align-content: space-between; + } + #results { + margin: auto; + } + #tables { + margin: auto; + display: flex; + align-content: space-between; + } + #forms > form { + margin: 0 1em 0 1em; + } + #tables > div { + margin: 0 5em 0 5em;; + } + </style> + </head> + <body> + <?php + if (isset($_POST['search']) && $_POST['input'] && $_POST['array']) { + $array = new RandArrayClass(); + $array->setArray($_POST['array']); + $input = $_POST['input']; + } elseif (isset($_POST['generate']) && $_POST['size'] && $_POST['min'] && $_POST['max']) { + $array = new RandArrayClass($_POST['size'], $_POST['min'], $_POST['max']); + } else { + $array = new RandArrayClass(); + } + + echo '<h1>M2GPI2024 Algorithmie</h1>'; + echo '<div id="forms">'; + echo displayGenerateForm(); + echo displaySearchForm($array->getArray()); + echo '</div><div id="tables">'; + echo '<div><h3>Unordered</h3>'; + echo displayArray($array->getArray()); + echo '</div><div><h3>Ordered</h3>'; + $array->bubbleSort(); + echo displayArray($array->getArray()); + echo '</div><div id="results">'; + echo '<h3>Results</h3>'; + echo "<p>Average value is " . $array->calculateAverageValue() . "</p>"; + echo "<p>Smallest value is " . $array->findMinimalValue() . "</p>"; + echo "<p>Largest value is " . $array->findMaximalValue() . "</p>"; + if (isset($input)) { + $result = $array->searchValue($input); + if ($result > -1) { + echo "Value " . $input . " exists at index " . $result; + } else { + echo "Value " . $input . " doesn't exist"; + } + } + echo '</div></div>'; + unset($array); + ?> + </body> +</html> \ No newline at end of file diff --git a/oop_rand_array_to_file.txt b/oop_rand_array_to_file.txt new file mode 100644 index 0000000000000000000000000000000000000000..29af738d09d8a1e87ffd5e92b9090436ae7b9bb4 --- /dev/null +++ b/oop_rand_array_to_file.txt @@ -0,0 +1,116 @@ +12:38:08 +6 +7 +17 +294 +336 +364 +384 +385 +534 +554 +570 +641 +646 +685 +694 +878 +890 +931 +993 +996 +12:39:02 +12 +36 +47 +68 +136 +190 +282 +303 +379 +496 +577 +633 +654 +674 +750 +757 +765 +824 +891 +981 +12:39:13 +173 +188 +194 +199 +200 +207 +214 +220 +227 +246 +12:41:18 +265 +275 +351 +391 +442 +467 +523 +527 +541 +632 +658 +676 +746 +748 +832 +874 +917 +969 +973 +973 +12:41:22 +30 +86 +90 +170 +184 +201 +248 +260 +317 +380 +558 +572 +625 +625 +658 +739 +749 +855 +862 +980 +12:41:25 +30 +86 +90 +170 +184 +201 +248 +260 +317 +380 +558 +572 +625 +625 +658 +739 +749 +855 +862 +980 diff --git a/rand_array_to_file.php b/rand_array_to_file.php new file mode 100755 index 0000000000000000000000000000000000000000..70f9ab87e4b294881e011f00b88422545a0cfe75 --- /dev/null +++ b/rand_array_to_file.php @@ -0,0 +1,203 @@ +<html> + <!-- + trier les valeurs par tri à bulle et les écrire dans un fichier + --> + <head> + <title>M2GPI</title> + <style> + body { + display: flex; + flex-direction: column; + } + #forms { + margin: auto; + display: flex; + align-items: flex-end; + align-content: space-between; + } + #results { + margin: auto; + } + #tables { + margin: auto; + display: flex; + align-content: space-between; + } + #forms > form { + margin: 0 1em 0 1em; + } + #tables > div { + margin: 0 5em 0 5em;; + } + </style> + </head> + <body> + <?php + function displayGenerateForm() : string { + $string = + '<form method="POST" action="rand_array.php"> + <input type="number" name="size" placeholder="number of entries"/><br> + <input type="number" name="min" placeholder="minimum values"/><br> + <input type="number" name="max" placeholder="maximal values"/><br> + <input type="submit" name="generate" value="Generate array"/> + </form>'; + return $string; + } + + function displaySearchForm($array) : string { + $string = + '<form method="POST" action="rand_array.php"> + <input type="number" name="input" placeholder="value to find"/><br> + <input type="submit" name="search" value="Search in array"/>'; + foreach($array as $value) { + $string .= '<input type="hidden" name="array[]" value="' . $value . '">'; + } + $string .= '</form>'; + return $string; + } + + function displayArray($array) : string { + $string = "<table><tr><th>Index</th><th>Value</th></tr>"; + $count = count($array); + for ($i=0; $i < $count; $i++) { + $string .= "<tr><td>" . $i . "</td>"; + $string .= "<td>" . $array[$i] . "</td></tr>"; + } + $string .= "</table>"; + return $string; + } + + function generateRandArray($size = 20, $min = 1, $max = 1000) : array { + $array = array(); + for ($i=0; $i < $size; $i++) { + $array[] = rand($min, $max); + } + return $array; + } + + function calculateAverage($array) : int { + $int = (int)0; + $count = count($array); + for ($i=0; $i < $count; $i++) { + $int = $int + $array[$i]; + } + $int = $int / count($array); + return $int; + } + + function calculateMinimum($array) : int { + $int = $array[0]; + $count = count($array); + for ($i=0; $i < $count; $i++) { + if ($array[$i] < $int) { + $int = $array[$i]; + } + } + return $int; + } + + function calculateMaximum($array) : int { + $int = $array[0]; + $count = count($array); + for ($i=0; $i < $count; $i++) { + if ($array[$i] > $int) { + $int = $array[$i]; + } + } + return $int; + } + + function bubbleSort($array) : array { + $temp = (int)0; + $count = count($array); + $swapped = (bool)true; + while ($swapped) { + $swapped = false; + $count = $count - 1; + for ($i=0; $i < $count; $i++) { + if ($array[$i] > $array[$i + 1]) { + $swapped = true; + $temp = $array[$i + 1]; + $array[$i + 1] = $array[$i]; + $array[$i] = $temp; + } + } + } + return $array; + } + + function searchValue($value, $array) : int { + $min = (int)0; + $max = count($array) - 1; + while ($min <= $max) { + $index = floor(($min + $max) / 2); + if ($array[$index] == $value) { + return $index; + } elseif ($array[$index] < $value) { + $min = $index + 1; + } elseif ($array[$index] > $value) { + $max = $index - 1; + } + } + return -1; + } + + function writeArrayValuesToFile($array, $path) : bool { + if ($file = fopen($path, 'a')) { + try { + fputs($file, chr(10) . date("H:i:s")); + for ($i=0; $i < count($array); $i++) { + fputs($file, $array[$i] . chr(10)); + } + } catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; + } finally { + fclose($file); + } + return true; + } + return false; + } + + if (isset($_POST['search']) && $_POST['input'] && $_POST['array']) { + $input = $_POST['input']; + $array = $_POST['array']; + } elseif (isset($_POST['generate']) && $_POST['size'] && $_POST['min'] && $_POST['max']) { + $array = generateRandArray($_POST['size'], $_POST['min'], $_POST['max']); + } else { + $array = generateRandArray(); + } + $sortedArray = bubbleSort($array); + $path = './rand_array_to_file.txt'; + + echo '<h1>M2GPI2024 Algorithmie</h1>'; + echo '<div id="forms">'; + echo displayGenerateForm(); + echo displaySearchForm($array); + echo '</div><div id="results">'; + echo '<h3>Results</h3>'; + echo "<p>Average value is " . calculateAverage($array) . "</p>"; + echo "<p>Smallest value is " . calculateMinimum($array) . "</p>"; + echo "<p>Largest value is " . calculateMaximum($array) . "</p>"; + if (writeArrayValuesToFile($sortedArray, $path)) { + echo "<p>Values saved to <br>'" . $path . "'</p>"; + } else { + echo "<p>Couldn't save values to <br>'" . $path . "'</p>"; + } + if (isset($input)) { + $result = searchValue($input, $sortedArray); + if ($result > -1) { + echo "Value " . $input . " exists at index " . $result; + } else { + echo "Value " . $input . " doesn't exist"; + } + } + echo '</div><div id="tables">'; + echo '<div><h3>Unordered</h3>'; + echo displayArray($array); + echo '</div><div><h3>Ordered</h3>'; + echo displayArray($sortedArray); + echo '</div></div>'; + ?> + </body> +</html> \ No newline at end of file diff --git a/rand_array_to_file.txt b/rand_array_to_file.txt new file mode 100644 index 0000000000000000000000000000000000000000..cb65b11022f0ecbfd5655ef7e59f8ace4f7b056d --- /dev/null +++ b/rand_array_to_file.txt @@ -0,0 +1,103 @@ +83\n130\n226\n272\n320\n372\n477\n485\n592\n618\n755\n782\n785\n800\n814\n834\n896\n917\n978\n996\n19(LR)60(LR)90(LR)118(LR)182(LR)207(LR)279(LR)312(LR)323(LR)383(LR)399(LR)416(LR)471(LR)490(LR)524(LR)537(LR)676(LR)687(LR)784(LR)788(LR)67 +75 +75 +89 +165 +185 +298 +325 +351 +353 +389 +403 +453 +468 +584 +638 +642 +808 +876 +926 +34 +79 +166 +167 +174 +236 +244 +261 +302 +328 +342 +405 +506 +555 +606 +618 +773 +784 +862 +972 + +10:44:52100 +138 +149 +323 +345 +359 +361 +416 +424 +556 +576 +624 +626 +801 +964 +965 +969 +987 +996 +996 + +10:47:339 +12 +26 +135 +170 +258 +300 +346 +376 +435 +462 +520 +538 +582 +707 +795 +831 +871 +896 +901 + +14:16:0440 +105 +112 +220 +329 +401 +452 +462 +517 +563 +580 +604 +632 +654 +878 +933 +934 +951 +959 +993