Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. /* ================================== CAR ====================================== */
  6.  
  7. struct Car {
  8.     const char *brand;
  9.     const char *model;
  10. } Car;
  11.  
  12. struct Car *car_new(const char *brand, const char *model) {
  13.     struct Car *car = malloc(sizeof(Car));
  14.     if(!car) {
  15.         exit(EXIT_FAILURE);
  16.     }
  17.     car->brand = brand;
  18.     car->model = model;
  19.     return car;
  20. }
  21.  
  22. /* ============================================================================= */
  23.  
  24.  
  25. /* ================================== CATALOG ================================== */
  26.  
  27. #define MAX_CATALOG_SIZE 3
  28.  
  29. struct Catalog {
  30.     int number_of_contained_cars;
  31.     struct Car *cars[];
  32. } Catalog;
  33.  
  34. struct Catalog *catalog_new() {
  35.     struct Catalog *catalog = malloc(sizeof(Catalog));
  36.     for(int i=0; i<MAX_CATALOG_SIZE; i++) {
  37.         // Qui alloco la memoria per le celle dell'array
  38.         catalog->cars[i] = malloc(sizeof(Car));
  39.     }
  40.     return catalog;
  41. }
  42.  
  43. bool catalog_is_full(struct Catalog catalog) {
  44.     return catalog.number_of_contained_cars == MAX_CATALOG_SIZE;
  45. }
  46.  
  47. void catalog_add(const struct Car *car, struct Catalog *catalog) {
  48.     if(catalog_is_full(*catalog)) {
  49.         printf("Catalog full");
  50.         exit(EXIT_FAILURE);
  51.     }
  52.     *catalog->cars[catalog->number_of_contained_cars] = *car;
  53.     catalog->number_of_contained_cars++;
  54. }
  55.  
  56. void catalog_display(const struct Catalog *catalog) {
  57.     for (int i=0; i<catalog->number_of_contained_cars; i++) {
  58.         struct Car *car = catalog->cars[i];
  59.         printf("%s | %s\n", car->brand, car->model);
  60.     }
  61. }
  62.  
  63. struct Catalog *catalog_load(char *file_path) {
  64.     // Qui andrebbe la lettura da file, io per fare prima ho creato direttamente il catalogo
  65.     // nel codice
  66.     struct Catalog *catalog = catalog_new();
  67.     struct Car *alfa = car_new("Alfa Romeo", "Giulia");
  68.     struct Car *ferrari = car_new("Ferrari", "Enzo");
  69.     catalog_add(alfa, catalog);
  70.     catalog_add(ferrari, catalog);
  71.     return catalog;
  72. }
  73.  
  74. /* ===================================== MAIN ======================================= */
  75.  
  76.  
  77. int main() {
  78.     struct Catalog *catalog = catalog_load("catalog.txt");
  79.     printf("Catalog loaded.\n");
  80.     int choice = 0;
  81.     do {
  82.         printf("\nWhat do you want to do?\n");
  83.         printf("1- Display catalog\n");
  84.         printf("2- Add car to catalog\n");
  85.         scanf("%d", &choice);
  86.         switch (choice) {
  87.             case 1: catalog_display(catalog); break;
  88.             case 2: {
  89.                 printf("Brand:\n");
  90.                 char *brand = malloc(sizeof(char *));
  91.                 scanf("%s", brand);
  92.                 printf("Model:\n");
  93.                 char *model = malloc(sizeof(char *));
  94.                 scanf("%s", model);
  95.                 struct Car *car = car_new(brand, model);
  96.                 catalog_add(car, catalog);
  97.             } break;
  98.             case 0: exit(EXIT_SUCCESS);
  99.             default: exit(EXIT_SUCCESS);
  100.         }
  101.     } while (choice != 0);
  102.     free(catalog);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement