PIVOT DE GAUSS

Programme réalisé en C en 1995.

Le programme Pivot de Gauss permettra de résoudre une équation jusqu'au 10ème degré avec 10 inconnues.


Vous avez ici la version exécutable en DOS (compressée) : Pivot.zip


Vous avez ici la source en C à recopier dans un langage comprenant le C :

#include <stdio.h>
#include <math.h>
#include <conio.h>

short equ, inc;
float m[10][10];
short t1, t2;
short pivot, prem, detail, impossible;

void AFFICHE (void)
{
    short t1 , t2;

    printf ("\n\nMatrice : \n");
    for (t1=1 ; t1<=inc ; t1++)
        printf (" %c", int (m[t1][0]));
    for (t2=1 ; t2<=equ ; t2++)
    {
        printf ("\n");
        for (t1=1 ; t1<=inc ; t1++)
            printf (" %3g", m[t1][t2]);
        printf (" ! %g", m[0][t2]);
    }
}

void TOUCHE (void)
{
    if (detail==1)
    {
        AFFICHE ();
        printf ("\n\nAppuyez sur une touche pour continuer...");
        getch ();
        printf ("\n");
    }
}

void CHERCHE_PIVOT (void)
{
    short tr;
    float pr;

    tr = pivot;
    for (t1=pivot ; t1<=inc ; t1++)
        if (m[t1][pivot]!=0)
        {
            tr=t1;
            t1=inc+1;
        }
        for (t1=pivot ; t1<=inc ; t1++)
            if (m[t1][pivot]==1 || m[t1][pivot]==-1)
            {
                tr=t1;
                t1=inc+1;
            }
            if (tr!=pivot)
            {
                if (detail==1)
                    printf ("\nInterchange les colonnes des inconnues %c et %c...", int (m[pivot][0]), int (m[tr][0]));
                for (t2=0 ; t2<=equ ; t2++)
                {
                    pr = m[tr][t2];
                    m[tr][t2] = m[pivot][t2];
                    m[pivot][t2] = pr;
                }
                TOUCHE ();
            }
}

void PIVOT_A_1 (void)
{
    float garde;

    if (m[pivot][pivot]!=1 && m[pivot][pivot]!=-1)
    {
        if (detail==1)
            printf ("\nPivot a 1 de l'equation numero %hd...", pivot);
        garde = m[pivot][pivot];
        for (t1=pivot ; t1<=inc ; t1++)
            m[t1][pivot] = m[t1][pivot]/garde;
        m[0][pivot] = m[0][pivot]/garde;
        TOUCHE ();
    }
}

void MISE_A_0 (void)
{
    float coef;

    if (detail==1)
        printf ("\nMise a zero de la colonne de l'inconnue %c...", int (m[pivot][0]));
    for (t2=1 ; t2<=equ ; t2++)
    {
        if (t2==pivot)
            t2++;
        coef = m[pivot][t2];
        if (m[pivot][pivot]>0)
            if (coef>0)
                coef = coef-coef*2;
            else
                if (coef<0)
                    coef = fabs (coef);
        for (t1=pivot ; t1<=inc ; t1++)
        {
            m[t1][t2] = m[t1][t2]+coef*m[t1][pivot];
        }
        m[0][t2] = m[0][t2]+coef*m[0][pivot];
    }
    TOUCHE ();
}

void CONTROL (void)
{
    short compte;

    for (t2=1 ; t2<=equ ; t2++)
    {
        compte = 0;
        for (t1=1 ; t1<=inc ; t1++)
            if (m[t1][t2]==0)
                compte++;
        if (compte==inc && m[0][t2]!=0)
            impossible = 1;
    }
}

void main (void)
{
    short min, quelc;

    printf ("\n\n\t\t\tM E T H O D E D E G A U S S");
    printf ("\n\t\t\t===============================");
    inc = 4;
    equ = 4;
    m[1][0] = 97;
    m[2][0] = 98;
    m[3][0] = 99;
    m[4][0] = 100;

    m[1][1] = 1;
    m[2][1] = 1;
    m[3][1] = 1;
    m[4][1] = 1;
    m[0][1] = 0;
    m[1][2] = 1;
    m[2][2] = -1;
    m[3][2] = -1;
    m[4][2] = 1;
    m[0][2] = 2;
    m[1][3] = 2;
    m[2][3] = 1;
    m[3][3] = -1;
    m[4][3] = 1;
    m[0][3] = 1;
    m[1][4] = 4;
    m[2][4] = 1;
    m[3][4] = -1;
    m[4][4] = 3;
    m[0][4] = 3;

    printf ("\n\nNombre d'équation ? (1-10) : ");
    scanf ("%hd", &equ);
    printf ("\nNombre d'inconnues ? (1-10) : ");
    scanf ("%hd", &inc);
    for (t2=1 ; t2<=equ ; t2++)
    {
        printf ("\nEquation numéro %hd\n", t2);
        for (t1=1 ; t1<=inc ; t1++)
        {
            m[t1][0] = t1+96;
            printf ("\tCoefficient de l'inconnue %c : ", int (m[t1][0]));
            scanf ("%g", &m[t1][t2]);
        }
        printf ("\tEgale a : ");
        scanf ("%g", &m[0][t2]);
        printf ("\t\t");
        prem = 1;
        for (t1=1 ; t1<=inc ; t1++)
        {
            if (m[t1][t2] != 0)
            {
                if (m[t1][t2] > 0 && prem == 0)
                    printf ("+ ");
                if (m[t1][t2] < 0)
                    printf ("- ");
                if (m[t1][t2] != 1 && m[t1][t2] != -1)
                    printf ("%g", fabs (m[t1][t2]));
                printf ("%c ", int (m[t1][0]));
                prem = 0;
            }
        }
        printf ("= %g", m[0][t2]);
        printf ("\n\t\tCorrect (o ou n) ? ");
        if (getch ()==110)
        t2--;
        printf ("\n");
    }

    min = inc;
    if (min>equ)
    min = equ;
    AFFICHE ();
    detail = 0;
    printf ("\n\nD‚tail (o ou n) ?");
    if (getch ()==111)
        detail = 1;
    printf ("\n");
    impossible = 0;
    for (pivot=1 ; pivot<=min ; pivot++)
    {
        CHERCHE_PIVOT ();
        CONTROL ();
        if (impossible==0)
            if (m[pivot][pivot]!=0)
            {
                PIVOT_A_1 ();
                CONTROL ();
                if (impossible==0)
                {
                    MISE_A_0 ();
                    CONTROL ();
                }
            }
        if (impossible==1)
            pivot = min+1;
    }
    quelc = 0;
    printf ("\n\nSOLUTION :");
    for (t2=1 ; t2<=inc ; t2++)
    {
        printf ("\n");
        if (m[t2][t2]!=0)
        {
            if (m[t2][t2]<0)
                printf ("-");
            else
                printf (" ");
            printf ("%c = ", int (m[t2][0]));
            if (m[0][t2]!=0)
                printf ("%g ", m[0][t2]) ;
            prem = 1;
            for (t1=t2+1 ; t1<=inc ; t1++)
            {
                if (m[t1][t2] < 0 && prem == 0)
                    printf ("+ ") ;
                if (m[t1][t2] > 0)
                    printf ("- ") ;
                if (m[t1][t2] != 0)
                {
                    if (m[t1][t2]!=1 && m[t1][t2]!=-1)
                        printf ("%g", fabs (m[t1][t2]));
                    printf ("%c ", int (m[t1][0])) ;
                }
                prem = 0;
            }
        }
        else
        {
            printf (" %c = quelconque", int (m[t2][0]));
            quelc++;
        }
    }
    if (impossible==1)
        printf ("\n\tSysteme impossible.");
    else
        if (quelc>0)
            printf ("\n\tSysteme indetermine d'ordre %hd.", quelc);
        else
            printf ("\n\tSolution unique.");
    printf ("\n\n\t\t\t\t\t\tPar\tGilles");
    printf ("\n\t\t\t\t\t\t\tDUT INFORMATIQUE 1995");
    printf ("\n\t\t\t\t\t\t\tU.V. M2");
    getch ();
}

Copyright © 2001 GIPP Info