Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. #ifdef _WIN32
  6. #define CLEAR "cls"
  7. #else // Negli altri OS
  8. #define CLEAR "clear"
  9. #endif
  10.  
  11. #define CLEAR_BUFFER do { c = getchar(); } while (c != '\n' && c != EOF); // Macro per pulire il buffer in input
  12. #define MAX 100 // Definisco il numero massimo di caratteri per il nome, cognome e indirizzo
  13. #define TRUE 1
  14. #define FALSE 0
  15.  
  16. typedef struct {
  17.     int day;
  18.     int month;
  19.     int year;
  20. } DATE_T;
  21.  
  22. typedef struct {
  23.     int code;
  24.     char *name;
  25.     char *surname;
  26.     char *address;
  27.     DATE_T date;
  28.     int *exams;
  29. } STUDENT_T;
  30.  
  31. STUDENT_T *student;
  32. char courses[][15] = {"Programmazione", "Inglese", "Analisi"}; // SISTEMARE POI
  33. int myIndex = 0; // Contatore che conta il numero degli studenti
  34.  
  35. int checkValue(int value, int check, int min, int max);
  36. void checkAllocation(STUDENT_T *student);
  37. void menu();
  38. void add(STUDENT_T *student);
  39. void printDatabase(STUDENT_T *student);
  40. char* readString();
  41. void exitmenu();
  42.  
  43.  
  44. int main()
  45. {
  46.  
  47.     student = (STUDENT_T*) malloc(sizeof(STUDENT_T));
  48.     checkAllocation(student);
  49.  
  50.     menu();
  51.  
  52.     free(student);
  53.  
  54.     return 0;
  55. }
  56.  
  57. void menu()
  58. {
  59.     int menu, check;
  60.  
  61.     system(CLEAR);
  62.  
  63.     printf("\t\t ---- MENU GESTIONE SEGRETERIA ---- \n\n");
  64.     printf("1 - Inserisci un nuovo studente\n");
  65.     printf("2 - Stampa il registro\n");
  66.     printf("3 - Elimina uno studente\n");
  67.     printf("4 - Ricerca uno studente\n");
  68.     printf("5 - Ricerca studenti per esami da sostenere\n");
  69.     printf("6 - Esci dal programma\n\n");
  70.     printf("Scegli un opzione dal menu: ");
  71.     check = scanf("%d", &menu);
  72.     menu = checkValue(menu, check, 1, 6);
  73.  
  74.     switch (menu) {
  75.     case 1 :
  76.         add(student);
  77.         break;
  78.     case 2 :
  79.         printDatabase(student);
  80.         break;
  81.     case 3 :
  82.         cancel(student);
  83.         break;
  84.     case 4 :
  85.         search(student);
  86.         break;
  87.     case 5 :
  88.         searchSubject(student);
  89.         break;
  90.     case 6 :
  91.         exit(EXIT_SUCCESS);
  92.     default :
  93.         printf("Errore, scelta non valida");
  94.         exit(EXIT_FAILURE);
  95.     }
  96.  
  97.     return;
  98. }
  99.  
  100.  
  101. int checkValue(int value, int check, int min, int max)
  102. {
  103.     int c;
  104.  
  105.     CLEAR_BUFFER
  106.     while (value < min || value > max || check != 1) {
  107.         printf("Errore, devi inserire un valore valido compreso tra %d e %d: ", min, max);
  108.         check = scanf("%d", &value);
  109.         CLEAR_BUFFER
  110.     }
  111.  
  112.     return value;
  113. }
  114.  
  115. void checkAllocation(STUDENT_T *student)
  116. {
  117.     if (student == NULL) {
  118.         printf("Errore durante l'allocazione dinamica della memoria");
  119.         exit(EXIT_FAILURE);
  120.     }
  121.  
  122.     return;
  123. }
  124.  
  125. void exitmenu()
  126. {
  127.     int option, check;
  128.  
  129.     printf("\n\nPremi 0 per tornare al main o 1 per uscire: ");
  130.     check = scanf("%d", &option);
  131.     option = checkValue(option, check, 0, 1);
  132.  
  133.     if (option == 0)
  134.         menu();
  135.     else if (option == 1)
  136.         exit(EXIT_SUCCESS);
  137. }
  138.  
  139. void add(STUDENT_T *student)
  140. {
  141.  
  142.     char c;
  143.     int check, j;
  144.     int i = myIndex;
  145.     /*enum subjects {Programmazione, Inglese, Analisi, Algoritmi, Architettura_Elaboratori, Algebra};
  146.     enum subjects courses;*/
  147.     system(CLEAR);
  148.  
  149.     student = (STUDENT_T*) realloc(student, (myIndex + 1) * sizeof(STUDENT_T));
  150.     checkAllocation(student);
  151.  
  152.     printf("I: %d, INDEX = %d", i, myIndex);
  153.     printf("\t\t ---- MENU GESTIONE SEGRETERIA ---- \n\n");
  154.     printf("Inserisci il numero di matricola: ");
  155.     check = scanf("%d", &student[i].code);
  156.     student[i].code = checkValue(student[i].code, check, 0, INT_MAX);
  157.     for (j = 0; j < myIndex; j++)
  158.         if (student[i].code == student[j].code) {
  159.             printf("Il numero di matricola inserito e\' gia presente nel database");
  160.             exitmenu();
  161.         }
  162.  
  163.     printf("Inserisci il nome: ");
  164.     for (j = 0; j < MAX && (c = getchar()) != '\n'; j++) {
  165.         student[i].name = (char*)realloc(student[i].name , (j+1) * sizeof(char));
  166.         if (student[i].name  == NULL) {
  167.             printf("Errore durante l'allocazione dinamica della memoria");
  168.             exit(EXIT_FAILURE);
  169.         }
  170.         student[i].name[j] = c;
  171.     }
  172.     student[i].name[j] = '\0';
  173.  
  174.     // altri input
  175.  
  176.     myIndex++;
  177.  
  178.     exitmenu();
  179.  
  180.     return;
  181.  
  182. }
  183.  
  184. void printDatabase(STUDENT_T *student)
  185. {
  186.     int i, j;
  187.  
  188.     system(CLEAR);
  189.  
  190.     printf("\t\t ---- VISUALIZZA CONTENUTO DATABASE ---- \n\n");
  191.     printf("INDEX: %d\n\n", myIndex);
  192.     if (myIndex == 0)
  193.         printf("Non ci sono elementi da visualizzare nel database");
  194.     else {
  195.         for (i = 0; i < myIndex; i++) {
  196.             printf("INDEX: %d, i: %d\n\n", myIndex, i);
  197.             printf("Numero matricola: %d\n", student[i].code);
  198.             printf("\tNome e Cognome: %s\n", student[i].name/*, student[i].surname*/);
  199.             /*printf("\tIndirizzo: %s\n", student[i].address);
  200.             printf("\tData di nascita: %d/%d/%d\n", student[i].date.day, student[i].date.month, student[i].date.year);
  201.             for (j = 0; j <= 2; j++) {
  202.                 if (student[i].exams[j] != -1)
  203.                     printf("\tVoto di %s: %d\n", courses[j], student[i].exams[j]);
  204.                 else
  205.                     printf("\tEsame di %s non sostenuto\n", courses[j]);
  206.             }*/
  207.  
  208.         }
  209.  
  210.     }
  211.  
  212.     exitmenu();
  213.  
  214.     return;
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement