14.18.4(a)
#include
#include
#include
#include
#define N 20
#define LEN 30
void show(struct person *ptr);
struct fullname
{
char lastname[N];
char middname[N];
char firstname[N];
};
struct person
{
int num;
struct fullname name;
};
int main()
{
struct person record[5] = {
{1234567,{"xia", "long" ,"Han" }},
{2234567,{"qiang", "" , "Li" }},
{3234567,{"yi", "zun" , "Li" }},
{4234567,{"ze", "ling" ,"Li" }},
{5234567,{"zhu", "" ,"Qing" }},
};
show(record);
return 0;
}
void show(struct person *ptr)
{
int i;
printf("All numbers messages:\n");
for (i = 0; i<5; i++)
{
if (strlen (ptr[i].name.middname) )
{
printf("%s, %s %c - %d\n",ptr[i].name.firstname, ptr[i].name.lastname,ptr[i].name.middname[0],ptr[i].num );
}
else
{
printf("%s, %s - %d\n", ptr[i].name.firstname, ptr[i].name.lastname, ptr[i].num);
}
}
}
14.18.4(b)
#include
#include
#include
#include
#define N 20
#define LEN 30
void show(struct person );
struct fullname
{
char lastname[N];
char middname[N];
char firstname[N];
};
struct person
{
int num;
struct fullname name;
};
int main()
{
int i;
struct person record[5] = {
{1234567,{"xia", "long" ,"Han" }},
{2234567,{"qiang", "" , "Li" }},
{3234567,{"yi", "zun" , "Li" }},
{4234567,{"ze", "ling" ,"Li" }},
{5234567,{"zhu", "" ,"Qing" }},
};
for(i = 0; i < 5; i++)
show(record[i]);
return 0;
}
void show(struct person pers)
{
int i;
printf("All numbers messages:\n");
if (strlen (pers.name.middname) )
{
printf("%s, %s %c - %d\n", pers.name.firstname, pers.name.lastname, pers.name.middname[0],pers.num );
}
else
{
printf("%s, %s - %d\n", pers.name.firstname, pers.name.lastname, pers.num);
}
}