- #include <stdio.h>
- #include <stdlib.h>
- struct student
- {
- char nazwisko[30];
- char imie[20];
- int numer;
- double oceny[5];
- double sr;
- };
- void dopisz();
- void drukuj(struct student);
- void drukuj_plik();
- struct student srednia(struct student);
- void popraw();
- void maksimum();
- void usun();
- main()
- {
- int znak;
- while(1)
- {
- printf("\t\t\tMenu\n");
- printf("----------------------------------------------------\n");
- printf("d - dopisz element\t\tu - usun studenta\n");
- printf("w - drukuj plik\n");
- printf("p - popraw\n");
- printf("m - maksymalna srednia\t\tn - minimalna srednia\n");
- printf("c - wyczysc ekran\n");
- printf("q - wyjdz\n");
- printf("----------------------------------------------------\n");
- znak = getchar();
- switch (znak)
- {
- case 'd' : dopisz();
- break;
- case 'u' : usun();
- break;
- case 'w' : drukuj_plik();
- break;
- case 'p' : popraw();
- break;
- case 'm' : maksimum();
- break;
- case 'c' : system("cls");
- break;
- case 'q' : return 0;
- default : printf("Bledny znak!\n");
- }
- fflush(stdin);
- }
- }
- void dopisz()
- {
- FILE *plik;
- struct student add;
- int i;
- double o;
- plik = fopen("dane.bin","ab");
- if (plik == NULL) printf("Plik nie istnieje\n");
- else
- {
- printf("\nPodaj nazwisko\n"); fflush(stdin); scanf("%29s", add.nazwisko);
- printf("Podaj imie\n"); fflush(stdin); scanf("%19s", add.imie);
- printf("Podaj numer\n"); fflush(stdin); scanf("%d", &add.numer);
- printf("Podaj oceny [5]\n");
- for(i=0; i<5; i++)
- {
- printf("Ocena [%d]\t\t", (i+1));
- fflush(stdin);
- scanf("%lg", &o);
- if (o<7 && o>0) add.oceny[i] = o;
- else
- {
- printf("Uwaga: Przekroczony zakres!\n");
- i--;
- }
- }
- add=srednia(add);
- fwrite(&add, sizeof(struct student), 1, plik);
- fclose(plik);
- }
- }
- void drukuj(struct student print)
- {
- int j;
- printf("Nazwisko:\t%s\t", print.nazwisko);
- printf("Numer:\t\t%d\n", print.numer);
- printf("Imie:\t\t%s\t", print.imie);
- printf("Oceny:\t\t");
- for (j=0; j<5; j++) printf("%g, ", print.oceny[j]);
- printf("\nSrednia:\t%g\n",print.sr);
- printf("\n");
- }
- void drukuj_plik()
- {
- int i=1;
- struct student druk;
- FILE *plik;
- plik = fopen("dane.bin", "rb");
- if (plik == NULL) printf("Plik nie istnieje\n");
- else
- {
- while(fread(&druk,sizeof(struct student),1,plik)!=0)
- {
- drukuj(druk);
- i++;
- }
- fclose(plik);
- }
- }
- struct student srednia(struct student sred)
- {
- int i;
- double s=0;
- for(i=0; i<5; i++) s+=sred.oceny[i];
- sred.sr=s/5;
- return sred;
- }
- void popraw()
- {
- char im[20], nazw[30];
- int i,num;
- struct student spr;
- FILE *plik;
- FILE *tmp;
- printf("\nPodaj nazwisko ucznia:\t"); fflush(stdin); scanf("%29[^\n]s", nazw);
- printf("Podaj imie ucznia:\t"); fflush(stdin); scanf("%19[^\n]s", im);
- printf("Podaj numer ucznia:\t"); fflush(stdin); scanf("%d", &num);
- tmp = fopen("tmp.bin", "wb");
- plik = fopen("dane.bin", "r+b");
- while(fread(&spr, sizeof(struct student), 1, plik)!=0)
- {
- if (!strcmp(nazw, spr.nazwisko) && !strcmp(im, spr.imie) && num == spr.numer)
- {
- printf("\nSzukany student:\n");
- drukuj(spr);
- printf("Podaj nowe dane:");
- printf("\nPodaj nazwisko\t"); fflush(stdin); scanf("%29s", spr.nazwisko);
- printf("Podaj imie\t"); fflush(stdin); scanf("%19s", spr.imie);
- printf("Podaj numer\t"); fflush(stdin); scanf("%d", &spr.numer);
- printf("Podaj oceny [5]\n");
- for(i=0; i<5; i++)
- {
- printf("Ocena %d\t\t", (i+1));
- fflush(stdin);
- scanf("%lg", &spr.oceny[i]);
- }
- spr=srednia(spr);
- fwrite(&spr, sizeof(struct student), 1, tmp);
- continue;
- }
- else
- {
- fwrite(&spr, sizeof(struct student), 1, tmp);
- }
- }
- fclose(plik);
- remove("dane.bin");
- fclose(tmp);
- rename("tmp.bin", "dane.bin");
- }
- void usun()
- {
- char im[20], nazw[30];
- int num;
- struct student us;
- FILE *plik;
- FILE *tmp;
- printf("\nPodaj nazwisko ucznia:\t"); fflush(stdin); scanf("%29[^\n]s", nazw);
- printf("Podaj imie ucznia:\t"); fflush(stdin); scanf("%19[^\n]s", im);
- printf("Podaj numer ucznia:\t"); fflush(stdin); scanf("%d", &num);
- tmp = fopen("tmp.bin", "wb");
- plik = fopen("dane.bin", "r+b");
- while(fread(&us, sizeof(struct student), 1, plik)!=0)
- {
- if (!strcmp(nazw, us.nazwisko) && !strcmp(im, us.imie) && num == us.numer)
- {
- printf("\nUsuniety student:\n");
- drukuj(us);
- continue;
- }
- else
- {
- fwrite(&us, sizeof(struct student), 1, tmp);
- }
- }
- fclose(plik);
- remove("dane.bin");
- fclose(tmp);
- rename("tmp.bin", "dane.bin");
- }
- void maksimum()
- {
- struct student stmp;
- double max=0;
- FILE *plik;
- plik = fopen("dane.bin", "r");
- while(fread(&stmp, sizeof(struct student), 1, plik)!=0)
- {
- if (stmp.sr > max) max = stmp.sr;
- }
- fclose(plik);
- printf("%lg\n", max);
- }
- void minimum()
- {
- struct student stmp;
- double min=10;
- FILE *plik;
- plik = fopen("dane.bin", "r");
- while(fread(&stmp, sizeof(struct student), 1, plik)!=0)
- {
- if (stmp.sr < min) min = stmp.sr;
- }
- fclose(plik);
- printf("%lg\n", min);
- }