Sunteți pe pagina 1din 3

#include <stdio.

h>
#include <conio.h>
#include <string.h>
typedef struct{
char nume[50];
char facultate[50];
int grupa;
}student;
void citire(student *st, int *nr)
{
fflush(stdin);
(*nr)++;
printf("Dati numele studentului:
");
gets((st + *nr)->nume);
printf("Dati facultatea:
");
gets((st + *nr)->facultate);
printf("Dati grupa:
");
scanf("%d", &((st + *nr)->grupa));
}
void afisare(student *st, int nr)
{
printf("\n%25s \t %25s \t %5d", (st + nr)->nume, (st + nr)->facultate, s
t[nr].grupa);
}
void cautare_facultate(student *st, int nr, char fac[50])
{
int i;
for (i = 0; i <= nr; i++)
if (strcmp((st + i)->facultate, fac) == 0)
afisare(st, i);
}
void cautare_grupa(student *st, int *nr, int gr)
{
int i;
for (i = 0; i <= (*nr); i++)
if ((st + i)->grupa == gr)
afisare(st, i);
}
void cautare_nume(student *st, int *nr, char nu[50])
{
int i;
for (i = 0; i <= (*nr); i++)
if (strcmp((st + i)->nume, nu) == 0)
afisare(st, i);
}
void stergere(student *st, int *nr, char nu[50])
{
int k = 0, i,j;
for (i = 0; i <= (*nr);i++)
if (strcmp((st + i)->nume, nu) == 0)
{
k++;
for (j = i; j <= (*nr - k); j++)
*(st + j) = st[j + 1];
}
*nr = *nr - k;
}
void sortare(student *st, int *nr)

{
int i, k;
student aux;
do {
k = 1;
for (i = 0; i <= (*nr - 1); i++)
if (strcmp((st + i)->nume, (st + i + 1)->nume) > 0)
{
aux = *(st + i);
*(st + i) = *(st + i + 1);
*(st + i + 1) = aux;
k = 0;
}
} while (!k);
}
int main()
{
student st[50];
int opt, n =-1, i, gr;
char nu[50], fac[50];
do{
printf("\n1.Adaugare Student");
printf("\n2.Afisarea tuturor studentilor ");
printf("\n3.Afisarea studentilor de la o anumita facultate");
printf("\n4.Afisarea studentilor de la o anumita grupa");
printf("\n5.Cautare unui student dupa nume");
printf("\n6.Stergerea unui student dupa nume");
printf("\n7.Sortarea studentilor dupa nume");
printf("\n8.Iesire");
printf("\nOptiunea Dumneavoastra ");
fflush(stdin);// Aveai nevoie de fflush si aici
scanf("%d", &opt);
switch (opt)
{
case 1:
citire(&st[0], &n);
break;
case 2:
printf("\n%25s \t%25s \t%5s \t", "Nume", "Facult
ate", "Grupa");
for (i = 0; i <=n; i++)
afisare(st, i);
break;
case 3:
printf("Dati facultatea dorita: ");
scanf("%s", fac);
printf("\n%25s \t%25s \t%5s\t", "Nume", "Faculta
te", "Grupa");
cautare_facultate(st, n, fac);
break;
case 4:
printf("Dati grupa dorita");
scanf("%d", &gr);
printf("\n%25s \t%25s \t%5s\t", "Nume", "Faculta
te", "Grupa");
cautare_grupa(st, &n, gr);
break;
case 5:

printf("Dati numele dorit");


scanf("%s", nu);
printf("\n%25s \t%25s \t%5s\t", "Nume", "Faculta
te", "Grupa");
cautare_nume(&st[0], &n, nu);
break;
case 6:
printf("Dati numele pe care doriti sa il sterget
i");
scanf("%s", nu);
stergere(&st[0], &n, nu);
break;
case 7:
sortare(&st[0], &n);
printf("\n%25s\t%25s\t%5s\t", "Nume", "Facultate
", "Grupa");
for (i = 0; i <=n; i++)
afisare(&st[0], i);
break;
case 8:
//exit(); -> Oricum iti iasa din program(din con
ditia de la do{}while
printf("Ati iesit din program!\n");
break;
}
} while (opt != 8);
getch();
return 0;
}