Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
1
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
	char nume[20];
	int note[3];
	int media;
}student;

int citeste_adauga_student(student **t,int n)
{
	char nume[20];
	int note[3];
	int med = 0;
	*t = (student*)realloc((* t), (n + 1) * sizeof(student));
	if (!(*t))
	{
		return -1;
	}
	else
	{
		(*t + n)->note[0] = (*t + (n-1))->note[0];

      printf("Dati numele: "); scanf("%s", (*t + n-1)->nume);
	  printf("Dati notele: \n");
	  printf("Prima nota: "); scanf("%d", &(*t + (n-1))->note[0]);
	  printf("A doua nota: "); scanf("%d", &(*t + (n - 1))->note[1]);
	  printf("A treia nota: "); scanf("%d", &(*t + (n - 1))->note[2]);
	  med = ((*t + (n - 1))->note[0] + (*t + (n - 1))->note[1] + (*t + (n - 1))->note[2]) / 3;
	  (*t + (n-1))->media = med;
	}
	return n+1;
}

int cmp_media(const void* a, const void* b)
{
	struct student* ia = (struct student*)a;
	struct student* ib = (struct student*)b;
	return ia->media<ib->media;
}

void burse_studenti(student *st, int k)
{
	printf("\nStudentii care primesc bursa sunt:\n");
	for (int i = 0; i<k && (st+i)->note[0]!=-1; i++)
	{

		printf("\n\nNumele: %s\n", (st + i)->nume);
		printf("Prima nota: %d\n", (st + i)->note[0]);
		printf("A doua nota: %d\n", (st + i)->note[1]);
		printf("A treia nota: %d\n", (st + i)->note[2]);
		printf("Media: %d\n", (st + i)->media);
	}
}

int main()
{
	 student *st=NULL;
	int n=1,k=0;
	enum {Iesire,Adaugare,Afisare}op;
	st = (student*)malloc(sizeof(student));
	st->note[0] = -1;

	while (1)
	{
		printf("\n0.Iesire\n");
		printf("1.Adaugati un student\n");
		printf("2.Afisarea elevilor cu bursa\n");
		printf("  Dati o optiune: "); scanf("%d", &op);
		switch (op)
		{
		case Iesire:
			exit(0);
			break;
		case Adaugare:
			n = citeste_adauga_student(&st, n);
			break;
		case Afisare:
			printf("Dati numarul studetilor care vor primi bursa:"); scanf("%d",&k);
			qsort(st, n, sizeof(student), cmp_media);
			burse_studenti(st, k);
			break;
		default:printf("\nOptiunea aleasa nu se afla in meniu!\n");
			break;
		}
		system("pause");
		system("cls");
	}

	return 0;
}