Advertisement
Miha_Ch

Medicine

May 11th, 2025
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. //ex 1
  6. struct Medicine{
  7.   char name[31];
  8.   char date[8];
  9.   long long int id;
  10.   float price;
  11.   int quantity;
  12. };
  13.  
  14. int main() {
  15.     FILE *file;
  16.     file = fopen("medicines.bin", "rb");
  17.     if (file == NULL) {
  18.       printf("Error opening file");
  19.       return 1;
  20.     }
  21.  
  22.     if (fseek(file, 0, SEEK_END) != 0) {
  23.       printf("Error seeking end");
  24.       fclose(file);
  25.       return 1;
  26.     }
  27.     long fileSize = ftell(file);
  28.     if (fileSize == -1) {
  29.       printf("determining the size of the file\n");
  30.       fclose(file);
  31.       return 1;
  32.     }
  33.  
  34.     rewind(file);
  35.     int n = fileSize / sizeof(struct Medicine);
  36.  
  37.     struct Medicine* medicines = (struct Medicine*)malloc(n * sizeof(struct Medicine));
  38.     if (medicines == NULL) {
  39.       printf("Error allocating memory\n");
  40.       fclose(file);
  41.       return 1;
  42.     }
  43.  
  44.     if (fread(medicines, sizeof(struct Medicine), n, file) != n) {
  45.       if(feof(file)) {
  46.         printf("End of file reached unexpectedly\n");
  47.       }
  48.       else if(ferror(file)) {
  49.         printf("Error reading from file\n");
  50.       }
  51.       free(medicines);
  52.       fclose(file);
  53.       return 1;
  54.     }
  55.     fclose(file);
  56.     free(medicines);
  57.  
  58.     return 0;
  59. }
  60.  
  61. // ex 2
  62. struct Medicine* expired(struct Medicine* medicines, int n, char* data) {
  63.   int year1, year2, month1, month2;
  64.  
  65.   // Парсване на зададената референтна дата (MM.ГГГГ)
  66.   year2 = (data[3] - '0') * 1000 + (data[4] - '0') * 100 + (data[5] - '0') * 10 + (data[6] - '0');
  67.   if (data[0] == '0') {
  68.     month2 = data[1] - '0';
  69.   } else {
  70.     month2 = (data[0] - '0') * 10 + (data[1] - '0');
  71.   }
  72.  
  73.   int counter = 0;
  74.   struct Medicine* expired = NULL;
  75.  
  76.   for (int i = 0; i < n; i++) {
  77.     // Корекция: четене на месец от medicines[i].date, не от data
  78.     if (medicines[i].date[0] == '0') {
  79.       month1 = medicines[i].date[1] - '0';
  80.     } else {
  81.       month1 = (medicines[i].date[0] - '0') * 10 + (medicines[i].date[1] - '0');
  82.     }
  83.  
  84.     year1 = (medicines[i].date[3] - '0') * 1000 + (medicines[i].date[4] - '0') * 100 +
  85.             (medicines[i].date[5] - '0') * 10 + (medicines[i].date[6] - '0');
  86.  
  87.     // Проверка дали лекарството е с изтекъл срок
  88.     if (year1 < year2 || (year1 == year2 && month1 < month2)) {
  89.       struct Medicine* temp = realloc(expired, (counter + 1) * sizeof(struct Medicine)); // 🟢 корекция: realloc върху expired
  90.       if (temp == NULL) {
  91.         printf("Error allocating memory\n");
  92.         free(expired); // 🟢 освободен само expired, не и medicines
  93.         return NULL;
  94.       }
  95.       expired = temp;
  96.       expired[counter++] = medicines[i];
  97.     }
  98.   }
  99.  
  100.   if (counter == 0) {
  101.     return NULL;
  102.   }
  103.  
  104.   // Принтиране на намерените лекарства
  105.   for (int i = 0; i < counter; i++) {
  106.     printf("%s - %s\n", expired[i].name, expired[i].date);
  107.   }
  108.  
  109.   return expired; // 🟢 корекция: вече връща указател към намерените елементи
  110. }
  111.  
  112. //ex 3
  113. int write(struct Medicine* medicines, int n, float max, float min) {
  114.   FILE* file;
  115.   file = fopen("offer.txt", "w");  // Отваряме файла за запис.
  116.   if (file == NULL) {
  117.     printf("Error opening file");
  118.     return 1;
  119.   }
  120.  
  121.   int counter = 0;  // Брояч за броя на записаните елементи.
  122.  
  123.   for (int i = 0; i < n; i++) {
  124.     if (medicines[i].price < max && medicines[i].price > min) {
  125.       fprintf(file, "%s\n%s\n%lld\n%.2f\n", medicines[i].name, medicines[i].date, medicines[i].id, medicines[i].price);  // Записваме в текстовия файл.
  126.       counter++;
  127.     }
  128.   }
  129.  
  130.   fclose(file);
  131.   return counter;  // Връщаме броя на записаните лекарства.
  132. }
  133.  
  134. void deleteMedicine(struct Medicine* medicines, int n, char name[31], char data[8]) {
  135.   int index = -1;
  136.  
  137.   for (int i = 0; i < n; i++) {
  138.     if (strcmp(medicines[i].name, name) == 0 && strcmp(medicines[i].date, data) == 0) {
  139.       index = i;
  140.       break;
  141.     }
  142.   }
  143.  
  144.   if (index == -1) {
  145.     printf("Not found\n");  // Ако не е намерено, извеждаме съобщение.
  146.   }
  147.   else {
  148.     for (int i = index; i < n - 1; i++) {
  149.       medicines[i] = medicines[i + 1];
  150.     }
  151.  
  152.     struct Medicine* resize = (struct Medicine*)realloc(medicines, (n - 1) * sizeof(struct Medicine));
  153.     if (resize == NULL) {
  154.       printf("Error reallocating memory\n");
  155.       exit(1);
  156.     }
  157.  
  158.     medicines = resize;  // Присвояваме новия размер на масива.
  159.   }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement