Sondage elections presidentielles langagec

Contenu du snippet

Utilisation de structures de fichiers: ecriture et lecture, des fonctions.
Ce programme comporte un menu de presentation, des saisies de chaines de caracteres et des lecture de chaines de caracteres. Une fonction qui evite les doublons(=redondance des personnes saisies). Une fonction statistique qui serait à améliorer. Note: 16/20.

Source / Exemple :


#include <stdio.h>
#include <conio.h>
#include <string.h>
#define LGNOM 20
#define LGPRENOM 15
#define LGTEL 11
#define LGMOIS 25
#define LGJOUR 3
#define LGANNEE 5
#define LGNOMCHER 20
#define LGVERIFNOM 20
#define LGVERIFPRENOM 15
#define LGNOM1 20
#define LGPRENOM1 15
#define LGCANDIDAT 3

int i;
float somme=0;

FILE *sortie;

struct 	{ char nom[LGNOM+1];
	char prenom[LGPRENOM+1];
	char jour[LGJOUR+1];
	char mois[LGMOIS+1];
	char annee[LGANNEE+1];
	char tel[LGTEL+1];
	char candidat[LGCANDIDAT+1];
	}fait;

FILE *entree;

char * tampon;
char * nomfic="c:\meaux2.dat";

void main (void)
{
void introduction (void);
void menu (void);
void saisie (void);
void lecture (void);
void liste (void);
void verif (void);
void statistique (void);

clrscr();
introduction();
getch();

clrscr();
menu();
getch();

}

void introduction (void)
{
clrscr();
printf ("\n\n\n\n\n\n\n\n\t\t\tB I E N V E N U E.");
getch();
clrscr();
printf ("\n\n\n\n\n\n\n\n\n\t\tSondage pour les ‚lections pr‚sidentielles.");
getch();
}

void menu (void)
{
int n;

printf ("\n\t\t\t\tMenu G‚n‚ral\n\n\n\n");
printf ("\t\t1) Vous voulez vous inscrire.\n\n\n");
printf ("\t\t2) Rechercher une personne.\n\n\n");
printf ("\t\t3) Impression de la liste d'inscription.\n\n\n");
printf ("\t\t4) V‚rifiez si vous ˆtes d‚j… inscrit.\n\n\n");
printf ("\t\t5) Stat:\n\n\n");
printf ("\nVotre choix:");
scanf("%d",&n);

if (n==1)
{
clrscr();
saisie();
getch();
}

if (n==2)
{
clrscr();
lecture();
getch();
}

if (n==3)
{
clrscr();
liste();
getch();
}

if (n==4)
{
clrscr();
verif();
getch();
}

if(n==5)
{
clrscr();
statistique();
getch();
}

}

void saisie (void)
{
sortie=fopen (nomfic,"a");

printf ("Entrez un nom en MAJUSCULES lors de la saisie.");
printf ("\n\nLaisser un nom VIDE … la fin de chaque saisie.");
gets (tampon);

while (printf ("\n\nNom:"), gets (fait.nom), strlen (fait.nom))
{
printf ("Pr‚nom:");
scanf ("%s",&fait.prenom);
printf ("Jour:");
scanf ("%s",&fait.jour);
printf ("Mois:");
scanf ("%s",&fait.mois);
printf ("Ann‚e:");
scanf ("%s",&fait.annee);
printf ("T‚l‚phone:");
scanf ("%s",&fait.tel);
printf ("Candidat LJ, JC, LP, RH, AL, NM:");
scanf ("%s",&fait.candidat);

fwrite (&fait, sizeof(fait), 1, sortie);
}
fclose (sortie);
}

void verif (void)
{
int intrus;
char verifnom[LGVERIFNOM+1];
char verifprenom[LGVERIFPRENOM+1];
char nom1[LGNOM1+1];
char prenom1[LGPRENOM1+1];

printf ("Entrer un nom et un prenom puis le prenom et le nom … v‚rifier:");
scanf ("%s%s%s%s",&verifnom,&verifprenom,&prenom1,&nom1);
intrus=0;

entree=fopen (nomfic,"r");

do
{
fread (&fait, sizeof(fait), 1, entree);
if (((strcmp (verifnom, fait.nom)==0)&&(strcmp (verifprenom, fait.prenom)==0))||((strcmp (prenom1,fait.prenom)==0)&&(strcmp (nom1,fait.nom)==0)))
intrus=1;
}
while ((!intrus)&&(!feof (entree)));

if (intrus)
{
printf ("Vous ˆtes d‚j… inscrit.");
}

else
{
printf ("Inscrivez vous.");
menu();
}

fclose (entree);
}

