Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define CHMAX 50
- typedef struct materie
- {
- char nume_materie[CHMAX];
- int credite;
- float nota;
- struct materie *next;
- }materie;
- typedef struct student
- {
- char nume_student[CHMAX];
- materie *sl;
- struct student *next;
- }student;
- student *root = NULL;
- materie *creareMaterie(char *mat, int credit, float nota)
- {
- materie *m = (materie *)malloc(sizeof(materie));
- strcpy(m->nume_materie, mat);
- m->credite = credit;
- m->nota = nota;
- m->next = NULL;
- return m;
- }
- student *creareStudent(char *nume)
- {
- student *s = (student *)malloc(sizeof(student));
- strcpy(s->nume_student, nume);
- s->sl = NULL;
- s->next = NULL;
- return s;
- }
- student *existaStudent(char *nume)
- {
- student *s = root;
- student *temp = NULL;
- while (s != NULL) {
- if (strcmp(s->nume_student, nume) == 0) {
- temp = s;
- return temp;
- }
- s = s->next;
- }
- return temp;
- }
- materie *adaugaMaterie(materie *lista, materie *x)
- {
- materie *q1, *q2;
- for (q1 = q2 = lista; q1 != NULL && q1->nota > x->nota; q2 = q1, q1 = q1->next);
- if (q1 == q2) {
- x->next = lista;
- lista = x;
- }
- else {
- q2->next = x;
- x->next = q1;
- }
- return lista;
- }
- student *adaugaStudent(student *lista, student *x, materie *mat)
- {
- student *q1, *q2;
- for (q1 = q2 = lista; q1 != NULL &&strcmp(q1->nume_student,x->nume_student)<0 ; q2 = q1, q1 = q1->next);
- if (q1 == q2) {
- x->next = lista;
- lista = x;
- }
- else {
- q2->next = x;
- x->next = q1;
- }
- x->sl = mat;
- return lista;
- }
- student *inserareStudent(student *stud, materie *mat)
- {
- student *exSTUD;
- if (root == NULL)
- {
- stud->sl = mat;
- root = stud;
- return stud;
- }
- else
- {
- exSTUD = existaStudent(stud->nume_student);
- if (exSTUD != NULL)
- {
- if (exSTUD->sl == NULL)
- {
- exSTUD->sl = mat;
- }
- else {
- exSTUD->sl = adaugaMaterie(exSTUD->sl, mat);
- }
- return root;
- }
- else {
- return adaugaStudent(root, stud, mat);
- }
- }
- }
- void citire()
- {
- char nume[CHMAX], mat[CHMAX];
- int credit;
- float note;
- student *nodSTUD;
- materie *nodMAT;
- FILE *f = fopen("examen.txt", "rt");
- if (!f) {
- fprintf(stderr, "Eroare la deschiderea fisierului\n");
- exit(EXIT_FAILURE);
- }
- while (!feof(f)) {
- fscanf(f, "%s %s %d %f", nume, mat, &credit, ¬e);
- nodMAT = creareMaterie(mat, credit, note);
- nodSTUD = creareStudent(nume);
- root = inserareStudent(nodSTUD, nodMAT);
- }
- fclose(f);
- }
- void afisare()
- {
- student *student;
- materie *materii;
- int contor = 1;
- for (student = root; student != NULL; student = student->next) {
- printf("%d. %s\n", contor++, student->nume_student);
- for (materii = student->sl; materii != NULL; materii = materii->next) {
- printf("\t%s %d %f\n", materii->nume_materie, materii->credite, materii->nota);
- }
- printf("________________________________________\n\n");
- }
- }
- void afisareMaterie(char *nume)
- {
- int flag = 0;
- student *stud;
- materie *mat;
- for (stud = root; stud != NULL; stud = stud->next) {
- for (mat = stud->sl; mat != NULL; mat = mat->next)
- {
- if (strcmp(mat->nume_materie, nume) == 0)
- {
- printf("%s %d %f\n",stud->nume_student, mat->credite, mat->nota);
- flag = 1;
- }
- }
- }
- if (!flag) {
- printf("Nu exista materia cu numele introdus\n ");
- }
- }
- void salvareRestantieri() {
- materie *mat;
- student *stud;
- FILE *g;
- for (stud = root; stud != NULL; stud = stud->next) {
- for (mat = stud->sl; mat != NULL; mat = mat->next) {
- if (mat->nota < 5)
- {
- printf("%s %s %d %f\n", stud->nume_student, mat->nume_materie, mat->credite, mat->nota);
- }
- }
- }
- g = fopen("retante.txt", "wt");
- if (g)
- {
- for (stud = root; stud != NULL; stud = stud->next) {
- for (mat = stud->sl; mat != NULL; mat = mat->next) {
- if (mat->nota < 5)
- {
- fprintf(g,"%s %s %d %f\n", stud->nume_student, mat->nume_materie, mat->credite, mat->nota);
- }
- }
- }
- }
- fclose(g);
- }
- int main()
- {
- int opt;
- char materie[CHMAX];
- do {
- //printf("\n__________________________________\n\n");
- printf("\n1. Citire din fisier\n");
- printf("2. Afisare date\n");
- printf("3. Afisare date despre o materie\n");
- printf("4. Salvare in fisier 'restante' a studentilor restantieri\n");
- printf("0. Iesire\n");
- printf("\nIntroduceti optiunea: ");
- scanf("%d", &opt);
- printf("\n");
- switch (opt)
- {
- case 1:
- citire();
- break;
- case 2:
- afisare();
- break;
- case 3:
- printf("Introduceti numele materiei: ");
- scanf("%s", materie);
- afisareMaterie(materie);
- break;
- case 4:
- salvareRestantieri();
- break;
- case 0: exit(0);
- break;
- default: printf("Optiune gresita\n");
- break;
- }
- } while (opt != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement