Anonymous Anonymous - 1 month ago
145 0

No description

C

Array

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRINGELEM_SIZE 15

char bool_label[][3] = { "NO", "YES" };

struct arr {
        int arr_size;
        void** elems;
        int elem_size;
        
        void (*display)(void **, int);
        int (*compare)(void *, void *);
        void (*delete)(void *);
};

typedef struct arr CArray;

CArray* build_array(int arr_size, int elem_size, void (*display_func)(void**, int),
int (*comp_func)(void*, void*), void (*free_func)(void*)) {
        CArray* A = (CArray *) malloc(sizeof(CArray));
        A->elems = (void **) malloc(sizeof(void *) * arr_size);
        A->arr_size = arr_size;
        A->elem_size = elem_size;
        A->display = display_func;
        A->compare = comp_func;
        A->delete = free_func;
        return A;
}

void add_elem(CArray* A, void* elem, int pos) {
        if(pos < 0 || pos > A->arr_size) {
                printf("Invalid pos!\n");
                return;
        }
        A->elems[pos] = elem;
}

void delete_elem(CArray* A, int pos) {
        if(!A->delete) {
                printf("Unidentified free function!\n");
                return;
        }
        if(pos < 0 || pos > A->arr_size) {
                printf("Invalid pos!\n");
                return;
        }
        A->delete(A->elems[pos]);
}

int search(CArray* A, void* elem) {
        for(int i = 0; i < A->arr_size; ++i) {
                if(!A->compare(A->elems[i], elem)) {
                        return i;
                }
        }
        return 0;
}

int IntCompare(void* e1, void* e2) {
        return (int*)e1 - (int*)e2;
}

void show_int(void** elems, int arr_size) {
        for(int i = 0; i < arr_size; ++i)
                printf("%d ", (int *) elems[i]);
}

void show_str(void** elems, int arr_size) {
        for(int i = 0; i < arr_size; ++i)
                printf("%s ", (char *) elems[i]);
}

char some_str[][MAX_STRINGELEM_SIZE] = { "C is the", "Best language", "Python sucks" };

int main() {
        CArray* IntArray = build_array(6, sizeof(int), show_int, IntCompare, NULL);
    CArray* StringArray= build_array(3, MAX_STRINGELEM_SIZE, show_str, strcmp, free);
        for(int i = 0; i < 6; ++i) {
                /*int* temp = (int *) malloc(sizeof(int));
                temp = i + 1;*/
                int temp = i + 1;
                add_elem(IntArray, temp, i);
        }
        for(int i = 0; i < 3; ++i)
                add_elem(StringArray, some_str[i], i);                
        
        int three = 3, ten = 10;
        printf("3 is an element? %s\n10 is an element? %s\n", bool_label[!!search(IntArray,
three)], bool_label[!!search(IntArray, ten)]);        
        
        IntArray->display(IntArray->elems, IntArray->arr_size);
        printf("\n\n");
        StringArray->display(StringArray->elems, StringArray->arr_size);
        printf("John is an element? %s\nPython sucks is an element? %s",
bool_label[!!search(StringArray, "John")], bool_label[!!search(StringArray, "Python
sucks")]);
        return 0;
}