Untitled
kubajanu 1,599 6th Jun, 2022
//// MAIN
#include <stdio.h>
#include <stdlib.h>
#include "my_data.h"
#include "my_tab.h"
LIST_ELEM* add() {
COORD* c = InitCoord();
LIST_ELEM* elem = NULL;
elem = (LIST_ELEM*)malloc(sizeof(LIST_ELEM));
elem->data_pointer = c;
elem->print_function = PrintCoord;
elem->free_function = FreeCoord;
return elem;
}
int main() {
LIST_ELEM* KONTENER = InitList(2);
for(int i = 0; i < 3; i++)
PushObj(KONTENER, add());
if (KONTENER[1].data_pointer) {
PrintCoord(KONTENER[1].data_pointer);
}
PrintList(KONTENER);
FreeList(KONTENER);
}
///// my_data.cpp
#pragma warning (disable : 4996)
#include "my_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
COORD* InitCoord()
{
COORD* coord = NULL;
coord = (COORD*)malloc(sizeof(COORD));
if (!coord) {
printf("ERROR");
exit(-1);
}
char name[512];
printf("Enter name of new coord: ");
scanf("%s", &name);
printf("Enter value of X: ");
scanf("%lf", &coord->xy[0]);
printf("Enter value of Y: ");
scanf("%lf", &coord->xy[1]);
coord->name = NULL;
coord->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
if (!coord->name) {
printf("ERROR");
exit(-1);
}
memset(coord->name, 0, strlen(name) + 1);
strcpy(coord->name, name);
return coord;
}
void PrintCoord(void* ptr)
{
COORD* coord = (COORD*)ptr;
printf("Name: %s X: %.3lf Y: %.3lf\n", coord->name, coord->xy[0], coord->xy[1]);
}
void FreeCoord(void* ptr)
{
COORD* coord = (COORD*)ptr;
if (coord) {
if (coord->name) {
free(coord->name);
coord->name = NULL;
}
free(coord);
coord = NULL;
}
}
///// my_data.h
#pragma once
struct COORD {
char* name;
double xy[2]; // 0->X 1->Y
};
COORD* InitCoord();
void PrintCoord(void* ptr);
void FreeCoord(void* ptr);
////// my_tab.cpp
#include "my_tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t LIST_ELEM::last = 0;
LIST_ELEM* InitList(size_t size)
{
LIST_ELEM* list = NULL;
list = (LIST_ELEM*)malloc(sizeof(LIST_ELEM) * size);
if (!list) {
printf("ERROR\n");
exit(-1);
}
memset(list, 0, sizeof(LIST_ELEM) * size);
return list;
}
void PushObj(LIST_ELEM* list, LIST_ELEM* obj)
{
size_t last = LIST_ELEM::last, total_size = _msize(list) / sizeof(LIST_ELEM);
if (last >= total_size) {
list = (LIST_ELEM*)realloc(list, 2*total_size*sizeof(LIST_ELEM));
if (!list)
exit(-1);
memset((void*)(list + total_size), 0, total_size * sizeof(LIST_ELEM));
}
list[last].data_pointer = obj->data_pointer;
list[last].free_function = obj->free_function;
list[last].print_function = obj->print_function;
LIST_ELEM::last++;
}
void PrintList(LIST_ELEM* list)
{
size_t last = LIST_ELEM::last;
for (size_t i = 0; i < last; i++) {
(*list[i].print_function)(list[i].data_pointer);
}
}
void FreeList(LIST_ELEM* list)
{
size_t last = LIST_ELEM::last;
for (size_t i = 0; i < last; i++) {
(*list[i].free_function)(list[i].data_pointer);
}
}
////// my_tab.h
#pragma once
typedef void(*PrintObj)(void* ptr);
typedef void(*FreeObj)(void* ptr);
struct LIST_ELEM {
void* data_pointer;
PrintObj print_function;
FreeObj free_function;
static size_t last;
};
LIST_ELEM* InitList(size_t size);
void PushObj(LIST_ELEM* list, LIST_ELEM* obj);
void PrintList(LIST_ELEM* list);
void FreeList(LIST_ELEM* list);
To share this paste please copy this url and send to your friends
RAW Paste Data