jippwar1
kubajanu 1,498 6th Jun, 2022
//// MAIN
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
#include "my_tab.h"
#pragma warning (disable : 4996)
ARR_ELEM* add()
{
STUDENT* a = InitStudent();
ARR_ELEM* elem = NULL;
elem = (ARR_ELEM*)malloc(sizeof(ARR_ELEM));
elem->ptr = a;
elem->ptr_free = FreeStudent;
elem->ptr_print = PrintStudent;
return elem;
}
int main() {
size_t size = 2;
ARR_ELEM* KONTENER = InitArr(size);
for(int i=0;i<3;i++)
PushObj(KONTENER, add());
PrintStudent(KONTENER[ARR_ELEM::last-1].ptr);
PrintArr(KONTENER);
FreeArr(KONTENER);
}
/// student.cpp
#include "student.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning (disable : 4996)
static const char* TYPE_STR[]{
"IT",
"MATHS"
};
STUDENT* InitStudent()
{
char surname[512];
TYPE choice;
printf("Input surname: ");
scanf("%s", surname);
for (int i = 0; i < END_OF_ENUM; i++) {
printf("%d.%s\n", i, TYPE_STR[i]);
}
printf("Input studies: ");
scanf("%d", &choice);
STUDENT* student = NULL;
student = (STUDENT*)malloc(sizeof(STUDENT));
if (!student) {
printf("ALLOC ERROR\n");
exit(-1);
}
student->surname = (char*)malloc((strlen(surname)+1)*sizeof(char));
if(!student->surname) {
printf("ALLOC ERROR\n");
exit(-1);
}
memset(student->surname, 0, strlen(surname) + 1);
strcpy(student->surname, surname);
student->studies = choice;
return student;
}
void PrintStudent(void* ptr)
{
STUDENT* student = (STUDENT*)ptr;
printf("Student: %s %s\n", student->surname, TYPE_STR[student->studies]);
}
void FreeStudent(void* ptr)
{
STUDENT* student = (STUDENT*)ptr;
if (student) {
if (student->surname) {
free(student->surname);
student->surname = NULL;
}
free(student);
student = NULL;
}
}
/// student.h
#pragma once
enum TYPE {
IT,
MATHS,
END_OF_ENUM
};
struct STUDENT {
char* surname;
TYPE studies;
};
STUDENT* InitStudent();
void PrintStudent(void *ptr);
void FreeStudent(void* ptr);
//// my_tab.cpp
#include "my_tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t ARR_ELEM::last = 0;
ARR_ELEM* InitArr(size_t size)
{
ARR_ELEM* tab = NULL;
tab = (ARR_ELEM*)malloc(size*sizeof(ARR_ELEM));
if (!tab) {
printf("ALLOC ERROR\n");
exit(-1);
}
memset(tab, 0, size);
return tab;
}
void PushObj(ARR_ELEM* arr, ARR_ELEM* elem)
{
if (!arr) {
printf("NO ARR");
exit(-1);
}
size_t real_size = _msize(arr) / sizeof(ARR_ELEM);
if (real_size <= ARR_ELEM::last) {
arr = (ARR_ELEM*)realloc(arr,2*real_size*sizeof(ARR_ELEM));
if (!arr) {
printf("ERROR!\n");
exit(-1);
}
memset((void*)(arr + real_size), 0, real_size * sizeof(ARR_ELEM));
}
arr[ARR_ELEM::last].ptr = elem->ptr;
arr[ARR_ELEM::last].ptr_free = elem->ptr_free;
arr[ARR_ELEM::last].ptr_print = elem->ptr_print;
ARR_ELEM::last++;
}
void PrintArr(ARR_ELEM* arr)
{
if (!arr) {
printf("NO ARR");
exit(-1);
}
size_t last = ARR_ELEM::last;
for (size_t i = 0; i < last; i++) {
(*arr[i].ptr_print)(arr[i].ptr);
}
}
void FreeArr(ARR_ELEM* arr)
{
if (arr) {
for (size_t i = 0; i < ARR_ELEM::last; i++) {
if (arr[i].ptr) {
(arr[i].ptr_free)(arr[i].ptr);
}
}
free(arr);
arr = NULL;
}
else
{
printf("NO ARR");
exit(-1);
}
}
//// my_tab.h
#pragma once
typedef void(*PrintObject)(void* print_obj);
typedef void(*FreeObject)(void* free_obj);
struct ARR_ELEM {
void* ptr;
PrintObject ptr_print;
FreeObject ptr_free;
static size_t last;
};
ARR_ELEM* InitArr(size_t size);
void PushObj(ARR_ELEM* arr, ARR_ELEM* elem);
void PrintArr(ARR_ELEM* arr);
void FreeArr(ARR_ELEM* arr);
To share this paste please copy this url and send to your friends
RAW Paste Data