void lecture (void)
{
char nomcher[LGNOMCHER+1];
int trouve;

entree=fopen (nomfic,"r");
printf ("Nom cherch‚:");
scanf ("%s",&nomcher);
trouve=0;

do
{
fread (&fait, sizeof(fait), 1, entree);
if (strcmp (nomcher, fait.nom)==0)
trouve=1;
}
while ((!trouve)&&(!feof (entree)));

if (trouve)
{
printf ("prenom:%s\n",fait.prenom);
printf ("jour:%s\n",fait.jour);
printf ("mois:%s\n",fait.mois);
printf ("annee:%s\n",fait.annee);
printf ("t‚l‚phone:%s\n",fait.tel);
printf ("candidat:%s\n",fait.candidat);
}

else
printf ("Ce nom ne figure pas dans la base de donn‚es.");

fclose (entree);
}

void liste (void)
{

entree=fopen (nomfic,"r");
printf ("\t\t\tListe des personnes inscrites.");
printf ("\n\n\n\tNom\tPr‚nom\tJour\tMois\tAnnee\tT‚l‚phone\tCandidat");

while (fread (&fait, sizeof(fait), 1, entree), !feof(entree))
{
printf ("\n\t%s  %s   %s      %s    %s    %s         %s",fait.nom,fait.prenom,fait.jour,fait.mois,fait.annee,fait.tel,fait.candidat);
}

fclose (entree);

/*printf ("Impression de la liste");
fprintf (stdprn,"Liste des personnes inscrites");*/
}

void statistique (void)
{
float somme_LJ=0, somme_JC=0, somme_LP=0, somme_RH=0, somme_AL=0, somme_NM=0,som;
float pourcentage_LJ, pourcentage_JC, pourcentage_LP, pourcentage_RH, pourcentage_AL, pourcentage_NM;
entree=fopen (nomfic,"r");

do
{
fread (&fait, sizeof (fait), 1, entree);

  if (fait.nom)
	{
somme=somme+1;
printf ("\n\t%s",fait.nom);
printf ("\n%f",somme);
	}

 if (strcmp (fait.candidat,"LJ")==0)
	{
somme_LJ=somme_LJ+1;
pourcentage_LJ=(somme_LJ/somme)*100;
printf ("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\%f",somme_LJ);
printf ("\n%f",pourcentage_LJ);
	}

 if (strcmp (fait.candidat,"JC")==0)
	{
somme_JC=somme_JC+1;
pourcentage_JC=(somme_JC/somme)*100;
printf ("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\n%f",somme_JC);
printf ("\n%f",pourcentage_JC);
	}

 if (strcmp (fait.candidat,"LP")==0)
	{
somme_LP=somme_LP+1;
pourcentage_LP=(somme_LP/somme)*100;
printf("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\n%f",somme_LP);
printf ("\n%f",pourcentage_LP);
	}

 if (strcmp (fait.candidat,"RH")==0)
	{
somme_RH=somme_RH+1;
pourcentage_RH=(somme_RH/somme)*100;
printf ("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\n%f",somme_RH);
printf ("\n%f",pourcentage_RH);
	}

 if (strcmp (fait.candidat,"AL")==0)
	{
somme_AL=somme_AL+1;
pourcentage_AL=(somme_AL/somme)*100;
printf ("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\n%f",somme_AL);
printf ("\n%f",pourcentage_AL);
	}

 if (strcmp (fait.candidat,"NM")==0)
	{
somme_NM=somme_NM+1;
pourcentage_NM=(somme_NM/somme)*100;
printf ("\n%s",fait.candidat);
printf ("\t%f",somme);
printf ("\n%f",somme_NM);
printf ("\n%f",pourcentage_NM);
	}

}
while (!feof (entree));

printf ("%f %f %f %f %f",somme,somme_JC,somme_LP,somme_AL,somme_NM);
printf ("\n\tLionel Jospin         Jacques Chirac        Jean-Marie Le Pen");
printf ("\n\n\t      %f \t             %f  \t            %f", pourcentage_LJ, pourcentage_JC, pourcentage_LP);

printf ("\n\n\n\t Robert  Hue         Arlette Laguillier         Noel Mamere");
printf ("\n\n\t      %f   \t            %f   \t            %f", pourcentage_RH, pourcentage_AL, pourcentage_NM);

}



A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.