-
-
Save fuminashi/7925706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* lst.c */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<lst.h> | |
lstpet* addlstpet(lstpet* l, pet* data){ | |
lstpet* ret = malloc(sizeof(lstpet)); | |
ret->data = data; | |
ret->next = l; | |
return ret; | |
} | |
lstowner* addlstowner(lstowner* l, owner* data){ | |
lstowner* ret = (lstowner*)malloc(sizeof(lstowner)); | |
ret->data = data; | |
ret->next = l; | |
return ret; | |
} | |
void printlst(lst* p){ | |
while(p!=NULL){ | |
(p->g->data->g->print)(*p->g->data); | |
p = p->g->next; | |
} | |
} | |
owner* searchlstownerbyname(lstowner* p, char* name){ | |
while(p!=NULL){ | |
if(!strcmp(p->data->name, name)){ | |
return p->data; | |
} | |
p = p->next; | |
} | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* lst.h */ | |
#ifndef _LST_H_ | |
#define _LST_H_ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<pet.h> | |
#include<owner.h> | |
typedef struct general{ | |
char* name; | |
void (*print)(union object); | |
} general; | |
typedef union object{ | |
general* g; | |
pet* p; | |
owner* o; | |
} object; | |
typedef struct lstgeneral{ | |
object* data; | |
struct general* next; | |
} lstgeneral; | |
typedef struct lstpet{ | |
struct pet* data; | |
struct lstpet* next; | |
void (*print)(union object); | |
} lstpet; | |
lstpet* addlstpet(lstpet* l, struct pet* data); | |
typedef struct _lstowner{ | |
owner* data; | |
struct _lstowner* next; | |
void (*print)(union object); | |
} lstowner; | |
lstowner* addlstowner(lstowner* l, owner* data); | |
typedef union lst{ | |
lstgeneral* g; | |
lstpet* p; | |
lstowner* o; | |
} lst; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* owner.c */ | |
#include<lst.h> | |
#include<stdio.h> | |
#include<stdlib.h> | |
owner* newowner(char* name){ | |
owner* ret = (owner*)malloc(sizeof(owner)); | |
ret->name = name; | |
ret->head = NULL; | |
ret->last = NULL; | |
ret->print = printowner; | |
return ret; | |
} | |
void addpet(owner* own, pet* pt){ | |
lstpet* p = malloc(sizeof(lstpet)); | |
p->data = pt; | |
p->next = NULL; | |
if(own->head ==NULL){ | |
own->head = p; | |
own->last = p; | |
}else{ | |
own->last->next = p; | |
own->last = p; | |
} | |
} | |
void printowner(owner* own){ | |
printf("onwername = %s\n",own->name); | |
lstpet* p; | |
p = own->head; | |
while(p){ | |
printpet(p->data); | |
p = p->next; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* owner.h */ | |
#ifndef _OWNER_H_ | |
#define _OWNER_H_ | |
#include<pet.h> | |
struct pet; | |
typedef struct owner{ | |
char* name; | |
struct lstpet* head; | |
struct lstpet* last; | |
void (*print)(union object); | |
} owner; | |
owner* newowner(char* name); | |
void addpet(owner* own, struct pet* pt); | |
void printowner(owner* own); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pet.c */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<owner.h> | |
pet* newpet(char* name, float weight, owner* own){ | |
pet* ret = (pet*)malloc(sizeof(pet)); | |
ret->name = name; | |
ret->weight = weight; | |
ret->own = own; | |
ret->print = printpet; | |
addpet(own, ret); | |
return ret; | |
} | |
void printpet(pet* p){ | |
printf("pet name = %s\n",p->name); | |
printf("pet weight = %f\n",p->weight); | |
printf("pet owner = %s\n",p->own->name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pet.h */ | |
#ifndef _PET_H_ | |
#define _PET_H_ | |
struct owner; | |
union object; | |
typedef struct pet{ | |
char* name; | |
struct owner* own; | |
float weight; | |
void (*print)(union object); | |
} pet; | |
pet* newpet(char* name, float weight, struct owner* own); | |
void printpet(pet* p); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* petdatabase.c */ | |
#include<stdio.h> | |
#include<lst.h> | |
int main(void){ | |
lst* lst; | |
lst->o = addlstowner(NULL, newowner("Sazae")); | |
lst->o = addlstowner(lst->o, newowner("Ojisan")); | |
lst->o = addlstowner(lst->o, newowner("Momotaro")); | |
lst->p = addlstpet(NULL, newpet("tama", 1.6f, | |
searchlstownerbyname(lst->o,"Sazae"))); | |
lst->p = addlstpet(lst->p, newpet("pochi", 3.6f, | |
searchlstownerbyname(lst->o,"Ojisan"))); | |
lst->p = addlstpet(lst->p, newpet("inu", 4.0f, | |
searchlstownerbyname(lst->o,"Momotaro"))); | |
lst->p = addlstpet(lst->p, newpet("saru", 3.2f, | |
searchlstownerbyname(lst->o,"Momotaro"))); | |
lst->p = addlstpet(lst->p, newpet("kiji", 0.8f, | |
searchlstownerbyname(lst->o,"Momotaro"))); | |
printlst(lst); | |
printf("------------------\n"); | |
/* 先生がもともと書いていたもの | |
lstowner* lsto; | |
lstpet* lstp; | |
lsto = addlstowner(NULL, newowner("Sazae")); | |
lsto = addlstowner(lsto, newowner("Ojisan")); | |
lsto = addlstowner(lsto, newowner("Kushami")); | |
lsto = addlstowner(lsto, newowner("Heidi")); | |
lsto = addlstowner(lsto, newowner("Mickey")); | |
lsto = addlstowner(lsto, newowner("Charlie")); | |
lstp = addlstpet(NULL, newpet("tama", 1.6f, | |
searchlstownerbyname(lsto,"Sazae"))); | |
lstp = addlstpet(lstp, newpet("pochi", 3.6f, | |
searchlstownerbyname(lsto,"Ojisan"))); | |
lstp = addlstpet(lstp, newpet("wagahai", 2.1f, | |
searchlstownerbyname(lsto,"Kushami"))); | |
lstp = addlstpet(lstp, newpet("kuma", 2.1f, | |
searchlstownerbyname(lsto,"Heidi"))); | |
lstp = addlstpet(lstp, newpet("shiro", 4.0f, | |
searchlstownerbyname(lsto,"Heidi"))); | |
lstp = addlstpet(lstp, newpet("joseph", 3.2f, | |
searchlstownerbyname(lsto,"Heidi"))); | |
lstp = addlstpet(lstp, newpet("Pluto", 0.8f, | |
searchlstownerbyname(lsto,"Mickey"))); | |
lstp = addlstpet(lstp, newpet("Snoopy", 0.8f, | |
searchlstownerbyname(lsto,"Charlie"))); | |
printlstowner(lsto); | |
printf("------------------\n"); | |
printlstpet(lstp); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment