通过函数指针,可以巧妙实现面向对象的东西.
//vector.h
#ifndef __VACTOR_H__
#define __VECTOR_H__
/*======================STRUCT=========================*/
#define DEFAULT_ARRAY__SIZE 10
typedef int Datatype;
typedef struct Array
{
Datatype *data; /**< pointer to the array object */
int size; /**< current array's size */
int maxsize; /**< default array's maxsize*/
void(*constructor)(struct Array *pthis); /**< pointer to constructor*/
void(*add_value)(struct Array *pthis, Datatype data); /**< funciton pointer: add the data into the array*/
void(*print)(struct Array *pthis); /**< function pointer:print all of the data of array*/
int(*get_array_size)(struct Array *pthis); /**< function pointer: return current array size*/
int(*return_index_of_firstmatch)(struct Array *pthis, Datatype data); /**< function pointer: return index of the first match data*/
int (*return_index_value)(struct Array *pthis, Datatype data); /**< function pointer: return the value of the index*/
void(*descontructor)(struct Array *pthis); /**< function pointer: descontructor*/
}Array;
/*========================FUNCTION==============================*/
/**
* @brief initialize struct Array
* @param pointer to struct Array
*/
void array_init(Array *pthis);
/**
* @brief __constructor
* @param pointer to struct Array
*/
void __constructor(Array *pthis);
/**
* @brief add value into the array
* @param pointer to struct Array
* @param the data to be added
*/
void __add_value(Array *pthis, Datatype data);
/**
* @brief print the array
* @param pointer to struct Array
*/
void __print(Array *pthis);
/**
* @brief get array size
* @param pointer to struct Array
* @return size
*/
int __get_array_size(Array *pthis);
/**
* @brief return the inde of first match data of the array
* @param pointer to struct Array
* @param the data to find
* @return >=0 on success, represent the index
* -1 on failure
*/
int __return_index_of_firstmatch(Array *pthis, Datatype data);
/**
* @brief return the value of index
* @param pointer to struct Array
* @param index
* @return value
*/
int __return_index_value(Array *pthis, int index);
/**
* @brief destructor
* @param pointer to struct Array
*/
void __destructor(Array *pthis);
#endif
//vector.c
#include"vector.h"
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<malloc.h>
void array_init(Array *pthis)
{
assert(pthis);
/*initialize the function pointer*/
pthis->constructor = __constructor;
pthis->descontructor = __destructor;
pthis->add_value = __add_value;
pthis->print = __print;
pthis->get_array_size = __get_array_size;
pthis->return_index_value = __return_index_value;
pthis->return_index_of_firstmatch = __return_index_of_firstmatch;
/* implicit call the constructor */
pthis->constructor(pthis);
}
void __constructor(Array *pthis)
{
assert(pthis);
pthis->size = 0;
pthis->maxsize = DEFAULT_ARRAY__SIZE;
pthis->data = (Datatype *)malloc(sizeof(Datatype) * pthis->maxsize);
memset(pthis->data, 0, sizeof(Datatype) * pthis->maxsize);
}
void __add_value(Array *pthis, Datatype data)
{
assert(pthis);
if(pthis->size == pthis->maxsize)
{
pthis->maxsize += DEFAULT_ARRAY__SIZE;
Datatype * temp = (Datatype *)malloc(sizeof(Datatype) * pthis->maxsize);
int i;
for(i = 0; i < pthis->size; i++)
temp[i] = pthis->data[i];
free(pthis->data);
pthis->data = temp;
}
pthis->data[pthis->size] = data;
pthis->size++;
}
void __print(Array *pthis)
{
int i;
for(i = 0; i < pthis->size; i++)
printf("%d ", pthis->data[i]);
printf("\n");
}
int __get_array_size(Array *pthis)
{
return pthis->size;
}
int __return_index_of_firstmatch(Array *pthis, Datatype data)
{
int i;
for(i = 0; i < pthis->size; i++)
{
if(pthis->data[i] == data)
return i + 1;
}
return -1;
}
int __return_index_value(Array *pthis, int index)
{
return pthis->data[index];
}
/* destructor the object*/
void __destructor(Array *pthis)
{
free(pthis->data);
}
//main.c
#include<stdio.h>
#include"vector.h"
int main()
{
Array arr;
array_init(&arr);
int i;
for(i = 0; i < 25; i++)
{
arr.add_value(&arr, i);
}
arr.print(&arr);
printf("current array size:%d\n", arr.get_array_size(&arr));
/* if arr.return_index_of_firstmatch(&arr,5) return -1 ,that means could not exist*/
printf("%d is in the %dth of the array\n", 5, arr.return_index_of_firstmatch(&arr,5));
printf("%d index of the array is value %d\n", 5, arr.return_index_value(&arr, 5));
arr.descontructor(&arr);
return 0;
}