Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // Files
- //
- // Created by Abdelali on 30/04/2016.
- // Copyright © 2016 Abdelaali. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define List "/Users/MacBook/Documents/workspace/xCode WorkPlace/Files/Files/List.txt"
- #define ListSorted "/Users/MacBook/Documents/workspace/xCode WorkPlace/Files/Files/ListSorted.txt"
- #define ListGradeA "/Users/MacBook/Documents/workspace/xCode WorkPlace/Files/Files/ListeNoteA.txt"
- #define ListGradeF "/Users/MacBook/Documents/workspace/xCode WorkPlace/Files/Files/ListeNoteF.txt"
- typedef struct Etudiant {
- char Nom[50];
- char Prenom[50];
- double Note;
- }Etd;
- void ajouterEtd(int,Etd*,int*,int*);
- void trii(Etd*,int,Etd,int*);
- int main(int argc, const char * argv[]) {
- FILE* f = NULL;
- FILE* FLS = NULL;
- FILE* FLGA = NULL;
- FILE* FLGF = NULL;
- Etd* E;
- Etd* Ehelp;
- Etd a;
- //char c,b;
- int n,i,pos=0;
- int Ln=5,Lp=5;
- printf("Donner le nbr d'etudiant: ");
- do{
- scanf("%d",&n);
- }while (n<1);
- E=(Etd*)malloc(n*sizeof(Etd));
- Ehelp=(Etd*)malloc(n*sizeof(Etd));
- ajouterEtd(n,E,&Ln,&Lp);
- f = fopen(List, "w");
- if(!f){
- fprintf(stderr, "Impossible d'ouvrire ou creer le fichier.");
- }else{
- fprintf(f,"Nom%*.*sPrenom%*.*sNote\n",Ln-(int)strlen("nom")+1,Ln-(int)strlen("nom")+1," ",Lp-(int)strlen("prenom")+1,Lp-(int)strlen("prenom")+1," ");
- }
- fclose(f);
- f=fopen(List, "a");
- if(!f){
- fprintf(stderr, "Impossible d'ouvrire ou creer le fichier.");
- }else{
- for(i=0;i<n;i++){
- fprintf(f,"%s%*.*s %s%*.*s %2.2lf\n",E[i].Nom,(Ln==strlen(E[i].Nom))?0:Ln-(int)strlen(E[i].Nom),(Ln==strlen(E[i].Nom))?0:Ln-(int)strlen(E[i].Nom)," ",E[i].Prenom,(Lp==strlen(E[i].Prenom))?0:Lp-(int)strlen(E[i].Prenom),(Lp==strlen(E[i].Prenom))?0:Lp-(int)strlen(E[i].Prenom)," ",E[i].Note);
- }
- }
- fclose(f);
- f=fopen(List, "r");
- FLS = fopen(ListSorted, "w");
- if(!FLS){
- fprintf(stderr, "Impossible d'ouvrire ou creer le fichier.");
- }else{
- fprintf(FLS,"Nom%*.*sPrenom%*.*sNote\n",Ln-(int)strlen("nom")+1,Ln-(int)strlen("nom")+1," ",1+Lp-(int)strlen("prenom"),1+Lp-(int)strlen("prenom")," ");
- }
- fclose(FLS);
- fscanf(f, "%s %s %s",a.Nom,a.Prenom,a.Nom);
- i=0;
- while (!feof(f)) {
- fscanf(f, "%s %s %lf",Ehelp[i].Nom,Ehelp[i].Prenom,&Ehelp[i].Note);
- i++;
- }
- trii(Ehelp, n, a,&pos);
- FLS =fopen(ListSorted, "a");
- for(i=0;i<n;i++){
- fprintf(FLS,"%s%*.*s%s%*.*s%2.2lf\n",Ehelp[i].Nom,1+Ln-(int)strlen(Ehelp[i].Nom),1+Ln-(int)strlen(Ehelp[i].Nom)," ",Ehelp[i].Prenom,1+Lp-(int)strlen(Ehelp[i].Prenom),1+Lp-(int)strlen(Ehelp[i].Prenom)," ",Ehelp[i].Note);
- }
- for(i=0;i<n;i++){
- printf("%s\t%s\t%2.2lf\t%d\n",Ehelp[i].Nom,Ehelp[i].Prenom,Ehelp[i].Note,pos);
- }
- fclose(FLS);
- //free(Ehelp);
- //Ehelp = (Etd*) malloc(pos*sizeof(Etd));
- FLGA = fopen(ListGradeA, "w");
- if(!FLGA){
- fprintf(stderr, "Impossible d'ouvrire ou creer le fichier.");
- }else{
- fprintf(FLGA,"Nom%*.*sPrenom%*.*sNote\n",Ln-(int)strlen("nom")+1,Ln-(int)strlen("nom")+1," ",Lp-(int)strlen("prenom")+1,Lp-(int)strlen("prenom")+1," ");
- }
- FLGF = fopen(ListGradeF, "w");
- if(!FLGF){
- fprintf(stderr, "Impossible d'ouvrire ou creer le fichier.");
- }else{
- fprintf(FLGF,"Nom%*.*sPrenom%*.*sNote\n",Ln-(int)strlen("nom")+1,Ln-(int)strlen("nom")+1," ",Lp-(int)strlen("prenom")+1,Lp-(int)strlen("prenom")+1," ");
- }
- //fclose(FLGA);
- //FLGA = fopen(ListGradeA, "a");
- for (i=0; i<n; i++) {
- if(Ehelp[i].Note>=10)
- fprintf(FLGA,"%s%*.*s%s%*.*s%2.2lf\n",Ehelp[i].Nom,1+Ln-(int)strlen(a.Nom),1+Ln-(int)strlen(a.Nom)," ",Ehelp[i].Prenom,1+Lp-(int)strlen(a.Prenom),1+Lp-(int)strlen(a.Prenom)," ",Ehelp[i].Note);
- else
- fprintf(FLGF,"%s%*.*s %s%*.*s %2.2lf\n",Ehelp[i].Nom,Ln-(int)strlen(a.Nom),Ln-(int)strlen(a.Nom)," ",Ehelp[i].Prenom,Lp-(int)strlen(a.Prenom),Lp-(int)strlen(a.Prenom)," ",Ehelp[i].Note);
- }
- return 0;
- }
- void trii(Etd* Ehelp,int n,Etd a,int* p){
- int i,j;
- for(i=0;i<n;i++){
- if(Ehelp[i].Note>=10.0)
- (*p)++;
- for(j=i+1;j<n;j++){
- if (Ehelp[i].Note<Ehelp[j].Note) {
- a.Note = Ehelp[i].Note;
- strcpy(a.Nom, Ehelp[i].Nom);
- strcpy(a.Prenom, Ehelp[i].Prenom);
- Ehelp[i].Note = Ehelp[j].Note;
- strcpy(Ehelp[i].Nom, Ehelp[j].Nom);
- strcpy(Ehelp[i].Prenom, Ehelp[j].Prenom);
- Ehelp[j].Note = a.Note;
- strcpy(Ehelp[j].Nom, a.Nom);
- strcpy(Ehelp[j].Prenom, a.Prenom);
- }
- }
- }
- }
- void ajouterEtd(int n, Etd* E,int*Ln,int*Lp){
- int i,j;
- for(i=0;i<n;i++){
- printf("Donner le nom d'etudiant N° %d:\n-> ",i+1);
- getchar();
- gets(E[i].Nom);
- printf("Donner le prenom d'etudiant N° %d:\n-> ",i+1);
- getchar();
- gets(E[i].Prenom);
- printf("Donner la note d'etudiant N° %d:\n-> ",i+1);
- scanf("%lf",&E[i].Note);
- *Ln = (int)strlen(E[0].Nom);
- *Lp = (int)strlen(E[0].Prenom);
- for(j=1;j<=i;j++){
- if (*Ln<strlen(E[j].Nom)) {
- *Ln = (int)strlen(E[j].Nom);
- }
- if (*Lp<strlen(E[j].Prenom)) {
- *Lp = (int) strlen(E[j].Prenom);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement