0% ont trouvé ce document utile (0 vote)
8 vues2 pages

Typedef Struct Char Int: "Smartphone %D: /N"

Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
8 vues2 pages

Typedef Struct Char Int: "Smartphone %D: /N"

Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

#include<stdlib.

h>
#include<stdlib.h>
#include<string.h>
// Q1
typedef struct type_telephone{
char mq[50];
int modele ;
}typtel;
typedef struct smarthphone{
int id_client ;
typtel Tt ;
int Tp ;
}SP ;
// Q2
SP* reserverMemoire(int N){
return (SP*)malloc(N * sizeof(SP));
}
void saisie(SP *T ,int N){
int drp;
for(int i=0;i<N;i++){
printf("smartphone %d : \n" , i+1);
do{
printf("l'ID du client : ");
scanf("%d",&T[i].id_client);
drp=0;
for (int j=0;j<i;j++){
if(T[j].id_client ==T[i].id_client)
drp=1; break ;
}
}while(drp==1);
printf("marque :");
scanf("%s",&T[i].Tt.mq);
printf(" modele :");
scanf("%d",&T[i].Tt.modele);
do{
printf(" type de panne (1 pour materiel , 2 pour logiciel) :");
scanf("%d",&T[i].Tp);
}while(T[i].Tp != 1 && T[i].Tp != 2);

}
}
void affiche(SP * T , int N ,typtel tt){
printf("les clients ayant une marque %s et un modele %d sont : \n",tt.mq , tt.modele);
for(int i=0 ; i<N ;i++){
if(strcmp(T[i].Tt.mq,tt.mq)==0 && T[i].Tt.modele==tt.modele)
printf("le client %d a une panne %d\n",T[i].id_client,T[i].Tp);
}
}
void trier(SP *T , int N ){
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
if (T[i].Tt.modele< T[j].Tt.modele) {
SP temp = T[i];
T[i] = T[j];
T[j] = temp;
}
}
}
}
int compterPannesMaterielles(SP* T, int N) {
int count = 0;
for (int i = 0; i < N; i++) {
if (T[i].Tp == 1) {
count++;
}
}
return count;
}
int main() {
int N;
printf("Entrez le nombre de smartphones: ");
scanf("%d", &N);

// Réserver la mémoire pour le tableau T


SP* T = reserverMemoire(N);
if (T == NULL) {
printf("Erreur d'allocation de memoire.\n");
return 1;
}

// Saisir les éléments du tableau


saisie(T, N);

// Afficher les Id_client et Type_de_panne pour un Type_Telephone donné


typtel tt;
printf("\nEntrez la marque a rechercher: ");
scanf("%s", &tt.mq);
printf("Entrez le modele a rechercher: ");
scanf("%d", &tt.modele);
affiche(T, N,tt);

// Trier le tableau par Modele et l'afficher


trier(T, N);
printf("\nTableau trié par Modele:\n");
for (int i = 0; i < N; i++) {
printf("Id_client: %d, Marque: %s, Modele: %d, Type_de_panne: %d\n",T[i].id_client, T[i].Tt.mq, T[i].Tt.
modele, T[i].Tp);
}

// Compter les smartphones avec panne matérielle


int pannesMaterielles = compterPannesMaterielles(T, N);
printf("\nNombre de smartphones avec panne materielle: %d\n", pannesMaterielles);

// Libérer la mémoire
free(T);
return 0;
}

Vous aimerez peut-être aussi