fork download
  1. // Lab 8, Working with structs
  2. // Programmer : Maiar Khattab
  3. // Editor(s) used : Code Blocks 13.12
  4. // Compiler(s) used : Code Blocks 13.12
  5.  
  6. #include<iostream>
  7. using std::cout;
  8. using std::endl;
  9.  
  10. #include<cstdlib>
  11.  
  12. //struct def
  13. struct tod
  14. {
  15. int hour;// the hr , 0-23
  16. int minute;// the min, 0-59
  17. int second;//the sec, 0-59
  18. char descr [32];//the description of the time of day
  19.  
  20. };
  21. //void printTod(const tod&);
  22. int main ()
  23. {
  24. cout << "Lab 8, Working With structs\n";
  25. cout << "Programmer: Maiar Khattab\n";
  26. cout << "Editor(s) used: Code Blocks 13.12\n";
  27. cout << "Compiler(s) used: Code Blocks 13.12\n";
  28. cout << "File: " << __FILE__ << endl;
  29. cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
  30.  
  31. tod theTime[] = {{12,0,0, "noon"},
  32. {0,0,0," midnight"},
  33. {6,00,00," supper "},
  34. {11,30,0,"bedtime"}};
  35.  
  36. for(int i; i <5; i++)
  37. {
  38. char descr [32];
  39. cout << theTime[i].descr << " is " << theTime[i].hour << ':'
  40. << theTime[i].minute << ":" << theTime[i].second << endl;
  41. }
  42. }
  43.  
  44.  
Success #stdin #stdout 0.01s 5272KB
stdin
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX_VOLS      100
#define MAX_RESA      500
#define MAX_CIN_LEN    10
#define MAX_CITY_LEN   50
#define DATE_LEN       11   // "jj/mm/aaaa"
#define TIME_LEN        6   // "hh:mm"

/*==========================================
  STRUCTURES & TYPES
==========================================*/
typedef struct {
    int  numeroVol;
    char villeDepart[MAX_CITY_LEN];
    char villeArrivee[MAX_CITY_LEN];
    char dateDepart[DATE_LEN];
    char heureDepart[TIME_LEN];
    int  dureeMinutes;
    int  nbPlacesTotal;
    int  nbPlacesDispo;
    bool actif;
} Vol;

typedef enum {
    RESA_CONFIRMEE,
    RESA_ANNULEE
} StatutResa;

typedef struct {
    char cin[MAX_CIN_LEN];
    char nom[30];
    char prenom[30];
} Passager;

typedef struct {
    int        numeroResa;
    int        numeroVol;
    char       dateVol[DATE_LEN];
    char       destination[MAX_CITY_LEN];
    int        numeroSiege;
    StatutResa statut;
    Passager   passager;
} Reservation;

/*==========================================
  VARIABLES GLOBALES
==========================================*/
Vol vols[MAX_VOLS];
int nbVols = 0;

Reservation reservations[MAX_RESA];
int nbReservations = 0;
int compteurResa = 1;

/*==========================================
  PROTOTYPES DE FONCTIONS
==========================================*/
/* Menus */
void afficherMenuPrincipal(void);
void afficherMenuVols(void);
void afficherMenuReservations(void);
void afficherMenuStatistiques(void);

/* Vols */
void afficherListeVols(void);
void afficherUnVol(const Vol *v);
void ajouterVol(void);
void modifierVol(void);
void supprimerVol(void);
void rechercherVol(void);

/* Réservations */
void reserverBillet(void);
void afficherReservationsParCIN(void);
void annulerReservation(void);

/* Statistiques */
void afficherStatistiquesGlobales(void);

/*==========================================
  IMPLEMENTATIONS
==========================================*/
void afficherMenuPrincipal() {
    printf("\n=========================================\n");
    printf("   Bienvenue dans notre espace Aérien\n");
    printf("=========================================\n");
    printf("1 - Gestion des vols\n");
    printf("2 - Gestion des réservations\n");
    printf("3 - Statistiques globales\n");
    printf("0 - Quitter\n");
    printf("=========================================\n");
    printf("Votre choix : ");
}

void afficherMenuVols() {
    printf("\n--- Gestion des vols ---\n");
    printf("1 - Afficher la liste des vols\n");
    printf("2 - Ajouter un nouveau vol\n");
    printf("3 - Modifier un vol existant\n");
    printf("4 - Supprimer un vol\n");
    printf("5 - Rechercher un vol\n");
    printf("0 - Retour\n");
    printf("Choix : ");
}

void afficherMenuReservations() {
    printf("\n--- Gestion des réservations ---\n");
    printf("1 - Réserver un billet\n");
    printf("2 - Afficher réservations d'un passager\n");
    printf("3 - Annuler une réservation\n");
    printf("0 - Retour\n");
    printf("Choix : ");
}

void afficherMenuStatistiques() {
    printf("\n--- Statistiques globales ---\n");
    printf("1 - Afficher statistiques\n");
    printf("0 - Retour\n");
    printf("Choix : ");
}

void afficherUnVol(const Vol *v) {
    printf("\nVol N°%d\n", v->numeroVol);
    printf("Départ    : %s\n", v->villeDepart);
    printf("Arrivée   : %s\n", v->villeArrivee);
    printf("Date/Heure: %s %s\n", v->dateDepart, v->heureDepart);
    printf("Durée     : %d min\n", v->dureeMinutes);
    printf("Places    : total %d / dispo %d\n", v->nbPlacesTotal, v->nbPlacesDispo);
    printf("Statut    : %s\n", v->actif ? "Actif" : "Annulé");
}

void afficherListeVols() {
    if (nbVols == 0) {
        printf("Aucun vol enregistré.\n");
        return;
    }
    for (int i = 0; i < nbVols; i++) {
        if (vols[i].actif)
            afficherUnVol(&vols[i]);
    }
}

void ajouterVol() {
    if (nbVols >= MAX_VOLS) {
        printf("Capacité maximale de vols atteinte.\n");
        return;
    }
    Vol v;
    printf("Numéro du vol : ");
    scanf("%d", &v.numeroVol);
    // unicité
    for (int i = 0; i < nbVols; i++)
        if (vols[i].numeroVol == v.numeroVol) {
            printf("Ce numéro existe déjà.\n");
            return;
        }
    printf("Ville de départ : ");
    scanf("%s", v.villeDepart);
    printf("Ville d’arrivée : ");
    scanf("%s", v.villeArrivee);
    printf("Date départ (jj/mm/aaaa) : ");
    scanf("%s", v.dateDepart);
    printf("Heure départ (hh:mm)     : ");
    scanf("%s", v.heureDepart);
    printf("Durée (minutes)         : ");
    scanf("%d", &v.dureeMinutes);
    printf("Nombre total de places  : ");
    scanf("%d", &v.nbPlacesTotal);
    v.nbPlacesDispo = v.nbPlacesTotal;
    v.actif = true;

    vols[nbVols++] = v;
    printf("Vol ajouté.\n");
}

void modifierVol() {
    int num, idx = -1;
    printf("Numéro du vol à modifier : ");
    scanf("%d", &num);
    for (int i = 0; i < nbVols; i++)
        if (vols[i].numeroVol == num) { idx = i; break; }
    if (idx < 0) {
        printf("Vol non trouvé.\n");
        return;
    }
    // compter réservations
    int resaCount = 0;
    for (int i = 0; i < nbReservations; i++)
        if (reservations[i].numeroVol == num &&
            reservations[i].statut == RESA_CONFIRMEE)
            resaCount++;

    printf("1-Destination 2-Date/Heure 3-Durée\n"
           "4-Places totales 5-Statut 0-Annuler\nChoix : ");
    int c; scanf("%d", &c);
    switch (c) {
        case 1:
            printf("Nouvelle destination : ");
            scanf("%s", vols[idx].villeArrivee);
            break;
        case 2:
            printf("Nouvelle date : ");
            scanf("%s", vols[idx].dateDepart);
            printf("Nouvelle heure: ");
            scanf("%s", vols[idx].heureDepart);
            break;
        case 3:
            printf("Nouvelle durée : ");
            scanf("%d", &vols[idx].dureeMinutes);
            break;
        case 4: {
            int nt;
            printf("Nouv. places totales : ");
            scanf("%d", &nt);
            if (nt < resaCount) {
                printf("%d places déjà réservées.\n", resaCount);
            } else {
                vols[idx].nbPlacesDispo += (nt - vols[idx].nbPlacesTotal);
                vols[idx].nbPlacesTotal = nt;
            }
            break;
        }
        case 5: {
            int s;
            printf("Statut (1=actif,0=annulé) : ");
            scanf("%d", &s);
            vols[idx].actif = (s==1);
            break;
        }
        default:
            printf("Annulation/modif invalide.\n");
    }
}

void supprimerVol() {
    int num, idx = -1;
    printf("Numéro du vol à supprimer : ");
    scanf("%d", &num);
    for (int i = 0; i < nbVols; i++)
        if (vols[i].numeroVol == num) { idx = i; break; }
    if (idx < 0) {
        printf("Vol non trouvé.\n");
        return;
    }
    int resaCount = 0;
    for (int i = 0; i < nbReservations; i++)
        if (reservations[i].numeroVol == num &&
            reservations[i].statut == RESA_CONFIRMEE)
            resaCount++;
    if (resaCount) {
        printf("%d réservation(s) existantes. Confirmez suppression? (o/n): ", resaCount);
        char c; getchar(); c=getchar();
        if (c!='o' && c!='O') {
            printf("Suppression annulée.\n");
            return;
        }
    }
    for (int i = idx; i < nbVols-1; i++)
        vols[i] = vols[i+1];
    nbVols--;
    printf("Vol supprimé.\n");
}

void rechercherVol() {
    printf("1=Numéro 2=Destination : ");
    int m; scanf("%d", &m);
    if (m==1) {
        int num; printf("Numéro : "); scanf("%d", &num);
        for (int i=0;i<nbVols;i++)
            if (vols[i].numeroVol==num)
                afficherUnVol(&vols[i]);
    } else if (m==2) {
        char dest[MAX_CITY_LEN];
        printf("Destination : "); scanf("%s", dest);
        for (int i=0;i<nbVols;i++)
            if (strcmp(vols[i].villeArrivee,dest)==0)
                afficherUnVol(&vols[i]);
    }
}

void reserverBillet() {
    int num;
    printf("Numéro du vol : ");
    scanf("%d", &num);
    int idx=-1;
    for(int i=0;i<nbVols;i++)
        if(vols[i].numeroVol==num && vols[i].actif)
            idx=i;
    if(idx<0 || vols[idx].nbPlacesDispo==0) {
        printf("Vol non dispo.\n");
        return;
    }
    Reservation r;
    r.numeroResa = compteurResa++;
    r.numeroVol  = num;
    strcpy(r.dateVol, vols[idx].dateDepart);
    strcpy(r.destination, vols[idx].villeArrivee);
    r.numeroSiege = vols[idx].nbPlacesTotal - vols[idx].nbPlacesDispo + 1;
    r.statut     = RESA_CONFIRMEE;

    printf("CIN   : "); scanf("%s", r.passager.cin);
    printf("Nom   : "); scanf("%s", r.passager.nom);
    printf("Prénom: "); scanf("%s", r.passager.prenom);

    reservations[nbReservations++] = r;
    vols[idx].nbPlacesDispo--;
    printf("Réservation #%d confirmée, siège %d.\n",
           r.numeroResa, r.numeroSiege);
}

void afficherReservationsParCIN() {
    char cin[MAX_CIN_LEN];
    printf("CIN passager : ");
    scanf("%s", cin);
    for(int i=0;i<nbReservations;i++){
        if(strcmp(reservations[i].passager.cin,cin)==0){
            printf("\nRéservation N°%d\n", reservations[i].numeroResa);
            printf("Vol     : %d\n", reservations[i].numeroVol);
            printf("Date    : %s\n", reservations[i].dateVol);
            printf("Dest.   : %s\n", reservations[i].destination);
            printf("Siège   : %d\n", reservations[i].numeroSiege);
            printf("Statut  : %s\n",
                   reservations[i].statut==RESA_CONFIRMEE ? "Confirmée" : "Annulée");
        }
    }
}

void annulerReservation() {
    printf("Annuler par 1=NumResa 2=CIN+Vol : ");
    int m; scanf("%d",&m);
    int found=0;
    if(m==1){
        int nr; printf("N° réservation : "); scanf("%d",&nr);
        for(int i=0;i<nbReservations;i++){
            if(reservations[i].numeroResa==nr &&
               reservations[i].statut==RESA_CONFIRMEE){
                reservations[i].statut=RESA_ANNULEE;
                // restituer place
                for(int j=0;j<nbVols;j++){
                    if(vols[j].numeroVol==reservations[i].numeroVol){
                        vols[j].nbPlacesDispo++;
                        break;
                    }
                }
                printf("Annulation OK.\n");
                found=1;
            }
        }
    } else if(m==2){
        char cin[MAX_CIN_LEN]; int num;
        printf("CIN   : "); scanf("%s",cin);
        printf("Vol # : "); scanf("%d",&num);
        for(int i=0;i<nbReservations;i++){
            if(strcmp(reservations[i].passager.cin,cin)==0 &&
               reservations[i].numeroVol==num &&
               reservations[i].statut==RESA_CONFIRMEE){
                reservations[i].statut=RESA_ANNULEE;
                for(int j=0;j<nbVols;j++){
                    if(vols[j].numeroVol==num){
                        vols[j].nbPlacesDispo++;
                        break;
                    }
                }
                printf("Annulation OK.\n");
                found=1;
            }
        }
    }
    if(!found) printf("Réservation non trouvée.\n");
}

void afficherStatistiquesGlobales() {
    int actifs=0, annules=0;
    int totalResa=nbReservations, resaAnn=0;
    int totalPlacesRest=0;

    for(int i=0;i<nbVols;i++){
        if(vols[i].actif) actifs++;
        else annules++;
        totalPlacesRest += vols[i].nbPlacesDispo;
    }
    for(int i=0;i<nbReservations;i++)
        if(reservations[i].statut==RESA_ANNULEE)
            resaAnn++;

    // passagers uniques
    char seen[MAX_RESA][MAX_CIN_LEN];
    int uniqueP=0;
    for(int i=0;i<nbReservations;i++){
        bool dup=false;
        for(int j=0;j<uniqueP;j++)
            if(strcmp(seen[j], reservations[i].passager.cin)==0)
                dup=true;
        if(!dup){
            strcpy(seen[uniqueP++], reservations[i].passager.cin);
        }
    }
    // vol le plus réservé
    int maxR=0, volMax=0;
    for(int v=0;v<nbVols;v++){
        int cnt=0;
        for(int r=0;r<nbReservations;r++)
            if(reservations[r].numeroVol==vols[v].numeroVol &&
               reservations[r].statut==RESA_CONFIRMEE)
                cnt++;
        if(cnt>maxR){ maxR=cnt; volMax=vols[v].numeroVol; }
    }
    printf("\n--- Statistiques ---\n");
    printf("Vols totaux       : %d\n", nbVols);
    printf("Vols actifs       : %d\n", actifs);
    printf("Vols annulés      : %d\n", annules);
    printf("Réservations tot. : %d\n", totalResa);
    printf("Réservations ann. : %d\n", resaAnn);
    printf("Passagers uniques : %d\n", uniqueP);
    printf("Vol le + réservé  : #%d (%d reservations)\n", volMax, maxR);
    if(nbVols)
        printf("Moyenne places rest.: %.2f\n",
               (float)totalPlacesRest/nbVols);
}

int main(void) {
    int choix, sc;
    do {
        afficherMenuPrincipal();
        scanf("%d", &choix);
        switch (choix) {
            case 1:
                do {
                    afficherMenuVols();
                    scanf("%d",&sc);
                    switch(sc){
                        case 1: afficherListeVols();       break;
                        case 2: ajouterVol();              break;
                        case 3: modifierVol();             break;
                        case 4: supprimerVol();            break;
                        case 5: rechercherVol();           break;
                    }
                } while(sc!=0);
                break;
            case 2:
                do {
                    afficherMenuReservations();
                    scanf("%d",&sc);
                    switch(sc){
                        case 1: reserverBillet();           break;
                        case 2: afficherReservationsParCIN(); break;
                        case 3: annulerReservation();        break;
                    }
                } while(sc!=0);
                break;
            case 3:
                do {
                    afficherMenuStatistiques();
                    scanf("%d",&sc);
                    if(sc==1) afficherStatistiquesGlobales();
                } while(sc!=0);
                break;
            case 0:
                printf("Au revoir !\n");
                break;
            default:
                printf("Choix invalide.\n");
        }
    } while(choix!=0);
    return 0;
}
stdout
Lab 8, Working With structs
Programmer: Maiar Khattab
Editor(s) used: Code Blocks 13.12
Compiler(s) used: Code Blocks 13.12
File: prog.cpp
Complied: May 18 2025 at 13:56:14

noon is 12:0:0
 midnight is 0:0:0
 supper  is 6:0:0
bedtime is 11:30:0
�Qj( �� is 124290712:32764:185754880