Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.78 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define CHMAX 50
  7. typedef struct materie
  8. {
  9.     char nume_materie[CHMAX];
  10.     int credite;
  11.     float nota;
  12.     struct materie *next;
  13. }materie;
  14.  
  15. typedef struct student
  16. {
  17.     char nume_student[CHMAX];
  18.     materie *sl;
  19.     struct student *next;
  20. }student;
  21.  
  22. student *root = NULL;
  23.  
  24. materie *creareMaterie(char *mat, int credit, float nota)
  25. {
  26.     materie *m = (materie *)malloc(sizeof(materie));
  27.     strcpy(m->nume_materie, mat);
  28.     m->credite = credit;
  29.     m->nota = nota;
  30.     m->next = NULL;
  31.  
  32.     return m;
  33.  
  34. }
  35.  
  36. student *creareStudent(char *nume)
  37. {
  38.     student *s = (student *)malloc(sizeof(student));
  39.     strcpy(s->nume_student, nume);
  40.     s->sl = NULL;
  41.     s->next = NULL;
  42.  
  43.     return s;
  44. }
  45.  
  46. student *existaStudent(char *nume)
  47. {
  48.     student *s = root;
  49.     student *temp = NULL;
  50.  
  51.     while (s != NULL) {
  52.         if (strcmp(s->nume_student, nume) == 0) {
  53.             temp = s;
  54.             return temp;
  55.         }
  56.         s = s->next;
  57.     }
  58.  
  59.     return temp;
  60.  
  61. }
  62.  
  63. materie *adaugaMaterie(materie *lista, materie *x)
  64. {
  65.     materie *q1, *q2;
  66.     for (q1 = q2 = lista; q1 != NULL && q1->nota > x->nota; q2 = q1, q1 = q1->next);
  67.  
  68.     if (q1 == q2) {
  69.         x->next = lista;
  70.         lista = x;
  71.     }
  72.     else {
  73.         q2->next = x;
  74.         x->next = q1;
  75.     }
  76.     return lista;
  77. }
  78.  
  79. student *adaugaStudent(student *lista, student *x, materie *mat)
  80. {
  81.     student *q1, *q2;
  82.     for (q1 = q2 = lista; q1 != NULL &&strcmp(q1->nume_student,x->nume_student)<0 ; q2 = q1, q1 = q1->next);
  83.  
  84.     if (q1 == q2) {
  85.         x->next = lista;
  86.         lista = x;
  87.     }
  88.     else {
  89.         q2->next = x;
  90.         x->next = q1;
  91.     }
  92.     x->sl = mat;
  93.     return lista;
  94. }
  95.  
  96. student *inserareStudent(student *stud, materie *mat)
  97. {
  98.     student *exSTUD;
  99.     if (root == NULL)
  100.     {
  101.         stud->sl = mat;
  102.         root = stud;
  103.         return stud;
  104.     }
  105.     else
  106.     {
  107.         exSTUD = existaStudent(stud->nume_student);
  108.         if (exSTUD != NULL)
  109.         {
  110.             if (exSTUD->sl == NULL)
  111.             {
  112.                 exSTUD->sl = mat;
  113.             }
  114.             else {
  115.                 exSTUD->sl = adaugaMaterie(exSTUD->sl, mat);
  116.             }
  117.             return root;
  118.         }
  119.         else {
  120.             return adaugaStudent(root, stud, mat);
  121.         }
  122.     }
  123. }
  124.  
  125. void citire()
  126. {
  127.     char nume[CHMAX], mat[CHMAX];
  128.     int credit;
  129.     float note;
  130.     student *nodSTUD;
  131.     materie *nodMAT;
  132.     FILE *f = fopen("examen.txt", "rt");
  133.  
  134.     if (!f) {
  135.         fprintf(stderr, "Eroare la deschiderea fisierului\n");
  136.         exit(EXIT_FAILURE);
  137.     }
  138.  
  139.     while (!feof(f)) {
  140.         fscanf(f, "%s %s %d %f", nume, mat, &credit, &note);
  141.  
  142.         nodMAT = creareMaterie(mat, credit, note);
  143.         nodSTUD = creareStudent(nume);
  144.         root = inserareStudent(nodSTUD, nodMAT);
  145.     }
  146.     fclose(f);
  147. }
  148.  
  149. void afisare()
  150. {
  151.     student *student;
  152.     materie *materii;
  153.     int contor = 1;
  154.  
  155.     for (student = root; student != NULL; student = student->next) {
  156.         printf("%d. %s\n", contor++, student->nume_student);
  157.         for (materii = student->sl; materii != NULL; materii = materii->next) {
  158.             printf("\t%s %d %f\n", materii->nume_materie, materii->credite, materii->nota);
  159.         }
  160.         printf("________________________________________\n\n");
  161.     }
  162. }
  163.  
  164. void afisareMaterie(char *nume)
  165. {
  166.     int flag = 0;
  167.     student *stud;
  168.     materie *mat;
  169.    
  170.     for (stud = root; stud != NULL; stud = stud->next) {
  171.         for (mat = stud->sl; mat != NULL; mat = mat->next)
  172.         {
  173.             if (strcmp(mat->nume_materie, nume) == 0)
  174.             {
  175.                 printf("%s %d %f\n",stud->nume_student, mat->credite, mat->nota);
  176.                 flag = 1;
  177.             }
  178.         }
  179.     }
  180.     if (!flag) {
  181.         printf("Nu exista materia cu numele introdus\n ");
  182.     }
  183. }
  184.  
  185. void salvareRestantieri() {
  186.     materie *mat;
  187.     student *stud;
  188.  
  189.     FILE *g;
  190.  
  191.     for (stud = root; stud != NULL; stud = stud->next) {
  192.         for (mat = stud->sl; mat != NULL; mat = mat->next) {
  193.             if (mat->nota < 5)
  194.             {
  195.                 printf("%s %s %d %f\n", stud->nume_student, mat->nume_materie, mat->credite, mat->nota);
  196.             }
  197.         }
  198.     }
  199.     g = fopen("retante.txt", "wt");
  200.  
  201.     if (g)
  202.     {
  203.         for (stud = root; stud != NULL; stud = stud->next) {
  204.             for (mat = stud->sl; mat != NULL; mat = mat->next) {
  205.                 if (mat->nota < 5)
  206.                 {
  207.                     fprintf(g,"%s %s %d %f\n", stud->nume_student, mat->nume_materie, mat->credite, mat->nota);
  208.                 }
  209.             }
  210.         }
  211.  
  212.     }
  213.     fclose(g);
  214. }
  215. int main()
  216. {
  217.     int opt;
  218.     char materie[CHMAX];
  219.     do {
  220.         //printf("\n__________________________________\n\n");
  221.         printf("\n1. Citire din fisier\n");
  222.         printf("2. Afisare date\n");
  223.         printf("3. Afisare date despre o materie\n");
  224.         printf("4. Salvare in fisier 'restante' a studentilor restantieri\n");
  225.         printf("0. Iesire\n");
  226.  
  227.         printf("\nIntroduceti optiunea: ");
  228.         scanf("%d", &opt);
  229.         printf("\n");
  230.  
  231.         switch (opt)
  232.         {
  233.         case 1:
  234.             citire();
  235.             break;
  236.         case 2:
  237.             afisare();
  238.             break;
  239.         case 3:
  240.             printf("Introduceti numele materiei: ");
  241.             scanf("%s", materie);
  242.             afisareMaterie(materie);
  243.             break;
  244.         case 4:
  245.             salvareRestantieri();
  246.             break;
  247.         case 0: exit(0);
  248.             break;
  249.  
  250.         default: printf("Optiune gresita\n");
  251.             break;
  252.  
  253.         }
  254.     } while (opt != 0);
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement