Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //ex 1
- struct Medicine{
- char name[31];
- char date[8];
- long long int id;
- float price;
- int quantity;
- };
- int main() {
- FILE *file;
- file = fopen("medicines.bin", "rb");
- if (file == NULL) {
- printf("Error opening file");
- return 1;
- }
- if (fseek(file, 0, SEEK_END) != 0) {
- printf("Error seeking end");
- fclose(file);
- return 1;
- }
- long fileSize = ftell(file);
- if (fileSize == -1) {
- printf("determining the size of the file\n");
- fclose(file);
- return 1;
- }
- rewind(file);
- int n = fileSize / sizeof(struct Medicine);
- struct Medicine* medicines = (struct Medicine*)malloc(n * sizeof(struct Medicine));
- if (medicines == NULL) {
- printf("Error allocating memory\n");
- fclose(file);
- return 1;
- }
- if (fread(medicines, sizeof(struct Medicine), n, file) != n) {
- if(feof(file)) {
- printf("End of file reached unexpectedly\n");
- }
- else if(ferror(file)) {
- printf("Error reading from file\n");
- }
- free(medicines);
- fclose(file);
- return 1;
- }
- fclose(file);
- free(medicines);
- return 0;
- }
- // ex 2
- struct Medicine* expired(struct Medicine* medicines, int n, char* data) {
- int year1, year2, month1, month2;
- // Парсване на зададената референтна дата (MM.ГГГГ)
- year2 = (data[3] - '0') * 1000 + (data[4] - '0') * 100 + (data[5] - '0') * 10 + (data[6] - '0');
- if (data[0] == '0') {
- month2 = data[1] - '0';
- } else {
- month2 = (data[0] - '0') * 10 + (data[1] - '0');
- }
- int counter = 0;
- struct Medicine* expired = NULL;
- for (int i = 0; i < n; i++) {
- // Корекция: четене на месец от medicines[i].date, не от data
- if (medicines[i].date[0] == '0') {
- month1 = medicines[i].date[1] - '0';
- } else {
- month1 = (medicines[i].date[0] - '0') * 10 + (medicines[i].date[1] - '0');
- }
- year1 = (medicines[i].date[3] - '0') * 1000 + (medicines[i].date[4] - '0') * 100 +
- (medicines[i].date[5] - '0') * 10 + (medicines[i].date[6] - '0');
- // Проверка дали лекарството е с изтекъл срок
- if (year1 < year2 || (year1 == year2 && month1 < month2)) {
- struct Medicine* temp = realloc(expired, (counter + 1) * sizeof(struct Medicine)); // 🟢 корекция: realloc върху expired
- if (temp == NULL) {
- printf("Error allocating memory\n");
- free(expired); // 🟢 освободен само expired, не и medicines
- return NULL;
- }
- expired = temp;
- expired[counter++] = medicines[i];
- }
- }
- if (counter == 0) {
- return NULL;
- }
- // Принтиране на намерените лекарства
- for (int i = 0; i < counter; i++) {
- printf("%s - %s\n", expired[i].name, expired[i].date);
- }
- return expired; // 🟢 корекция: вече връща указател към намерените елементи
- }
- //ex 3
- int write(struct Medicine* medicines, int n, float max, float min) {
- FILE* file;
- file = fopen("offer.txt", "w"); // Отваряме файла за запис.
- if (file == NULL) {
- printf("Error opening file");
- return 1;
- }
- int counter = 0; // Брояч за броя на записаните елементи.
- for (int i = 0; i < n; i++) {
- if (medicines[i].price < max && medicines[i].price > min) {
- fprintf(file, "%s\n%s\n%lld\n%.2f\n", medicines[i].name, medicines[i].date, medicines[i].id, medicines[i].price); // Записваме в текстовия файл.
- counter++;
- }
- }
- fclose(file);
- return counter; // Връщаме броя на записаните лекарства.
- }
- void deleteMedicine(struct Medicine* medicines, int n, char name[31], char data[8]) {
- int index = -1;
- for (int i = 0; i < n; i++) {
- if (strcmp(medicines[i].name, name) == 0 && strcmp(medicines[i].date, data) == 0) {
- index = i;
- break;
- }
- }
- if (index == -1) {
- printf("Not found\n"); // Ако не е намерено, извеждаме съобщение.
- }
- else {
- for (int i = index; i < n - 1; i++) {
- medicines[i] = medicines[i + 1];
- }
- struct Medicine* resize = (struct Medicine*)realloc(medicines, (n - 1) * sizeof(struct Medicine));
- if (resize == NULL) {
- printf("Error reallocating memory\n");
- exit(1);
- }
- medicines = resize; // Присвояваме новия размер на масива.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement