Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Save New Duplicate & Edit Just Text1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- #ifndef INC_15_FUNCTIONS_H
- #define INC_15_FUNCTIONS_H
- #define DEFINE_ARRAY(TYPE) \
- struct array_##TYPE##_t{ \
- int size; \
- int capacity; \
- TYPE *data;\
- }; \
- #define CREATE_ARRAY(TYPE) \
- struct array_##TYPE##_t *create_array_##TYPE(int size){ \
- if(size <= 0){ \
- return NULL; \
- } \
- \
- struct array_##TYPE##_t *struktura = calloc(sizeof(struct array_##TYPE##_t), 1); \
- if(struktura == NULL){ \
- return NULL;\
- } \
- struktura->data = calloc(sizeof(TYPE), size); \
- if(struktura->data == NULL){ \
- free(struktura);\
- return NULL;\
- } \
- \
- struktura->size = 0;\
- struktura->capacity = size;\
- \
- return struktura;\
- \
- } \
- #define FREE_ARRAY(TYPE)\
- void free_array_##TYPE(struct array_##TYPE##_t *array){ \
- if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){ \
- return ; \
- } \
- free(array->data);\
- free(array);\
- \
- \
- } \
- #define SAVE_ARRAY(TYPE)\
- int save_array_##TYPE(const struct array_##TYPE##_t *array, const char *filename){ \
- if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){ \
- return 1; \
- } \
- \
- FILE *Fptr = fopen(filename, "wb"); \
- if(Fptr == NULL){ \
- return 2; \
- } \
- fwrite(&array->size, sizeof(int), 1, Fptr); \
- fwrite(&array->data, sizeof(TYPE), array->size, Fptr); \
- fclose(Fptr); \
- return 0;\
- } \
- #define LOAD_ARRAY(TYPE) \
- int load_array_##TYPE(struct array_##TYPE##_t **array, const char *filename){ \
- if(array == NULL){\
- return 1; \
- } \
- FILE *Fptr = fopen("filename", "rb"); \
- if(Fptr == NULL){ \
- return 2; \
- } \
- int size; \
- if(fread(&size, sizeof(int), 1, Fptr) != 1){ \
- fclose(Fptr); \
- return 3;\
- }; \
- if(size <= 0){ \
- fclose(Fptr); \
- return 3;\
- } \
- *array = create_array_##TYPE(size); \
- if(*array == NULL){\
- fclose(Fptr); \
- return 4;\
- } \
- if(fread(array, sizeof(TYPE), size, Fptr) != (size_t)size){ \
- fclose(Fptr);\
- free_array_##TYPE(*array);\
- } \
- return 0;\
- \
- \
- } \
- #define SORT_ARRAY(TYPE) \
- int sort_array_##TYPE(struct array_##TYPE##_t *array){ \
- if(array == NULL || array->data == NULL || array->size <= 0 || array->capacity <= 0){ \
- return 1; \
- } \
- for(int i = array->size-1; i>=0; i--){ \
- for(int j = 1; j>=i; j++){ \
- if(array->data+j < array->data+j-1){ \
- int temp = *array->data+j;\
- array->data = array->data+j-1;\
- *array->data = temp;\
- } \
- } \
- } \
- return 0;\
- \
- }
- #endif //INC_15_FUNCTIONS_H
Advertisement
Add Comment
Please, Sign In to add comment