Advertisement
STANAANDREY

movies

Jan 10th, 2023
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <inttypes.h>
  6. #include <stdbool.h>
  7. #include <assert.h>
  8. #define CHUNK 128
  9. #define LINE_MAX_SIZE 200
  10. #define MAX_TITLE_LEN 100
  11. #define FILE_PREF "out"
  12. #define FILE_NAME "movies.csv"
  13. #define ISNULL(p) if (p == NULL) return NULL;
  14. #define CONCAT(x, y) (x""y)
  15.  
  16. typedef struct {
  17.     char title[MAX_TITLE_LEN];
  18.     int year;
  19.     uint64_t budget;
  20. } Movie;
  21.  
  22. typedef enum {
  23.     SORT_BY_YEAR = 1, SORT_BY_TITLE, SORT_BY_BUDGET
  24. } SortedByEnum;
  25.  
  26. FILE *safeOpenFile(const char *const path, const char *const mode) {
  27.     FILE *file = NULL;
  28.     file = fopen(path, mode);
  29.     if (file == NULL) {
  30.         perror("opening error!");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     return file;
  34. }
  35.  
  36. void safeCloseFile(FILE *file) {
  37.     if (fclose(file) == EOF) {
  38.         perror(NULL);
  39.     }
  40. }
  41.  
  42. void getValue(char s[], char val[]) {
  43.     static char *p = NULL;
  44.     memset(val, 0, sizeof(char) * MAX_TITLE_LEN);
  45.     if (s != NULL) {
  46.         p = s;
  47.     }
  48.     while (*p != ',') {
  49.         strncat(val, p, 1);
  50.         p++;
  51.     }
  52.     p++;
  53. }
  54.  
  55. Movie processLine(char s[]) {
  56.     static char val[MAX_TITLE_LEN];
  57.     Movie movie = {};
  58.  
  59.     getValue(s, val);
  60.     movie.year = strtoll(val, NULL, 10);
  61.  
  62.     getValue(NULL, val);
  63.  
  64.     getValue(NULL, val);
  65.     strcpy(movie.title, val);
  66.  
  67.     for (int i = 0; i < 3; i++) {
  68.         getValue(NULL, val);
  69.     }
  70.  
  71.     getValue(NULL, val);
  72.     movie.budget = strtoll(val, NULL, 10);
  73.  
  74.     return movie;
  75. }
  76.  
  77. void printMovie(FILE *file, const Movie *const movie) {
  78.     fprintf(file, "{");
  79.     fprintf(file, " title: %s,", movie->title);
  80.     fprintf(file, " year: %d,", movie->year);
  81.     fprintf(file, " budget: %" PRId64, movie->budget);
  82.     fprintf(file, " }\n");
  83. }
  84.  
  85. void printMovieArr(FILE *file, Movie movies[], int n) {
  86.     for (int i = 0; i < n; i++) {
  87.         printMovie(file, &movies[i]);
  88.     }
  89. }
  90.  
  91. Movie *read(int *n) {
  92.     FILE *file = safeOpenFile(FILE_NAME, "r");
  93.     Movie *movies = NULL;
  94.     int index = 0, currSize = 0;
  95.     static char line[LINE_MAX_SIZE];
  96.     fgets(line, LINE_MAX_SIZE, file);
  97.     while (fgets(line, LINE_MAX_SIZE, file)) {
  98.         Movie movie = processLine(line);
  99.         if (index == currSize) {
  100.             currSize += CHUNK;
  101.             movies = (Movie*)realloc(movies, sizeof(Movie) * currSize);
  102.             ISNULL(movies);
  103.         }
  104.         movies[index++] = movie;//*/
  105.     }
  106.  
  107.     movies = (Movie*)realloc(movies, sizeof(Movie) * index);
  108.     ISNULL(movies);
  109.     *n = index;//*/
  110.     safeCloseFile(file);
  111.     return movies;
  112. }
  113.  
  114. void swap(Movie *a, Movie *b) {
  115.     Movie aux = *a;
  116.     *a = *b;
  117.     *b = aux;
  118. }
  119.  
  120. void bubbleSort(Movie movies[], int n, SortedByEnum sortedBy) {
  121.     bool sorted;
  122.     do {
  123.         sorted = true;
  124.         for (int i = 1; i < n; i++) {
  125.             switch(sortedBy) {
  126.                 case SORT_BY_TITLE:
  127.                     if (strcmp(movies[i - 1].title, movies[i].title) > 0) {
  128.                         swap(&movies[i - 1], &movies[i]);
  129.                         sorted = false;
  130.                     }
  131.                     break;
  132.                 case SORT_BY_YEAR:
  133.                     if (movies[i - 1].year > movies[i].year) {
  134.                         swap(&movies[i - 1], &movies[i]);
  135.                         sorted = false;
  136.                     }
  137.                     break;
  138.                 case SORT_BY_BUDGET:
  139.                     if (movies[i - 1].budget > movies[i].budget) {
  140.                         swap(&movies[i - 1], &movies[i]);
  141.                         sorted = false;
  142.                     }
  143.                     break;
  144.                 default:
  145.                     assert(false);
  146.             }
  147.         }
  148.     } while(!sorted);
  149. }
  150.  
  151. int main(void) {
  152.     int n;
  153.     Movie *movies = read(&n);
  154.     if (movies == NULL) {
  155.         fprintf(stderr, "error at realloc!\n");
  156.         exit(-1);
  157.     }
  158.  
  159.     for (int i = 1; i <= 3; i++) {
  160.         char fname[10];
  161.         sprintf(fname, "%s%d.txt", FILE_PREF, i);
  162.         FILE *file = safeOpenFile(fname, "w");
  163.         bubbleSort(movies, n, (SortedByEnum)i);
  164.         printMovieArr(file, movies, n);
  165.         safeCloseFile(file);
  166.     }
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement