c struct, objective code

////////
////
typedef int (*PF_EAT) (char* food, const int cnt);
typedef int (*PF_WALK) (char* place, const int miles, int walk_miles);
typedef void (*PF_SAY) (const char* to_who, const char* words); #define FEMALE 0
#define MALE 1 typedef struct{
char* name;
char sex;
int age;
PF_EAT eat;
PF_WALK walk;
PF_SAY say;
}ST_PERSON; struct foods{
char name[];
int cnt;
}; struct foods food_to_full[] = {
{"milk", },
{"rice", },
{"bread", }
}; int f_eat (char* food, const int cnt)
{ int index = ;
int ret = -;//ret = -1, nothing to eat if(food == ){
return -;
} for(index = ; index < ; index++){
if(strcmp(food, food_to_full[index].name) == ){
ret = (cnt >= food_to_full[index].cnt ? : );//ret = 1, not full printf("eat %d *%s %s\n", cnt, food, ret == ? "is full" : "is not full"); break;
}
} return ret;
} int f_walk(char* place, const int miles, int walk_miles)
{
int ret = ;
if(place == ){
return ;
} ret = (miles - walk_miles > ) ? (miles - walk_miles) : ; printf("walk to %s, %s", place, ret > ? "is on the load" : "has arrived the location" ); if(ret > )
{
printf(", remains %d miles to walk", ret);
}
printf("\n");
return ret; } void f_say (const char* to_who, const char* words)
{
printf("Hi %s, %s\n",to_who, words);
} void person_one_day(ST_PERSON *person, struct foods* food_have, char *where, int miles, int walk_miles, char* to_who, char* words )
{
printf("%s, %s, %d years old\n", person->name, (person->sex == MALE) ? "male" : "female", person->age);
person->eat(food_have->name, food_have->cnt);
person->walk(where, miles, walk_miles);
person->say(to_who , words); printf("\n");
}
ST_PERSON Ocean = {
.name = "Ocean",
.sex = MALE,
.age = ,
.eat = f_eat,
.walk = f_walk,
.say = f_say,
}; ST_PERSON Li = {
.name = "Li",
.sex = FEMALE,
.age = ,
.eat = f_eat,
.walk = f_walk,
.say = f_say,
};
int main(int argc, char** argv) 
{
struct foods food_have_ocean = {"rice", };
struct foods food_have_li = {"milk", };
person_one_day(&Li, &food_have_li, "town", , , "Lucy", "I am so tired!");
person_one_day(&Ocean, &food_have_ocean, "town", , , "Lucy", "I am waiting for my wife");
return ;
}

/*

>gcc person.c ; ./a.out

Li, female, 28 years old
eat 1 *milk is not full
walk to town, is on the load, remains 4 miles to walk
Hi Lucy, I am so tired!


Ocean, male, 26 years old
eat 1 *rice is full
walk to town, has arrived the location
Hi Lucy, I am waiting for my wife

*/

c语言, objective code(new 1)的更多相关文章

  1. c语言, objective code(new 2)

    参考: 1. C中的继承和多态 http://www.cnblogs.com/skynet/archive/2010/09/23/1833217.html

  2. C语言工具---Code::Blocks

    Code::Blocks Code::Blocks 是一个开源的全功能的跨平台C/C++集成开发环境. Code::Blocks是开放源码软件.由纯粹的C++语言开发完成,它使用了著名的图形界面库wx ...

  3. iOS开发核心语言Objective C —— 全部知识点总结

    本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...

  4. iOS开发核心语言Objective C —— 面向对象思维、setter和getter方法及点语法

    本分享是面向有意向从事iOS开发的伙伴们.或者已经从事了iOS的开发人员.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基础,建议您先翻阅我之前分享的iOS ...

  5. iOS开发核心语言Objective C —— 所有知识点总结

    C和OC对比 OC中主要开发在什么平台上的应用程序?答:可以使用OC开发Mac OS X平台和iOS平台的应用程序 OC中新增关键字大部分是以什么开头?答:OC中新增关键字大部分是以@开头 OC中新增 ...

  6. 【转载】谷歌酝酿将苹果Swift作为安卓APP主要开发语言

    TNW中文站 4月8日报道 安卓操作系统的软件开发语言是Java,而在过去几年中,有关Java的版权,谷歌(微博)和甲骨文之间发生了长期的诉讼.最新外媒消息称,谷歌正在考虑将苹果开发的Swift作为未 ...

  7. Protobuf语言指南(转)

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...

  8. Protobuf语言指南

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...

  9. 【编辑器】Visual Studio Code

    1.官网:https://code.visualstudio.com/Download 2.插件:https://marketplace.visualstudio.com/VSCode https:/ ...

随机推荐

  1. jQuery通知插件noty

    jQuery 通知查件noty 简单使用 官方:http://ned.im/noty/ 其它查件推荐 NotifIt Demo http://js.itivy.com/jiaoben1852/inde ...

  2. 安装ADT 提示 android ADT Unable to connect to repository

    以往安装ADT根本就不是个问题,可是现在就是个大问题了,联通的宽带网络连www.google.com.hk都打不开,你叫我们这些P民怎么活? https://dl-ssl.google.com/and ...

  3. 用macports装了一份openssl

    我已经用macports装了一份openssl,然后自己又编译了一份openssl....第三方给Mac出的一个类似BSD Ports的一个软件包管理工具装的话只需要sudo port install ...

  4. Ruby学习: 类变量和类方法

    一.类变量 在ruby中,可以为类定义类变量,类变量的值为类的所有实例(对象)所共享. 有点类似其它语言(如java)中的静态变量,但与java中的静态变量的区别是, 类变量是私有的,无法在类的外部访 ...

  5. c++ 复制构造函数和赋值函数

    c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...

  6. perl 自动发产品

    use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...

  7. 注解配置 ssh

    注解 <?xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://ww ...

  8. chrome 浏览器帐号登录不来,如何解决自己的书签

    装系统前把 该目录下  C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default    的  Bookmarks 复制出 ...

  9. Flask web开发 简单介绍

    Flask是一个基于python的轻量级web框架.当安装好后Flask后 (pip install flask),就可以开始使用了. 一.最简单的例子 1.新建目录,作为web应用的目录,如: mk ...

  10. SolrCloud简介

    原文地址:http://www.chepoo.com/solrcloud-introduction.html 一.简介 SolrCloud是Solr4.0版本以后基于Solr和Zookeeper的分布 ...