Advertisement
Miha_Ch

Treatment

May 11th, 2025
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. //ex1
  6. struct Treatment{
  7.   int id;
  8.   char date[11];
  9.   char name[31];
  10.   char diagnosis[51];
  11. };
  12.  
  13. int main() {
  14.   FILE *file;
  15.   file = fopen("history.bin", "rb");
  16.   if (file == NULL) {
  17.     printf("Error opening file\n");
  18.     return 1;
  19.   }
  20.  
  21.   // Взимаме броя на епикризите
  22.   int n;
  23.   if (fread(&n, sizeof(int), 1, file) != 1) {
  24.     printf("Error reading the number of records\n");
  25.     fclose(file);
  26.     return 1;
  27.   }
  28.   if (n < 0) {
  29.     printf("Invalid count\n");
  30.   }
  31.  
  32.   struct Treatment* history = (struct Treatment*) malloc(n * sizeof(struct Treatment));
  33.   if (history == NULL) {
  34.     printf("Error allocating memory\n");
  35.     fclose(file);
  36.     return 1;
  37.   }
  38.  
  39.   if (fread(history, sizeof(struct Treatment), n, file) != n) {
  40.     if (feof(file)) {
  41.       perror("");
  42.     }
  43.     else if(ferror(file)) {
  44.       perror("");
  45.     }
  46.     free(history);
  47.     fclose(file);
  48.     return 1;
  49.   }
  50.   fclose(file);
  51.   free(history);
  52.  
  53.    return 0;
  54. }
  55.  
  56. //ex 2
  57. int count_treatment(struct Treatment* history, int n, char name[50], char diagnosis[51]) {
  58.   int counter = 0;
  59.   for (int i = 0; i < n; i++) {
  60.     if (strcmp(history[i].name, name) == 0 && strcmp(history[i].diagnosis, diagnosis) == 0) {
  61.       counter++;
  62.     }
  63.   }
  64.   return counter;
  65. }
  66.  
  67. // ex 3
  68. struct Treatment* add_new_treatment(struct Treatment* history, int n) {
  69.  
  70.   struct Treatment new_Treatment;
  71.   printf("Въведи ID на епикризата: ");
  72.   scanf("%d", &new_Treatment.id);
  73.  
  74.   printf("Въведи дата (ДД.ММ.ГГГГ): ");
  75.   scanf("%10s", new_Treatment.date);
  76.  
  77.   printf("Въведи име на пациент: ");
  78.   scanf(" %30[^\n]", new_Treatment.name);  // Чете име с интервали
  79.  
  80.   printf("Въведи диагноза: ");
  81.   scanf(" %50[^\n]", new_Treatment.diagnosis);  // Чете диагноза с интервали
  82.  
  83.   // Преоразмеряване на масива
  84.   struct Treatment* resizedHistory = realloc(history, (n + 1) * sizeof(struct Treatment));
  85.   if (resizedHistory == NULL) {
  86.     printf("Грешка при заделяне на памет.\n");
  87.     return NULL;
  88.   }
  89.  
  90.   resizedHistory[n] = new_Treatment;  // Добавяне на новата епикриза
  91.   n++;  // Увеличаваме броя
  92.  
  93.   return resizedHistory;
  94. }
  95.  
  96. int write_text_file(struct Treatment* history, int n, char diagnosis[51]) {
  97.   FILE *file;
  98.   file = fopen("illness.txt", "w");
  99.   if (file == NULL) {
  100.     printf("Error opening file\n");
  101.     return 1;
  102.   }
  103.   int counter = 0;
  104.   for (int i = 0; i < n; i++) {
  105.     if (strcmp(history[i].diagnosis, diagnosis) == 0) {
  106.       fprintf(file, "Болничен престой на %s за лечение на %s: %sг.\n", history[i].name, history[i].diagnosis, history[i].date);
  107.       counter++;
  108.     }
  109.   }
  110.   fclose(file);
  111.   return counter;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement