#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char *name;
char *sex;
int age;
char *job;
char *mail;
} profile;
static void initProfile(profile *sp);
static void setProfile(profile *sp);
static void disposeProfile(profile *sp);
static void showProfile(profile *sp);
static int getAge(int birthday);
static const size_t Maxbuf = 64;
int main(void)
{
profile *entity;
entity = malloc(sizeof(profile));
initProfile(entity);
setProfile(entity);
showProfile(entity);
disposeProfile(entity);
free(entity);
return 0;
}
static void initProfile(profile *sp)
{
sp->name = malloc(Maxbuf + 1);
sp->sex = malloc(Maxbuf + 1);
sp->job = malloc(Maxbuf + 1);
sp->mail = malloc(Maxbuf + 1);
}
static void setProfile(profile *sp)
{
snprintf(sp->name, Maxbuf, "%s %s", "HASE", "Kazuaki");
snprintf(sp->sex, Maxbuf, "%s", "Male");
snprintf(sp->job, Maxbuf, "%s", "Conposer");
snprintf(sp->mail, Maxbuf, "%s@%s", "hase", "psipsina.jp");
sp->age = getAge(19860313);
}
static void disposeProfile(profile *sp)
{
free(sp->name);
free(sp->sex);
free(sp->job);
free(sp->mail);
}
static void showProfile(profile *sp)
{
printf("Name: %s\n", sp->name);
printf("Sex : %s\n", sp->sex);
printf("Age : %d\n", sp->age);
printf("Job : %s\n", sp->job);
printf("Mail: %s\n", sp->mail);
}
static int getAge(int birthday)
{
time_t t = time(NULL);
struct tm *local = localtime(&t);
const int thisYear = local->tm_year + 1900;
const int thisDays = (local->tm_mon + 1) * 100 + local->tm_mday;
const int birthYear = birthday / 10000;
const int birthDays = birthday % 10000;
int age = thisYear - birthYear;
return ((thisDays >= birthDays) ? age : --age);
}