Vous êtes sur la page 1sur 2

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
struct student {
char nume[20];
int nota;
struct student *urm; };
struct materie {
char mater[20];
struct student *nod;
struct materie *urm; };
struct materie *lista = NULL;
void meniu(void);
void citire(void);
struct materie *adaugare_ord_materie(struct materie *, char *);
struct student *adaugare_ordonata(struct student *, char *, int);
void afisare(struct materie *);
void afisare_sublista(struct student *);
struct materie *cautare(struct materie *, char *);
void cautare_lista(struct materie *, char *);
void cautare_sublista(struct student *, char *);
struct materie *stergere(struct materie *, char *);
struct materie *stergere_sublista(struct materie *, char *);
int main(void)
{meniu();
return 0;
}void meniu(void)
{ int opt, sub_opt, initializat = 0;
char sir[20];
do {
system("CLS");
printf("Meniu.\n");
printf("1.Initializare.\n");
printf("2.Afisare.\n");
printf("3.Cautare dupa materie.\n");
printf("4.Cautare dupa student.\n");
printf("5.Stergere dupa materie.\n");
printf("6.Stergere dupa student.\n");
printf("0.Iesire.\n");
printf("Optiunea dvs: ");
scanf("%d", &opt);
switch(opt)
{case 1: citire(); break;
case 2: afisare(lista); system("Pause"); break;
case 3: printf("Introduceti materia: ");
scanf("%s", sir); cautare_lista(lista, sir);
system("Pause"); break;
case 4: printf("Introduceti studentul: ");
scanf("%s", sir); cautare_sublista(lista->nod, sir);
system("Pause"); break;
case 5: printf("Introduceti materia: ");
scanf("%s", sir); lista = stergere(lista, sir); break;
case 6: printf("Introduceti studentul: ");
scanf("%s", sir); lista = stergere_sublista(lista, sir); break;break;
case 0: break;
default: printf("Optiune invalida!\n"); system("Pause");
}} while(opt != 0); }

void citire(void)
{char nume[20], mat[20];
int nota;
struct materie *p;
FILE *f = fopen("materii.txt", "r");
if(f == NULL) {
printf("Fisierul nu poate fi citit!");
system("Pause");
return;
}while(!feof(f)) {
fscanf(f, "%s %d %s", nume, &nota, mat);
p = cautare(lista, mat);
if(p == NULL) {
lista = adaugare_ord_materie(lista, mat);
p = cautare(lista, mat);
}p->nod = adaugare_ordonata(p->nod, nume, nota);
}}
struct materie *cautare(struct materie *l, char *sir)
{for(; l != NULL && strcmp(l->mater, sir) != 0; l = l->urm);
if(l != NULL && strcmp(l->mater, sir) == 0)
return l;
return NULL;
}struct materie *adaugare_ord_materie(struct materie *l, char *sir)
{struct materie *nou, *actual, *prev;
nou = (struct materie *)malloc(sizeof(struct materie));
strcpy(nou->mater, sir);
nou->nod = NULL;
for(actual = l, prev = NULL; actual != NULL &&
strcmp(actual->mater, sir) < 0;
prev = actual, actual = actual->urm);
nou->urm = actual;
if(prev == NULL)
return nou;
else {
prev->urm = nou;
return l;
}}

struct student *adaugare_ordonata(struct student *l, char *sir, int n)


{
struct student *nou, *actual, *prev;
nou = (struct student *)malloc(sizeof(struct student));
strcpy(nou->nume, sir);
nou->nota = n;
for(actual = l, prev = NULL; actual != NULL && strcmp
(actual->nume, sir) < 0;
prev = actual, actual = actual->urm);
nou->urm = actual;
if(prev == NULL)
return nou;
else {
prev->urm = nou;
return l;
}}

void afisare(struct materie *l)


{for(; l != NULL; l = l->urm) {
printf("%s\n", l->mater);
afisare_sublista(l->nod);
}}
void afisare_sublista(struct student *l)
{for(; l != NULL; l = l->urm)
printf("%s %d\n", l->nume, l->nota);
}void cautare_lista(struct materie *l, char *sir)
{for(; l != NULL && strcmp(l->mater, sir) != 0; l = l->urm);
printf("%s\n", l->mater);
afisare_sublista(l->nod);
}
void cautare_sublista(struct student *l, char *sir)
{for(; l != NULL && strcmp(l->nume, sir) != 0; l = l->urm);
printf("%s %d", l->nume, l->nota);
}

struct materie *stergere(struct materie *l, char *sir)


{struct materie *prev, *actual;
for(actual = l, prev = NULL; actual != NULL && strcmp(actual->mater, sir) != 0;
prev = actual, actual = actual->urm);
if(prev == NULL)
l = l->urm;
else
prev->urm = actual->urm;
return l;
}struct materie *stergere_sublista(struct materie *m, char *sir)
{struct student *prev, *actual;
struct materie *cur;
for(cur = m; m != NULL; m = m->urm) {
for(actual = m->nod, prev = NULL; actual != NULL && strcmp(actual->nume, sir) != 0;
prev = actual, actual = actual->urm);
if(prev == NULL)
m->nod = m->nod->urm;
else
prev->urm = actual->urm;
}return m;
}