Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //ex1
- struct Treatment{
- int id;
- char date[11];
- char name[31];
- char diagnosis[51];
- };
- int main() {
- FILE *file;
- file = fopen("history.bin", "rb");
- if (file == NULL) {
- printf("Error opening file\n");
- return 1;
- }
- // Взимаме броя на епикризите
- int n;
- if (fread(&n, sizeof(int), 1, file) != 1) {
- printf("Error reading the number of records\n");
- fclose(file);
- return 1;
- }
- if (n < 0) {
- printf("Invalid count\n");
- }
- struct Treatment* history = (struct Treatment*) malloc(n * sizeof(struct Treatment));
- if (history == NULL) {
- printf("Error allocating memory\n");
- fclose(file);
- return 1;
- }
- if (fread(history, sizeof(struct Treatment), n, file) != n) {
- if (feof(file)) {
- perror("");
- }
- else if(ferror(file)) {
- perror("");
- }
- free(history);
- fclose(file);
- return 1;
- }
- fclose(file);
- free(history);
- return 0;
- }
- //ex 2
- int count_treatment(struct Treatment* history, int n, char name[50], char diagnosis[51]) {
- int counter = 0;
- for (int i = 0; i < n; i++) {
- if (strcmp(history[i].name, name) == 0 && strcmp(history[i].diagnosis, diagnosis) == 0) {
- counter++;
- }
- }
- return counter;
- }
- // ex 3
- struct Treatment* add_new_treatment(struct Treatment* history, int n) {
- struct Treatment new_Treatment;
- printf("Въведи ID на епикризата: ");
- scanf("%d", &new_Treatment.id);
- printf("Въведи дата (ДД.ММ.ГГГГ): ");
- scanf("%10s", new_Treatment.date);
- printf("Въведи име на пациент: ");
- scanf(" %30[^\n]", new_Treatment.name); // Чете име с интервали
- printf("Въведи диагноза: ");
- scanf(" %50[^\n]", new_Treatment.diagnosis); // Чете диагноза с интервали
- // Преоразмеряване на масива
- struct Treatment* resizedHistory = realloc(history, (n + 1) * sizeof(struct Treatment));
- if (resizedHistory == NULL) {
- printf("Грешка при заделяне на памет.\n");
- return NULL;
- }
- resizedHistory[n] = new_Treatment; // Добавяне на новата епикриза
- n++; // Увеличаваме броя
- return resizedHistory;
- }
- int write_text_file(struct Treatment* history, int n, char diagnosis[51]) {
- FILE *file;
- file = fopen("illness.txt", "w");
- if (file == NULL) {
- printf("Error opening file\n");
- return 1;
- }
- int counter = 0;
- for (int i = 0; i < n; i++) {
- if (strcmp(history[i].diagnosis, diagnosis) == 0) {
- fprintf(file, "Болничен престой на %s за лечение на %s: %sг.\n", history[i].name, history[i].diagnosis, history[i].date);
- counter++;
- }
- }
- fclose(file);
- return counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement