Advertisement
Zeshin

SLOJNITE ZADACHI

May 14th, 2024 (edited)
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Medicine{
  6.     char name[30];
  7.     long id;
  8.     char year[7];
  9.     float price;
  10. };
  11. //DISCLAIMER: NE E POLZVANO CHATGPT
  12. struct Medicine* fresh(struct Medicine* arr, int size, char* date){
  13.     char* month = strtok(date, "."); // razdelq meseca
  14.     char* year = strtok(month, NULL); // razdelq godinata
  15.     int len = 1; // razmer na din masiv
  16.     int counter = 0; // index za masiva
  17.     struct Medicine* meds =(struct Medicine*)malloc(sizeof(struct Medicine)*len);
  18.     for(int i = 0;i<size;i++){
  19.         char *med_date = meds[i].year;
  20.         char* med_month = strtok(med_date, ".");
  21.         char* med_year = strtok(med_year, NULL);
  22.         if(strcmp(med_year, year) > 0 || (strcmp(med_month, month) >= 0 && strcmp(med_year, year) == 0)){ // proverqvame dali izticha po-kusno ot dadeniq date
  23.             if(counter == len){ // ako index == razmera, yvelichavame razmera
  24.                 len++; // yvelichavame razmera s 1
  25.                 meds = realloc(meds, sizeof(struct Medicine)*len);
  26.            }
  27.             meds[counter] = meds[i]; // initzializirame na index counter
  28.             counter++; // i vdigame indexa s edno
  29.         }
  30.     }
  31.     return meds;
  32. }
  33. //DISCLAIMER: NE E POLZVANO CHATGPT
  34.  
  35. void remove_medicine(struct Medicine* arr,int size, float max, float min){ // raboti na principa na gornata
  36.     int len = 1;
  37.     int index = 0;
  38.     struct Medicine* meds =(struct Medicine*)malloc(sizeof(struct Medicine)*size);
  39.     for(int i=0;i<size;i++){
  40.         if(arr[i].price >= min && arr[i].price <= max){
  41.             if(index == len){
  42.                 len++;
  43.                 meds = realloc(meds, sizeof(struct Medicine)*len);
  44.             }
  45.             meds[index] = arr[i];
  46.             index++;
  47.         }
  48.     }
  49.     arr = meds;
  50. }
  51.  
  52. int main(){
  53.     FILE* file;
  54.     file = fopen("medicine.bin", "rb");
  55.     if(file == NULL){
  56.         exit(1);
  57.     }
  58.     int size = 1;
  59.     int index = 0;
  60.     struct Medicine* medicines =(struct Medicine*)malloc(sizeof(struct Medicine)*size);
  61.     struct Medicine m;
  62.     while(fread(&m, sizeof(struct Medicine), 1, file)){
  63.         if(index == size){
  64.             size++;
  65.             medicines = realloc(medicines, sizeof(struct Medicine)*size);
  66.         }
  67.         medicines[index] = m;
  68.         index++;
  69.     }
  70.     fclose(file);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement