Génération de tableau d'entiers et lettres alea : Triage ASC / DESC
Bonjour,
A moins que ce ne soit pas utile, mais j'ose espérer le contraire :)
Un message récent sur le forum me fait penser que ça pourrait justement être simpa de présenter un petit programme dont le but est :
- La génération de tableau d'int + caractère de l'alphabet via rand()
- Le triage de celui-ci à la fois en ASC(croissant) puis DESC(décroissant) -- "Sélection" & "A bulles"
- L'affichage du tableau
Bon je précise que ce programme date un peu, il n'est pas impossible qu'il puisse être amélioré donc je suis preneur pour l'optimiser.
main.c
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <string.h>
#include <stdio.h>
#include "tirage.h"
#include "tri.h"
#include "affichage.h"
/**********************************************/
/*** PROGRAMME PRINCIPAL ***/
/*** CESIMILLIONS ***/
/**********************************************/
int main()
{
/** Initialisation de "rand()" **/
initialiser();
/** Déclaration des tableaux -- TIRAGE INITIALE **/
int numeros[NOMBRES] = {0};
int lettres[LETTRES] = {26};
/** Déclaration des tableaux -- TRIAGE CROISSANT/ASC **/
int numeros_tries[NOMBRES];
int lettres_triees[LETTRES];
/** Déclaration des tableaux -- TRIAGE DECROISSANT/DESC **/
int numeros_tries_inverse[NOMBRES];
int lettres_triees_inverse[LETTRES];
/** Variable du menu **/
int choix;
/** Étiquette de tirage **/
tirage:
tirage(numeros, lettres);
/** Copie des tirages vers les tableaux de triage **/
memcpy(numeros_tries, numeros, sizeof(int) * NOMBRES);
memcpy(lettres_triees, lettres, sizeof(int) * LETTRES);
/** Appels des fonctions de triage **/
tri_selection(numeros_tries, NOMBRES);
tri_selection(lettres_triees, LETTRES);
tri_bulles(numeros_tries, NOMBRES);
tri_bulles(lettres_triees, LETTRES);
/** Copie des tableaux de triages vers les tableaux d'inversion **/
memcpy(numeros_tries_inverse, numeros_tries, sizeof(int) * NOMBRES);
memcpy(lettres_triees_inverse, lettres_triees, sizeof(int) * LETTRES);
inverser(numeros_tries_inverse, NOMBRES);
inverser(lettres_triees_inverse, LETTRES);
printf("\n\t \4 TIRAGE : "); affichage(numeros, lettres);
printf("\n -\4 -||CESIMILLIONS||- \4-\n 1 - Afficher le tirage brut\n 2 - Afficher le tirage par ordre croissant\n 3 - Afficher le tirage par ordre decroissant\n 4 - Nouveau tirage\n 0 - Quitter \1\n\n");
while(1)
{
printf("\n \4 ");
scanf("%d", &choix);
switch (choix)
{
case 1 :
affichage(numeros, lettres);
break;
case 2 :
affichage(numeros_tries, lettres_triees);
break;
case 3 :
affichage(numeros_tries_inverse, lettres_triees_inverse);
break;
case 4 :
goto tirage;/** Retour à l'étiquette de tirage **/
break;
case 0 :
printf("\n\t \1\2 Sayonara !! \1\2\n\n");
return EXIT_SUCCESS;
}
}
} |
Tirage.c
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "tirage.h"
void initialiser()
{
srand(time(NULL));
}
int alea(int min, int max)
{
return ((rand() % (max - min)) + min);
}
void tirage(int nums[], int lettres[])
{
int i;
int tempNum = -1;
//On réinitialise les tableaux.
for (i=0 ; i < NOMBRES ; i++) nums[i] = -1;
for (i=0 ; i < LETTRES ; i++) lettres[i] = 26;
for (i=0 ; i < NOMBRES ; i++)//On tire les nombres.
{
do
{
tempNum = alea(0, 99);
}
while (contient(nums, NOMBRES, tempNum));//On vérifie qu'il n'a pas déjà été tiré.
nums[i] = tempNum;//On l'inscrit dans la case.
}
for (i=0 ; i < LETTRES ; i++)//On tire les lettres.
{
do
{
tempNum = alea(0, 26);
}
while (contient(lettres, LETTRES, tempNum));//On vérifie qu'elle n'a pas déjà été tirée.
lettres[i] = tempNum;//On l'inscrit dans las case.
}
}
int contient(int tableau[], int taille, int valeur)
{
int estContenu = 0;
int i;
for (i=0 ; i < taille && estContenu == 0 ; i++)
{
if (tableau[i] == valeur) estContenu = 1;
}
return estContenu;
} |
Tri.c:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include "tri.h"
void tri_selection(int tableau[], int taille)
{
int index_min;
int tampon;
int i;
int j;
for (i=0 ; i < taille - 1 ; i++)
{
index_min = i;
for (j=i+1 ; j < taille ; j++)
{
if (tableau[j] < tableau[index_min]) index_min = j;
}
if (index_min != i)
{
tampon = tableau[i];
tableau[i] = tableau[index_min];
tableau[index_min] = tampon;
}
}
}
void tri_bulles(int tableau[], int taille)
{
int index_min;
int tampon;
int i;
int echange_fait;
do
{
echange_fait = 0;
for (i=0 ; i < taille - 1 ; i++)
{
if (tableau[i] > tableau[i + 1])
{
tampon = tableau[i];
tableau[i] = tableau[i + 1];
tableau[i + 1] = tampon;
echange_fait = 1;
}
}
}
while (echange_fait == 1);
}
void inverser(int tableau[], int taille)
{
int i, tampon;
for (i=0 ; i < taille / 2 ; i++)
{
tampon = tableau[i];
tableau[i] = tableau[taille - 1 - i];
tableau[taille - 1 - i] = tampon;
}
} |
Affichage.c
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include "affichage.h"
void affichage(int nums[], int lettres[])
{
int i;
printf("\n\t");
for (i=0 ; i < NOMBRES ; i++)
{
printf(" %d ", nums[i]);
}
printf(" -|- ");
for (i=0 ; i < LETTRES ; i++)
{
printf("%c ", tabLettres[lettres[i]]);
}
printf("\n");
} |
Tirage.h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef __TIRAGE_H__
#define __TIRAGE_H__
#include <stdlib.h>
#include <time.h>
//Nombre de nombres tirés.
#define NOMBRES 10
//Nombre de lettres tirées.
#define LETTRES 4
static char tabLettres[27] = "abcdefghijklmnopqrstuvwxyz";
void initialiser();
int alea(int min, int max);
void tirage(int nums[], int lettres[]);
int contient(int tableau[], int taille, int valeur);
#endif /* __TIRAGE_H__ */ |
Tri.h :
Code:
1 2 3 4 5 6 7 8 9
|
#ifndef __TRI_H__
#define __TRI_H__
void tri_selection(int tableau[], int taille);
void tri_bulles(int tableau[], int taille);
void inverser(int tableau[], int taille);
#endif /* __TRI_H__ */ |
Affichage.h:
Code:
1 2 3 4 5 6 7 8 9 10
|
#ifndef __AFFICHAGE_H__
#define __AFFICHAGE_H__
#include "tirage.h"
#include <stdio.h>
void affichage(int nums[], int lettres[]);
#endif /* __AFFICHAGE_H__ */ |
En espérant que ça ne soit pas inutile et qu'une page man ne fasse pas mon taf à ma place :aie:
Sur ce, à plus sur le forum & Enjoy ! :D