Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 13.2 ZROBIC!!!
- feof(zmienna plikowa)
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct{
- char * towar;
- char * producent;
- float cenaGr;
- } HURT;
- typedef struct{
- char * producent;
- float marza;
- } SKLEP;
- void zapiszSklepy(SKLEP * bazaSklepow, int iloscSklepow)
- {
- int i;
- FILE * file;
- file = fopen("sklepy.txt", "wb");
- fwrite(&iloscSklepow, sizeof(int), 1, file);
- for(i = 0; i<iloscSklepow; i++)
- {
- int length = strlen(bazaSklepow[i].producent);
- fwrite(&length, sizeof(int), 1, file);
- fwrite(&bazaSklepow[i].producent, sizeof(char), length, file);
- fwrite(&bazaSklepow[i].marza, sizeof(float), 1, file);
- }
- fclose(file);
- }
- SKLEP * wczytajSklepy(char * fileName)
- {
- int i;
- FILE * file;
- file = fopen(fileName, "wb");
- int iloscSklepow;
- fread(&iloscSklepow, sizeof(int), 1, file);
- printf("Wczytano %d plikow!", iloscSklepow);
- fclose();
- return NULL;
- }
- SKLEP * minMarza(SKLEP * bazaSklepow, int iloscSklepow)
- {
- int i;
- SKLEP * currentMin = &bazaSklepow[0];
- for(i = 1; i < iloscSklepow; i++)
- if (currentMin->marza > bazaSklepow[i].marza)
- currentMin = &bazaSklepow[i];
- return currentMin;
- }
- SKLEP * znajdzSklep(char * name, SKLEP * bazaSklepow, int iloscSklepow)
- {
- int i;
- for(i = 0; i<iloscSklepow; i++)
- if (strcmp(name, bazaSklepow[i].producent) == 0)
- return bazaSklepow + i;
- return NULL;
- }
- void wyswietl(HURT * towary, int iloscTowarow, SKLEP * sklepy, int iloscSklepow)
- {
- int i;
- for(i = 0; i<iloscTowarow; i++)
- {
- printf("Towar: %s; Producent: %s; Cena: %f + Marza: ",
- towary[i].towar,
- towary[i].producent,
- towary[i].cenaGr);
- SKLEP * sklep = znajdzSklep(towary[i].producent, sklepy, iloscSklepow);
- if (sklep != NULL)
- printf("%f\n", sklep->marza);
- else
- printf("0.0\n");
- }
- }
- int main()
- {
- int N, K, i;
- char buffer[100];
- printf("Podaj ilosc struktur 'HURT': ");
- scanf("%d", &N);
- printf("Podaj ilosc struktur 'SKLEP': ");
- scanf("%d", &K);
- HURT * towary = malloc(sizeof(HURT) * N);
- for(i = 0; i<N; i++)
- {
- getchar();
- printf("Podaj nazwe towaru: ");
- gets(buffer);
- towary[i].towar = malloc(sizeof(char) * (strlen(buffer) + 1));
- strcpy(towary[i].towar, buffer);
- printf("Podaj nazwe producenta: ");
- gets(buffer);
- towary[i].producent = malloc(sizeof(char) * (strlen(buffer) + 1));
- strcpy(towary[i].producent, buffer);
- printf("Podaj cene (float): ");
- float cena;
- scanf("%f", &towary[i].cenaGr);
- }
- SKLEP * sklepy = malloc(sizeof(SKLEP) * K);
- for(i = 0; i<K; i++)
- {
- getchar();
- printf("Podaj nazwe producenta: ");
- gets(buffer);
- sklepy[i].producent = malloc(sizeof(char) * (strlen(buffer) + 1));
- strcpy(sklepy[i].producent, buffer);
- printf("Podaj marze (float): ");
- float cena;
- scanf("%f", &sklepy[i].marza);
- }
- wyswietl(towary, N, sklepy, K);
- printf("Zapisuje...\n");
- zapiszSklepy(sklepy, K);
- printf("Zapisano!");
- printf("Wczytuje...\n");
- wczytajSklepy("sklepy.txt");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement