Ajouter exo1/main.c
This commit is contained in:
parent
bdc038dff8
commit
542320fc55
171
exo1/main.c
Normal file
171
exo1/main.c
Normal file
@ -0,0 +1,171 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_PRODUITS 10
|
||||
#define MAX_NOM 30
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
char nom[MAX_NOM];
|
||||
float prix;
|
||||
int quantite;
|
||||
} Produit;
|
||||
|
||||
void initialiserInventaire(Produit *inventaire);
|
||||
void afficherInventaire(Produit *inventaire, int taille);
|
||||
void ajouterProduit(Produit *inventaire, int *nbProduits);
|
||||
void modifierQuantite(Produit *inventaire, int nbProduits);
|
||||
float calculerValeurTotale(Produit *inventaire, int nbProduits);
|
||||
void rechercherProduit(Produit *inventaire, int nbProduits);
|
||||
|
||||
int main() {
|
||||
Produit inventaire[MAX_PRODUITS];
|
||||
int nbProduits = 0;
|
||||
int choix;
|
||||
|
||||
initialiserInventaire(inventaire);
|
||||
|
||||
do {
|
||||
printf("\n===== GESTION D'INVENTAIRE =====\n");
|
||||
printf("1. Afficher l'inventaire\n");
|
||||
printf("2. Ajouter un produit\n");
|
||||
printf("3. Modifier la quantité d'un produit\n");
|
||||
printf("4. Calculer la valeur totale de l'inventaire\n");
|
||||
printf("5. Rechercher un produit\n");
|
||||
printf("0. Quitter\n");
|
||||
printf("Votre choix: ");
|
||||
scanf("%d", &choix);
|
||||
|
||||
switch(choix) {
|
||||
case 1:
|
||||
afficherInventaire(inventaire, nbProduits);
|
||||
break;
|
||||
case 2:
|
||||
ajouterProduit(inventaire, &nbProduits);
|
||||
break;
|
||||
case 3:
|
||||
modifierQuantite(inventaire, nbProduits);
|
||||
break;
|
||||
case 4: {
|
||||
float valeur = calculerValeurTotale(inventaire, nbProduits);
|
||||
printf("Valeur totale de l'inventaire: %.2f €\n", valeur);
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
rechercherProduit(inventaire, nbProduits);
|
||||
break;
|
||||
case 0:
|
||||
printf("Au revoir!\n");
|
||||
break;
|
||||
default:
|
||||
printf("Choix invalide!\n");
|
||||
}
|
||||
} while(choix != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void initialiserInventaire(Produit *inventaire) {
|
||||
for(int i = 0; i < MAX_PRODUITS; i++) {
|
||||
inventaire[i].id = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void afficherInventaire(Produit *inventaire, int taille) {
|
||||
if(taille == 0) {
|
||||
printf("L'inventaire est vide.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\n===== INVENTAIRE =====\n");
|
||||
printf("ID\tNOM\t\tPRIX\tQUANTITÉ\n");
|
||||
|
||||
for(int i = 0; i <= taille; i++) {
|
||||
if(inventaire[i].id != -1) {
|
||||
printf("%d\t%s\t\t%.2f\t%d\n",
|
||||
inventaire[i].id,
|
||||
inventaire[i].nom,
|
||||
inventaire[i].prix,
|
||||
inventaire[i].quantite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ajouterProduit(Produit *inventaire, int *nbProduits) {
|
||||
if(*nbProduits >= MAX_PRODUITS) {
|
||||
printf("L'inventaire est plein!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Produit nouveau;
|
||||
|
||||
nouveau.id = *nbProduits;
|
||||
|
||||
printf("Nom du produit: ");
|
||||
scanf("%s", nouveau.nom);
|
||||
|
||||
printf("Prix unitaire: ");
|
||||
scanf("%f", &nouveau.prix);
|
||||
|
||||
printf("Quantité initiale: ");
|
||||
scanf("%d", &nouveau.quantite);
|
||||
|
||||
inventaire[*nbProduits] = nouveau;
|
||||
nbProduits++;
|
||||
}
|
||||
|
||||
void modifierQuantite(Produit *inventaire, int nbProduits) {
|
||||
int id, nouvelleQuantite;
|
||||
|
||||
printf("ID du produit à modifier: ");
|
||||
scanf("%d", &id);
|
||||
|
||||
printf("Nouvelle quantité: ");
|
||||
scanf("%d", &nouvelleQuantite);
|
||||
|
||||
for(int i = 0; i < nbProduits; i++) {
|
||||
if(inventaire[i].id == id) {
|
||||
inventaire[i].quantite = nouvelleQuantite;
|
||||
printf("Quantité mise à jour!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Produit non trouvé!\n");
|
||||
}
|
||||
|
||||
float calculerValeurTotale(Produit *inventaire, int nbProduits) {
|
||||
float total = 0.0;
|
||||
|
||||
for(int i = 0; i < nbProduits; i++) {
|
||||
total += inventaire[i].prix;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
void rechercherProduit(Produit *inventaire, int nbProduits) {
|
||||
char nom[MAX_NOM];
|
||||
int trouve = 0;
|
||||
|
||||
printf("Nom du produit à rechercher: ");
|
||||
scanf("%s", nom);
|
||||
|
||||
printf("\nRésultats de la recherche pour '%s':\n", nom);
|
||||
|
||||
for(int i = 0; i < nbProduits; i++) {
|
||||
if(inventaire[i].nom == nom) {
|
||||
printf("ID: %d, Nom: %s, Prix: %.2f, Quantité: %d\n",
|
||||
inventaire[i].id,
|
||||
inventaire[i].nom,
|
||||
inventaire[i].prix,
|
||||
inventaire[i].quantite);
|
||||
trouve = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!trouve) {
|
||||
printf("Aucun produit ne correspond à cette recherche.\n");
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user