Advertisement
ifinox

09.01.2018 PodstawyProgramowania

Jan 9th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. /*
  2.     13.2 ZROBIC!!!
  3.     feof(zmienna plikowa)
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. typedef struct{
  11.     char * towar;
  12.     char * producent;
  13.     float cenaGr;
  14. } HURT;
  15.  
  16. typedef struct{
  17.     char * producent;
  18.     float marza;
  19. } SKLEP;
  20.  
  21. void zapiszSklepy(SKLEP * bazaSklepow, int iloscSklepow)
  22. {
  23.     int i;
  24.     FILE * file;
  25.     file = fopen("sklepy.txt", "wb");
  26.  
  27.     fwrite(&iloscSklepow, sizeof(int), 1, file);
  28.     for(i = 0; i<iloscSklepow; i++)
  29.     {
  30.         int length = strlen(bazaSklepow[i].producent);
  31.  
  32.         fwrite(&length, sizeof(int), 1, file);
  33.         fwrite(&bazaSklepow[i].producent, sizeof(char), length, file);
  34.         fwrite(&bazaSklepow[i].marza, sizeof(float), 1, file);
  35.     }
  36.  
  37.     fclose(file);
  38. }
  39.  
  40. SKLEP * wczytajSklepy(char * fileName)
  41. {
  42.     int i;
  43.     FILE * file;
  44.     file = fopen(fileName, "wb");
  45.  
  46.     int iloscSklepow;
  47.     fread(&iloscSklepow, sizeof(int), 1, file);
  48.     printf("Wczytano %d plikow!", iloscSklepow);
  49.     fclose();
  50.     return NULL;
  51. }
  52.  
  53. SKLEP * minMarza(SKLEP * bazaSklepow, int iloscSklepow)
  54. {
  55.     int i;
  56.     SKLEP * currentMin = &bazaSklepow[0];
  57.     for(i = 1; i < iloscSklepow; i++)
  58.         if (currentMin->marza > bazaSklepow[i].marza)
  59.             currentMin = &bazaSklepow[i];
  60.     return currentMin;
  61. }
  62.  
  63. SKLEP * znajdzSklep(char * name, SKLEP * bazaSklepow, int iloscSklepow)
  64. {
  65.     int i;
  66.     for(i = 0; i<iloscSklepow; i++)
  67.         if (strcmp(name, bazaSklepow[i].producent) == 0)
  68.             return bazaSklepow + i;
  69.     return NULL;
  70. }
  71.  
  72. void wyswietl(HURT * towary, int iloscTowarow, SKLEP * sklepy, int iloscSklepow)
  73. {
  74.     int i;
  75.     for(i = 0; i<iloscTowarow; i++)
  76.     {
  77.         printf("Towar: %s; Producent: %s; Cena: %f + Marza: ",
  78.                towary[i].towar,
  79.                towary[i].producent,
  80.                towary[i].cenaGr);
  81.         SKLEP * sklep = znajdzSklep(towary[i].producent, sklepy, iloscSklepow);
  82.         if (sklep != NULL)
  83.             printf("%f\n", sklep->marza);
  84.         else
  85.             printf("0.0\n");
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     int N, K, i;
  92.     char buffer[100];
  93.  
  94.     printf("Podaj ilosc struktur 'HURT': ");
  95.     scanf("%d", &N);
  96.  
  97.     printf("Podaj ilosc struktur 'SKLEP': ");
  98.     scanf("%d", &K);
  99.  
  100.  
  101.     HURT * towary = malloc(sizeof(HURT) * N);
  102.     for(i = 0; i<N; i++)
  103.     {
  104.         getchar();
  105.  
  106.         printf("Podaj nazwe towaru: ");
  107.         gets(buffer);
  108.         towary[i].towar = malloc(sizeof(char) * (strlen(buffer) + 1));
  109.         strcpy(towary[i].towar, buffer);
  110.  
  111.         printf("Podaj nazwe producenta: ");
  112.         gets(buffer);
  113.         towary[i].producent = malloc(sizeof(char) * (strlen(buffer) + 1));
  114.         strcpy(towary[i].producent, buffer);
  115.  
  116.         printf("Podaj cene (float): ");
  117.         float cena;
  118.         scanf("%f", &towary[i].cenaGr);
  119.     }
  120.  
  121.     SKLEP * sklepy = malloc(sizeof(SKLEP) * K);
  122.  
  123.     for(i = 0; i<K; i++)
  124.     {
  125.         getchar();
  126.  
  127.         printf("Podaj nazwe producenta: ");
  128.         gets(buffer);
  129.         sklepy[i].producent = malloc(sizeof(char) * (strlen(buffer) + 1));
  130.         strcpy(sklepy[i].producent, buffer);
  131.  
  132.         printf("Podaj marze (float): ");
  133.         float cena;
  134.         scanf("%f", &sklepy[i].marza);
  135.     }
  136.  
  137.     wyswietl(towary, N, sklepy, K);
  138.  
  139.     printf("Zapisuje...\n");
  140.     zapiszSklepy(sklepy, K);
  141.     printf("Zapisano!");
  142.  
  143.     printf("Wczytuje...\n");
  144.     wczytajSklepy("sklepy.txt");
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement