Advertisement
Miha_Ch

Courses

May 11th, 2025 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. //ex 1
  6. struct Course{
  7.   char name[51];
  8.   char date[11];
  9.   int lectures;
  10.   float price;
  11. };
  12.  
  13. int main() {
  14.     FILE *file;
  15.  
  16.     file = fopen("Course.bin", "rb");
  17.     if (file == NULL) {
  18.       printf("File could not be opened\n");
  19.       return 1;
  20.     }
  21.                             // Четем първо броя на курсовете който е посочен в първия ред на файла
  22.     if(fseek(file, 0, SEEK_END) != 0){               //int n;
  23.       printf("File could not be seeked\n");          //if (fread(&n, sizeof(int), 1, file) != 1) {
  24.       fclose(file);                                  //printf("Could not read number of courses\n");
  25.       return 1;                                      //fclose(file);
  26.     }                                                //return 1;
  27.                                                      //}
  28.     long size = ftell(file);
  29.     if(size < 0){                                    // За side safety: проверка дали броят е положителен
  30.       printf("Error determining to end of file\n");
  31.       fclose(file);                                  //if (n <= 0) {
  32.       return 1;                                      //printf("Invalid number of courses in file\n");
  33.     }                                                //fclose(file);
  34.                                                      //return 1;
  35.     rewind(file);                                    //}
  36.  
  37.     int n = size / sizeof(struct Course);
  38.     ////////////////////////////////////////////////
  39.     struct Course* courses = (struct Course*) malloc(n * sizeof(struct Course));
  40.  
  41.     if (courses == NULL) {
  42.       printf("Memory could not be allocated\n");
  43.       fclose(file);
  44.       return 1;
  45.     }
  46.  
  47.     if(fread(courses, sizeof(struct Course), n, file) != n) {
  48.       if (feof(file)) {
  49.         perror("");        //printf("File could not be read\n");
  50.       }
  51.       else if (ferror(file)) {
  52.         perror("");       //printf("Error reading file\n");
  53.       }
  54.       free(courses);
  55.       fclose(file);
  56.       return 1;
  57.     }
  58.     fclose(file);
  59.     free(courses);
  60.  
  61.     return 0;
  62. }
  63.  
  64. //ex 2
  65.  
  66. int discount(struct Course* courses, int n, int index){
  67.  
  68.   if (index < 0 || index >= n) {
  69.     printf("Invalid index\n");
  70.     return 1;
  71.   }
  72.  
  73.   courses[index].price *= 0.9;
  74.  
  75.   printf("%.2flv - %s - %s\n", courses[index].price, courses[index].name, courses[index].date);
  76.  
  77.   return 0;
  78. }
  79.  
  80. //ex 3
  81. int write (struct Course* courses, int n, float max, float min){
  82.  
  83.   FILE *file;
  84.   file = fopen("offer.txt", "w");
  85.   if (file == NULL) {
  86.     printf("File could not be opened\n");
  87.     return 1;
  88.   }
  89.  
  90.   int counter = 0;
  91.   for (int i = 0; i < n; i++) {
  92.     if (courses[i].price <= max && courses[i].price >= min) {
  93.       counter++;
  94.       fprintf("%s\n%s\n%d\n%.2flv", courses[i].name, courses[i].date, courses[i].lectures, courses[i].price);
  95.     }
  96.   }
  97.   fclose(file);
  98.   return counter;
  99. }
  100.  
  101. //ex4
  102. struct Course* deleteCourse(struct Course* courses, int* n, const char name[51], const char date[11]) {
  103.  
  104.   int index = -1;
  105.  
  106.   for (int i = 0; i < *n; i++) {
  107.     if (strcmp(courses[i].name, name) == 0 && strcmp(courses[i].date, date) == 0) {
  108.       index = i;
  109.       break;
  110.     }
  111.   }
  112.  
  113.   if (index == -1) {
  114.     printf("Course not found.\n");
  115.     return courses;  // няма нужда от промяна
  116.   }
  117.  
  118.   // изместване на елементите наляво
  119.   for (int i = index; i < *n - 1; i++) {
  120.     courses[i] = courses[i + 1];
  121.   }
  122.  
  123.   // преоразмеряване на масива
  124.   struct Course* resized = realloc(courses, (*n - 1) * sizeof(struct Course));
  125.   if (resized == NULL && *n - 1 > 0) {
  126.     perror("");
  127.     return NULL;
  128.   }
  129.  
  130.   (*n)--;  // намаляваме броя на елементите
  131.   return resized;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement