Daste-was-me

Untitled

Apr 26th, 2022
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 KB | None | 0 0
  1. Save New Duplicate & Edit Just Text1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. #ifndef INC_15_FUNCTIONS_H
  117. #define INC_15_FUNCTIONS_H
  118.  
  119. #define DEFINE_ARRAY(TYPE) \
  120.     struct array_##TYPE##_t{    \
  121.         int size;           \
  122.         int capacity;       \
  123.         TYPE *data;\
  124.     };                            \
  125.  
  126.  
  127. #define CREATE_ARRAY(TYPE) \
  128.     struct array_##TYPE##_t *create_array_##TYPE(int size){ \
  129.         if(size <= 0){      \
  130.             return NULL;                    \
  131.         }                  \
  132.                          \
  133.         struct array_##TYPE##_t *struktura = calloc(sizeof(struct array_##TYPE##_t), 1); \
  134.         if(struktura == NULL){                        \
  135.             return NULL;\
  136.         }                   \
  137.         struktura->data = calloc(sizeof(TYPE), size); \
  138.         if(struktura->data == NULL){                  \
  139.             free(struktura);\
  140.             return NULL;\
  141.         }                   \
  142.                             \
  143.         struktura->size = 0;\
  144.         struktura->capacity = size;\
  145.                             \
  146.         return struktura;\
  147.                                 \
  148.     }                            \
  149.  
  150.  
  151. #define FREE_ARRAY(TYPE)\
  152.     void free_array_##TYPE(struct array_##TYPE##_t *array){ \
  153.         if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){     \
  154.             return ;                  \
  155.         }                 \
  156.         free(array->data);\
  157.         free(array);\
  158.         \
  159.                           \
  160.     }                          \
  161.  
  162.  
  163. #define SAVE_ARRAY(TYPE)\
  164.     int save_array_##TYPE(const struct array_##TYPE##_t *array, const char *filename){ \
  165.          if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){ \
  166.             return 1;                 \
  167.          }                \
  168.                           \
  169.          FILE *Fptr = fopen(filename, "wb");                                   \
  170.          if(Fptr == NULL){ \
  171.             return 2;     \
  172.             }             \
  173.          fwrite(&array->size, sizeof(int), 1, Fptr);                              \
  174.          fwrite(&array->data, sizeof(TYPE), array->size, Fptr);                   \
  175.          fclose(Fptr);    \
  176.          return 0;\
  177.     }                              \
  178.  
  179.  
  180. #define LOAD_ARRAY(TYPE) \
  181.     int load_array_##TYPE(struct array_##TYPE##_t **array, const char *filename){ \
  182.         if(array == NULL){\
  183.             return 1;                  \
  184.         }                 \
  185.         FILE *Fptr = fopen("filename", "rb");                               \
  186.         if(Fptr == NULL){ \
  187.             return 2;                  \
  188.         }                 \
  189.         int size;         \
  190.         if(fread(&size, sizeof(int), 1, Fptr) != 1){                        \
  191.             fclose(Fptr); \
  192.             return 3;\
  193.         };                                 \
  194.         if(size <= 0){    \
  195.             fclose(Fptr); \
  196.             return 3;\
  197.         }                \
  198.         *array = create_array_##TYPE(size);                                        \
  199.         if(*array == NULL){\
  200.             fclose(Fptr); \
  201.             return 4;\
  202.         }                \
  203.         if(fread(array, sizeof(TYPE), size, Fptr) != (size_t)size){                 \
  204.             fclose(Fptr);\
  205.             free_array_##TYPE(*array);\
  206.         }                \
  207.         return 0;\
  208.         \
  209.                               \
  210.     }                          \
  211.  
  212.  
  213. #define SORT_ARRAY(TYPE) \
  214.     int sort_array_##TYPE(struct array_##TYPE##_t *array){ \
  215.         if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){ \
  216.             return 1;                 \
  217.          }               \
  218.          for(int i = array->size-1; i>=0; i--){      \
  219.             for(int j = 1; j>=i; j++){               \
  220.                 if(array->data+j < array->data+j-1){  \
  221.                     int temp = *array->data+j;\
  222.                     array->data = array->data+j-1;\
  223.                     *array->data = temp;\
  224.                 }             \
  225.             }                \
  226.          }               \
  227.          return 0;\
  228.          \
  229.     }
  230. #endif //INC_15_FUNCTIONS_H
Advertisement
Add Comment
Please, Sign In to add comment