Saisie de la dimension des matrices carrées
Exercice réalisé ci-dessus :
On désire créer un formulaire html qui permet à l’utilisateur de saisir la dimension d’une matrice carrée (nb lignes = nb colonnes). Nommer ce fichier tpmatrice.html...etc.
Voici le code tpmatrice.html :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Formulaire Matrice</title>
<link rel="stylesheet" type="text/css" href="style2.css">
</head>
<body>
<h1>Saisie de la dimension des matrices carrées</h1>
<form action="saisiemat.php" method="post">
<label for="dimension">Dimension (nb lignes = nb colonnes): </label>
<input type="text" id="dimension" name="dimension" required>
<input type="submit" value="OK">
</form>
Voici le code calculmat.php :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calcul des Matrices</title>
</head>
<body>
<h1>Résultats des matrices</h1>
<?php
$dimension = isset($_POST['dimension']) ? intval($_POST['dimension']) : 0;
if ($dimension > 0) {
$matriceA = isset($_POST['matriceA']) ? $_POST['matriceA'] : [];
$matriceB = isset($_POST['matriceB']) ? $_POST['matriceB'] : [];
echo "<h2>Matrice A</h2>";
echo "<table>";
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
echo "<td>" . $matriceA[$i][$j] . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<h2>Matrice B</h2>";
echo "<table>";
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
echo "<td>" . $matriceB[$i][$j] . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<h2>Résultats</h2>";
echo "<h3>Somme des deux matrices</h3>";
echo "<table>";
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
echo "<td>" . ($matriceA[$i][$j] + $matriceB[$i][$j]) . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<h3>Produit des deux matrices</h3>";
echo "<table>";
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
$produit = 0;
for ($k = 0; $k < $dimension; $k++) {
$produit += $matriceA[$i][$k] * $matriceB[$k][$j];
}
echo "<td>" . $produit . "</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "Veuillez saisir une dimension valide.";
}
?>
</body>
</html>
Voici le code coefficients.php :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Saisie des Coefficients</title>
</head>
<body>
<h1>Saisie des coefficients des matrices</h1>
<?php
$dimension = isset($_GET['dimension']) ? intval($_GET['dimension']) : 0;
if ($dimension > 0) {
?>
<form action="calculmat.php" method="post">
<h2>Matrice A</h2>
<table>
<?php
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
echo "<td>";
echo "<label for='matriceA_$i$j'>Matrice A[$i][$j]: </label>";
echo "<input type='text' id='matriceA_$i$j' name='matriceA[$i][$j]' required>";
echo "<br>";
echo "</td>";
}
echo "</tr>";
}
?>
</table>
<hr>
<h2>Matrice B</h2>
<table>
<?php
for ($i = 0; $i < $dimension; $i++) {
echo "<tr>";
for ($j = 0; $j < $dimension; $j++) {
echo "<td>";
echo "<label for='matriceB_$i$j'>Matrice B[$i][$j]: </label>";
echo "<input type='text' id='matriceB_$i$j' name='matriceB[$i][$j]' required>";
echo "<br>";
echo "</td>";
}
echo "</tr>";
}
?>
</table>
<input type="hidden" name="dimension" value="<?php echo $dimension; ?>">
<input type="submit" value="OK">
</form>
<?php
} else {
echo "Veuillez saisir une dimension valide.";
}
?>
</body>
</html>
Voici le code saisiemat.php :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$dimension = isset($_POST['dimension']) ? intval($_POST['dimension']) : 0;
if ($dimension > 0) {
header("Location: coefficients.php?dimension=$dimension");
exit();
} else {
echo "Veuillez saisir une dimension valide.";
}
}
?>