c语言, objective code(new 1)

时间:2024-01-18 21:40:56

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

*/