Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Last active February 23, 2017 09:36
Show Gist options
  • Save gustavorv86/3ccc69e4d1467fdb5d39070d08c1ca76 to your computer and use it in GitHub Desktop.
Save gustavorv86/3ccc69e4d1467fdb5d39070d08c1ca76 to your computer and use it in GitHub Desktop.
Adaptación del lenguaje C a la programación orientada a objetos
/*
File: Agenda.c
*/
#include "Agenda.h"
#include <stdio.h>
#include <stdlib.h>
void addLast(Agenda _this, Persona persona) {
AgendaNode node;
node = malloc(SIZEOF_AGENDANODE);
node->persona = persona;
node->next = NULL;
if(_this->_size == 0) {
_this->_listHead = node;
_this->_listTail = node;
} else {
_this->_listTail->next = node;
_this->_listTail = node;
}
(_this->_size)++;
}
void removeFirst(Agenda _this) {
Persona delPersona;
AgendaNode delNode;
int size;
size = _this->getSize(_this);
if(size > 0) {
delPersona = _this->_listHead->persona;
delPersona->destroyPersona(delPersona);
delNode = _this->_listHead;
_this->_listHead = _this->_listHead->next;
free(delNode);
}
(_this->_size)--;
}
int getSize(Agenda _this) {
return _this->_size;
}
Persona get(Agenda _this, int index){
AgendaNode ptr;
int i;
if(0 <= index && index < getSize(_this)) {
ptr = _this->_listHead;
for(i=0 ; i<index; i++) {
ptr = ptr->next;
}
return ptr->persona;
} else {
return NULL;
}
}
void print(Agenda _this){
AgendaNode ptr;
ptr = _this->_listHead;
printf("size = %d \n",_this->getSize(_this));
while(ptr != NULL) {
printf("%s\n", ptr->persona->toString(ptr->persona) );
ptr = ptr->next;
}
}
void destroyAgenda(Agenda _this){
// borra la agenda y su contenido
while(_this->getSize(_this) > 0) {
_this->removeFirst(_this);
}
free(_this);
}
Agenda newAgenda() {
Agenda _this = malloc(SIZEOF_AGENDA);
// inicializar atributos
_this->_listHead = NULL;
_this->_listTail = NULL;
_this->_size = 0;
// asignar metodos
_this->addLast = addLast;
_this->removeFirst = removeFirst;
_this->getSize = getSize;
_this->get = get;
_this->print = print;
_this->destroyAgenda = destroyAgenda;
return _this;
}
/*
File: Agenda.h
*/
#ifndef AGENDA_H
#define AGENDA_H
#define SIZEOF_AGENDANODE sizeof(struct _AgendaNode)
#define SIZEOF_AGENDA sizeof(struct _Agenda)
#include "Persona.h"
typedef struct _AgendaNode {
Persona persona;
struct _AgendaNode * next;
} * AgendaNode;
typedef struct _Agenda{
// ATTRS
AgendaNode _listHead;
AgendaNode _listTail;
int _size;
// METHODS
void (*addLast) (struct _Agenda* _this, Persona persona);
void (*removeFirst) (struct _Agenda* _this);
Persona (*get) (struct _Agenda* _this, int index);
int (*getSize) (struct _Agenda* _this);
void (*print) (struct _Agenda* _this);
void (*destroyAgenda) (struct _Agenda* _this);
} * Agenda;
Agenda newAgenda();
#endif
/*
File: Main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "Agenda.h"
int main() {
Agenda agenda;
Persona p0, p1, p2, p3, px;
// Crear los contactos
p0 = newPersona();
p0->setNombre(p0, "Mariano");
p0->setApellidos(p0, "Gonzalez Perez");
p0->setTelefono(p0, 600000000);
p1 = newPersona();
p1->setNombre(p1, "Elena");
p1->setApellidos(p1, "Lorenzo Garrido");
p1->setTelefono(p1, 600111111);
p2 = newPersona2("Luis", "Garcia Moratilla", 600222222);
p3 = newPersona2("Jorge", "Sanchez Navarro", 600333333);
// Introducirlos en la lista
agenda = newAgenda();
agenda->addLast(agenda, p3);
agenda->addLast(agenda, p2);
agenda->addLast(agenda, p1);
agenda->addLast(agenda, p0);
// Mostrar los datos de la agenda
agenda->print(agenda);
printf("\n\n");
// Obtener el contacto de la posicion 3
px = agenda->get(agenda, 3);
printf("%s\n",px->toString(px));
// Borrar toda la agenda y su contenido
agenda->destroyAgenda(agenda);
return (EXIT_SUCCESS);
}
/*
File: Persona.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Persona.h"
// GETTERS
char* getNombre(Persona _this){
return _this->_nombre;
}
char* getApellidos(Persona _this){
return _this->_apellidos;
}
long getTelefono(Persona _this){
return _this->_telefono;
}
int _numberCount(long number) {
int count = 0;
while(number > 0) {
number = number / 10;
count++;
}
return count;
}
char* toString(Persona _this) {
int strLength;
char* string;
strLength = strlen(_this->_nombre) + strlen(_this->_apellidos) + _numberCount(_this->_telefono) + 5;
string = malloc(strLength * sizeof(char));
snprintf(string,strLength,"[%s,%s,%ld]",_this->_nombre,_this->_apellidos,_this->_telefono);
return string;
}
char* _stringCopy(char* string) {
char* other_string;
int size;
size = strlen(string) +1;
other_string = malloc(size * sizeof(char));
strlcpy(other_string, string, size);
return other_string;
}
void setNombre(Persona _this, char* nombre){
_this->_nombre = _stringCopy(nombre);
}
void setApellidos(Persona _this, char* apellidos){
_this->_apellidos = _stringCopy(apellidos);
}
void setTelefono(Persona _this, long telefono){
_this->_telefono = telefono;
}
// DESTRUCTOR
void destroyPersona(Persona _this){
free(_this->_nombre);
free(_this->_apellidos);
free(_this);
}
// CONSTRUCTOR
Persona newPersona(){
Persona _this = malloc(SIZEOF_PERSONA);
_this->getNombre = getNombre;
_this->getApellidos = getApellidos;
_this->getTelefono = getTelefono;
_this->toString = toString;
_this->setNombre = setNombre;
_this->setApellidos = setApellidos;
_this->setTelefono = setTelefono;
_this->destroyPersona = destroyPersona;
return _this;
}
Persona newPersona2(char* nombre, char* apellidos, long telefono) {
Persona _this;
_this = newPersona();
_this->setNombre(_this, nombre);
_this->setApellidos(_this, apellidos);
_this->setTelefono(_this, telefono);
return _this;
}
/*
File: Persona.h
*/
#ifndef PERSONA_H
#define PERSONA_H
#define SIZEOF_PERSONA sizeof(struct _Persona)
typedef struct _Persona{
// ATTRS
char* _nombre; /* cadena de caracteres con el nombre */
char* _apellidos; /* cadena de caracteres con el apellido */
long _telefono;
// METHODS
char* (*getNombre) (struct _Persona* _this);
char* (*getApellidos) (struct _Persona* _this);
long (*getTelefono) (struct _Persona* _this);
char* (*toString) (struct _Persona* _this);
void (*setNombre) (struct _Persona* _this, char* nombre);
void (*setApellidos) (struct _Persona* _this, char* apellidos);
void (*setTelefono) (struct _Persona* _this, long telefono);
void (*destroyPersona) (struct _Persona* _this);
} * Persona;
// CONSTRUCTORES
Persona newPersona();
Persona newPersona2(char* nombre, char* apellidos, long telefono);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment