<?php
class matrice{
private $ligne;
private $colonne;
private $nom;
private $M=array();
public function __construct()
{
for($i=0;$i<$this->ligne;$i++)
{
for($j=0;$j<$this->colonne;$j++)
{
$this->M[$i][$j]=0;
}
}
}
public function setname_dims($l,$c,$n){
$this->ligne=$l;
$this->colonne=$c;
$this->nom=$n;
}
public function saisie()
{ echo"<form method='POST' action='Classe_Matrice.php'>";
echo"saisir la matrice $this->nom :::\n";
echo"<table >";
for($i=0;$i<$this->ligne;$i++)
{
echo "<tr>";
for($j=0;$j<$this->colonne;$j++)
{
echo "<td>";
echo"<input type='text' name=".$this->nom.$i.$j."> </td>";
}
echo"</tr>";}
for($i=0;$i<$this->ligne;$i++)
{
for($j=0;$j<$this->colonne;$j++)
{
$this->M[$i][$j]=$_POST["$this->nom$i$j"];
}
}
echo "</table>";}
public function submit()
{
echo "<td><input type='submit' value='submit'></td>";
echo "<br>";
}
public function write($fic){
$fichier=fopen($fic,"w+");
if(!$fichier){
echo" fichier introuvable";
exit;
}
for($i=0;$i<$this->ligne;$i++)
{
for($j=0;$j<$this->colonne;$j++)
{
fputs($fichier,$this->M[$i][$j]);
fputs($fichier," ");}
fputs($fichier,"\n");}
fclose($fichier);}
public function read($fic)
{$j=0;
$fichier=fopen($fic,"r");
if(!$fichier){echo "impossible";
exit;}
while(!feof($fichier)){
$line=fgets($fichier);
$array=explode(" ",$line);
for($i=0;$i<count($array);$i++){
$this->M[$j][$i]=$array[$i];}
$j++;
}}
public function somme(matrice $B){
echo" La somme de $this->nom et $B->nom :::";
$res=$this;
if((($res->ligne)==($B->ligne)) && (($res->colonne)==($B->colonne))){
for( $i=0;$i< $B->ligne ; $i++){
for( $j=0;$j< $B->colonne ; $j++){
$res->M[$i][$j] =$this->M[$i][$j] + $B->M[$i][$j];}
}
return $res;
}
else
{
echo "impossible de sommer les deux matrices";
return NULL;
}
}
public function produit(matrice $B){
echo" Le produit de $this->nom et $B->nom:::\n";
if($this->colonne!==$B->ligne){
echo "impossible de faire la multiplication ";
return NULL;
}
$m=new matrice();
$m->setname_dims($this->ligne,$B->colonne,'p');
$res=$m;
for( $i=0;$i< $this->ligne ; $i++){
for( $j=0;$j< $B->colonne ; $j++){
for( $k=0;$k< $this->colonne ; $k++){
$res->M[$i][$j]+=$this->M[$i][$k]*$B->M[$k][$j];}
}}
return $res;
}
public function afficher()
{
echo"<table width='200' border='1'>";
for($i=0;$i<$this->ligne;$i++)
{
echo "<tr>";
for($j=0;$j<$this->colonne;$j++)
{
echo "<td>".$this->M[$i][$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
$M= new matrice();
$M->setname_dims(2,2,'M');
$N= new matrice();
$N->setname_dims(2,2,'N');
$c= new matrice();
$M->saisie();
$M->write("a.txt");
$N->saisie();
$N->write("b.txt");
$M->submit();
$M->read("a.txt");
echo"M:::";
$M->afficher();
$N->read("b.txt");
echo"N:::";
$N->afficher();
$M->produit($N)->afficher();
$N->somme($M)->afficher();
?>
A remplacer à mon avis par :