博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用C进行面向对象编程
阅读量:2355 次
发布时间:2019-05-10

本文共 1936 字,大约阅读时间需要 6 分钟。

stack.h

#ifndef __STACK_H__#define __STACK_H__#include 
#include
#include
#include
#ifdef __cplusplusextern "C"{#endifstruct validator{ bool (*const vali) (struct validator *p_this,int value); void *const p_data;}validator;struct range{ int min; int max;}range;struct { int previous_value;}previous;struct stack{ int top; const size_t size; struct validator * const p_validator; int *p_buf;}stack;#ifdef __cplusplus}#endif#endif

stack.c

#include "stack.h"static int stack_empty(struct stack *p_this){    return p_this->top == 0;}static int stack_full(struct stack *p_this){    return p_this->top == p_this->size;}static int validate_range(struct validator *p_this,int value){    if(!p_this)        return -1;    struct range *temp = (struct range *) p_this->p_data;    return (temp->min < value && temp->max > value);}int validate(struct validator *p_this,int value){    if(!p_this)    return 0;    return p_this->vali(p_this,value);}int pop(struct stack *p_this,int *value){    if(!p_this || stack_empty(p_this))    return -1;    p_this->top--;    *value = p_this->p_buf[p_this->top];    return 0;}int push(struct stack *p_this,int value){    if(stack_full(p_this) || !validate(p_this->p_validator,value))    return -1;    p_this->p_buf[p_this->top] = value;    p_this->top++;    return 0;}int main(int argc,char *argv[]){    int buff[16];    struct range a = {        .min = 10,        .max = 100    };    struct validator b = {        .p_data = (void *)&a,        .vali = validate_range,    };    struct stack c = {        .top = 0,        .size = 16,        .p_validator = &b,        .p_buf = buff,    };    push(&c,12);    int d;    pop(&c,&d);    printf("%d\n",d);    return 0;}

Makefile

src=stack.cobj=$(src:.o=.c)target=stackall:$(target)$(target):$(obj)	gcc $(obj) -o $@ -std=c99%.o : %.c	gcc -c $< -O $@ -std=c99clean:	rm -af $(target) $(obj)

 

转载地址:http://euftb.baihongyu.com/

你可能感兴趣的文章
我的下拉刷新实现步骤
查看>>
自定义AlertView实现模态对话框
查看>>
抛出异常函数 @throw
查看>>
iOS 截屏的总结,AVplayer的截屏,当前屏幕的截屏,截屏后导航栏不见的情况
查看>>
定时帧
查看>>
我的第一个用OC实现冒泡算法的程序
查看>>
一篇文章详解iOS之AutoResizing、AutoLayout、sizeClass来龙去脉
查看>>
pod
查看>>
ASIHTTPRequest 使用
查看>>
ASINetworkQueue 与 NSOperationQueue区别
查看>>
GCD介绍(一): 基本概念和Dispatch Queue
查看>>
GCD介绍(二): 多核心的性能
查看>>
GCD介绍(三): Dispatch Sources
查看>>
GCD介绍(四): 完结
查看>>
GCD实战一:使用串行队列实现简单的预加载
查看>>
微信开放平台创建应用时应用官网的问题
查看>>
协议与委托(Protocol and Delegate)实例解析
查看>>
生日礼物codeGift
查看>>
应用市场
查看>>
iOS审核马甲包被拒4.3的解决方案
查看>>