- #include<iostream.h>
- #include<fstream>
- #include<stdio.h>
-
- int main()
- {
- char name[256], name_file[256];
- bool avec_fichier, est_nulle;
- int col,lig;
- double temp;
- double matrix[256][256];
- FILE *fichier;
-
- cout << "==================================";
- cout << endl << "= Gauss =";
- cout << endl << "= auteur: Bouba =";
- cout << endl << "==================================" << endl << endl;
- cout << "Entrez le nom de la matrice a créer:";
- gets(name);
- cout << "Voulez vous enregistrer la matrice \"" << name << "\" dans un fichier? (O/N)";
-
- if(getchar()=='o')
- {
- strcpy(name_file,name);
- strcat(name_file,".mtx");
- if((fichier=fopen(name_file,"a+"))!=NULL)
- {
- cout <<"Fichier \"" << name_file <<"\" créé avec succès!";
- avec_fichier=true;
- }
- else
- {
- cout <<"Impossible de créer le fichier \"" << name_file << "\"!";
- }
- }
-
-
- cout << endl << "Entrez les dimensions de la matrice \"" << name << "\"";
- cout << endl << "lignes : ";
- cin >> lig;
- cout << "colonnes : ";
- cin >> col;
- cout << "La matrice \"" << name << "\" est de dimension " << lig << "x" << col;
- cout << endl << "Entrez la valeur de chaque élément de la matrice \"" << name << "\"" << endl;
-
- for(int i=0; i<lig; i++)
- for(int j=0; j<col; j++)
- {
- cout << name << "[" << i+1 << "][" << j+1 << "] : ";
- cin >> matrix[i][j];
- }
-
- cout << "La matrice \"" << name << "\" est la suivante" << endl << endl;
-
- if(avec_fichier==true)
- fprintf(fichier,"Matrice initiale\n");
-
- for(int i=0; i<lig; i++)
- {
- cout << "|\t";
- for(int j=0; j<col; j++)
- {
- cout << matrix[i][j] << "\t";
- if(avec_fichier==true)
- {
- fprintf(fichier,"%f\t",matrix[i][j]);
- }
- }
- if(avec_fichier==true)
- fprintf(fichier,"\n");
- cout << "|" << endl;
- }
-
- cout << endl << "Appuyez sur [entrée] pour continuer";
- getchar();
- while(getchar()!='\n');
- cout << "L'algorithme de Gauss donne la matrice échelonnée suivante" << endl;
-
- int l=0, c=0;
- double tmp[256];
-
- //c'est ici que commence l'algorithme de gauss
- while((l<lig)&&(c<col))
- {
- for(int i=0;i<lig;i++)
- {
- for(int j=0;j<col;j++)
- {
- if(matrix[i][j]==0)
- est_nulle=true;
- else
- est_nulle=false;
- }
- }
-
- if(est_nulle==true)
- {
- cout << endl << "La matrice \"" << name << "\" est nulle" << endl;
- for(int i=0; i<lig; i++)
- {
- cout << "|\t";
- for(int j=0; j<col; j++)
- {
- cout << matrix[i][j] << "\t";
- }
- cout << "|" << endl;
- }
- return 0;
- }
- else
- {
- temp=0;
- while(temp==0)
- {
- temp=0;
- for(int x=l; x<lig; x++)
- {
- temp=matrix[x][c]+temp;
- }
- if(temp==0)
- c++;
- }
- if(matrix[l][c]==0)
- {
- int i=l+1;
- while(matrix[i][c]==0)
- {
- i=i+1;
- }
- for(int x=0;x<col;x++)
- {
- tmp[x]=matrix[l][x];
- matrix[l][x]=matrix[i][x];
- matrix[i][x]=tmp[x];
- }
- }
- for(int i=l+1;i<lig;i++)
- {
- temp=matrix[i][c]/matrix[l][c];
- for(int x=c;x<col;x++)
- {
- matrix[i][x] = matrix[i][x] - temp*matrix[l][x];
- if((matrix[i][x] < 0.00000000000001) && (matrix[i][x]>-0.00000000000001))
- matrix[i][x]=0;
- }
- }
- l++;
- c++;
- }
- }
-
- if(avec_fichier==true)
- fprintf(fichier,"\nVoici le résulatat du pivot de gauss sur cette matrice\n");
-
- for(int i=0; i<lig; i++)
- {
- cout << "|\t";
- for(int j=0; j<col; j++)
- {
- cout << matrix[i][j] << "\t";
- if(avec_fichier==true)
- {
- fprintf(fichier,"%f\t",matrix[i][j]);
- }
- }
- if(avec_fichier==true)
- fprintf(fichier,"\n");
- cout << "|" << endl;
- }
- return 0;
- }
-
-
-
#include<iostream.h>
#include<fstream>
#include<stdio.h>
int main()
{
char name[256], name_file[256];
bool avec_fichier, est_nulle;
int col,lig;
double temp;
double matrix[256][256];
FILE *fichier;
cout << "==================================";
cout << endl << "= Gauss =";
cout << endl << "= auteur: Bouba =";
cout << endl << "==================================" << endl << endl;
cout << "Entrez le nom de la matrice a créer:";
gets(name);
cout << "Voulez vous enregistrer la matrice \"" << name << "\" dans un fichier? (O/N)";
if(getchar()=='o')
{
strcpy(name_file,name);
strcat(name_file,".mtx");
if((fichier=fopen(name_file,"a+"))!=NULL)
{
cout <<"Fichier \"" << name_file <<"\" créé avec succès!";
avec_fichier=true;
}
else
{
cout <<"Impossible de créer le fichier \"" << name_file << "\"!";
}
}
cout << endl << "Entrez les dimensions de la matrice \"" << name << "\"";
cout << endl << "lignes : ";
cin >> lig;
cout << "colonnes : ";
cin >> col;
cout << "La matrice \"" << name << "\" est de dimension " << lig << "x" << col;
cout << endl << "Entrez la valeur de chaque élément de la matrice \"" << name << "\"" << endl;
for(int i=0; i<lig; i++)
for(int j=0; j<col; j++)
{
cout << name << "[" << i+1 << "][" << j+1 << "] : ";
cin >> matrix[i][j];
}
cout << "La matrice \"" << name << "\" est la suivante" << endl << endl;
if(avec_fichier==true)
fprintf(fichier,"Matrice initiale\n");
for(int i=0; i<lig; i++)
{
cout << "|\t";
for(int j=0; j<col; j++)
{
cout << matrix[i][j] << "\t";
if(avec_fichier==true)
{
fprintf(fichier,"%f\t",matrix[i][j]);
}
}
if(avec_fichier==true)
fprintf(fichier,"\n");
cout << "|" << endl;
}
cout << endl << "Appuyez sur [entrée] pour continuer";
getchar();
while(getchar()!='\n');
cout << "L'algorithme de Gauss donne la matrice échelonnée suivante" << endl;
int l=0, c=0;
double tmp[256];
//c'est ici que commence l'algorithme de gauss
while((l<lig)&&(c<col))
{
for(int i=0;i<lig;i++)
{
for(int j=0;j<col;j++)
{
if(matrix[i][j]==0)
est_nulle=true;
else
est_nulle=false;
}
}
if(est_nulle==true)
{
cout << endl << "La matrice \"" << name << "\" est nulle" << endl;
for(int i=0; i<lig; i++)
{
cout << "|\t";
for(int j=0; j<col; j++)
{
cout << matrix[i][j] << "\t";
}
cout << "|" << endl;
}
return 0;
}
else
{
temp=0;
while(temp==0)
{
temp=0;
for(int x=l; x<lig; x++)
{
temp=matrix[x][c]+temp;
}
if(temp==0)
c++;
}
if(matrix[l][c]==0)
{
int i=l+1;
while(matrix[i][c]==0)
{
i=i+1;
}
for(int x=0;x<col;x++)
{
tmp[x]=matrix[l][x];
matrix[l][x]=matrix[i][x];
matrix[i][x]=tmp[x];
}
}
for(int i=l+1;i<lig;i++)
{
temp=matrix[i][c]/matrix[l][c];
for(int x=c;x<col;x++)
{
matrix[i][x] = matrix[i][x] - temp*matrix[l][x];
if((matrix[i][x] < 0.00000000000001) && (matrix[i][x]>-0.00000000000001))
matrix[i][x]=0;
}
}
l++;
c++;
}
}
if(avec_fichier==true)
fprintf(fichier,"\nVoici le résulatat du pivot de gauss sur cette matrice\n");
for(int i=0; i<lig; i++)
{
cout << "|\t";
for(int j=0; j<col; j++)
{
cout << matrix[i][j] << "\t";
if(avec_fichier==true)
{
fprintf(fichier,"%f\t",matrix[i][j]);
}
}
if(avec_fichier==true)
fprintf(fichier,"\n");
cout << "|" << endl;
}
return 0;
}