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>
- struct student
- {
- char imie[20];
- char nazwisko[20];
- int numer;
- double oceny[5];
- double srednia;
- };
- void dopisz(char *nazwa);
- double srednia(struct student s);
- double minimum(char *nazwa);
- int usun(char *nazwa);
- void wydruk(char *nazwa);
- void popraw(char *nazwa);
- main()
- {
- char z;
- char nazwa[30];
- printf("Podaj nazwe pliku na ktorym bedziemy dzis pracowac :)\n");
- fflush(stdin);
- scanf("%s", &nazwa);
- while (1)
- {
- printf("Co chcesz zrobic?\nd-dopisz\nu-usun\np-popraw\nq-wyjscie\nw-wydruk\n");
- fflush(stdin);
- z = getchar();
- switch (z)
- {
- case 'd':
- dopisz(nazwa);
- break;
- case 'q':
- return 0;
- case 'u':
- usun(nazwa);
- break;
- case 'w':
- wydruk(nazwa);
- break;
- case 'p':
- popraw(nazwa);
- break;
- }
- }
- system("pause");
- }
- void dopisz(char *nazwa)
- {
- int i;
- FILE *p;
- struct student s;
- p = fopen(nazwa, "a");
- if (p == 0)
- {
- printf("Blad otwarcia pliku. Program zakonczy dzialanie \n");
- exit(0);
- }
- printf("Podaj imie studenta\n");
- fflush(stdin);
- gets(s.imie);
- printf("Podaj nazwisko studenta \n");
- fflush(stdin);
- gets(s.nazwisko);
- printf("Podaj numer studenta \n");
- fflush(stdin);
- scanf("%d", &s.numer);
- for (i = 0; i<5; i++)
- {
- printf("Podaj %d ocene studenta: ", i + 1);
- fflush(stdin);
- scanf("%lf", &s.oceny[i]);
- }
- s.srednia = srednia(s);
- printf("\n");
- fwrite(&s, sizeof(struct student), 1, p);
- fclose(p);
- }
- double srednia(struct student s)
- {
- int i;
- double suma = 0;
- for (i = 0; i<5; i++)
- {
- suma += s.oceny[i];
- }
- return ((double)suma / 5);
- }
- double minimum(char *nazwa)
- {
- double mini;
- struct student s;
- FILE *p;
- p = fopen(nazwa, "r");
- if (!p)
- {
- printf("Blad otwarcia pliku. Program konczy dzialanie \n");
- exit(0);
- }
- fread(&s, sizeof(s), 1, p);
- mini = s.srednia;
- while (fread(&s, sizeof(struct student), 1, p) == 1)
- {
- if (s.srednia<mini) mini = s.srednia;
- }
- fclose(p);
- return mini;
- }
- int usun(char *nazwa)
- {
- FILE *p1, *p2;
- struct student s;
- double min = minimum(nazwa);
- int ile = 0;
- p1 = fopen(nazwa, "r");
- if (!p1)
- {
- printf("Blad otwarcia pliku. Zamykam program \n");
- exit(0);
- }
- p2 = fopen("temp.txt", "w");
- if (!p2)
- {
- printf("Blad otwarcia pliku temp.txt. Zamykam program. \n");
- exit(0);
- }
- while (fread(&s, sizeof(struct student), 1, p1) == 1)
- {
- if (s.srednia>min)
- {
- fwrite(&s, sizeof(struct student), 1, p2);
- }
- else
- {
- ile++;
- }
- }
- fclose(p1);
- fclose(p2);
- remove(nazwa);
- rename("temp.txt", nazwa);
- return ile;
- }
- void wydruk(char *nazwa)
- {
- struct student s;
- int i;
- FILE *p;
- p = fopen(nazwa, "r");
- while (fread(&s, sizeof(s), 1, p))
- {
- printf("Numer studenta: %d\n", s.numer);
- printf("Imie studenta %s\n", s.imie);
- printf("Nazwisko studenta: %s\n", s.nazwisko);
- printf("Oceny: \n");
- for (i = 0; i<5; i++)
- {
- printf("%.1lf\n", s.oceny[i]);
- }
- printf("Srednia z powyzszych ocen studenta: %.1lf ", s.srednia);
- }
- fclose(p);
- }
- void popraw(char *nazwa)
- {
- struct student s;
- int nr;
- int l = 0;
- FILE *p;
- char z;
- int i, jest = 0;
- printf("Podaj nr studenta ktorego dane chcesz zmienic \n");
- scanf("%d", &nr);
- p = fopen(nazwa, "r+");
- while ((fread(&s, sizeof(s), 1, p) == 1))
- {
- if (s.numer == nr)
- {
- jest = 1;
- break;
- }
- else l++;
- }
- if (jest)
- {
- printf("n -zmien nazwisko\ni- zmien imie\no-zmien oceny\n inny klawisz - exit \n\n");
- fflush(stdin);
- z = getchar();
- switch (z)
- {
- case 'i':
- printf("Podaj imie\n");
- fflush(stdin);
- gets(s.imie);
- break;
- case 'n':
- printf("Podaj nazwisko \n");
- fflush(stdin);
- gets(s.nazwisko);
- break;
- case 'o':
- printf("Podaj 5 ocen studenta \n");
- for (i = 0; i<5; i++)
- {
- printf("%d = \n", i);
- fflush(stdin);
- scanf("%lf", &s.oceny[i]);
- }
- break;
- default: break;
- }
- fseek(p, l - 1, SEEK_SET);
- fwrite(&s, sizeof(struct student), 1, p);
- fclose(p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement