创建一个内含3个嵌套结构类型的数组,按要求输入并输出打印
#include<stdio.h>
#include<string.h>
#define SIZE 20
struct name {
char lname[SIZE];
char mname[SIZE];
char fname[SIZE];
};
struct person {
int num;
struct name handle;
};
struct person getinfo(void);
char *s_gets(char *st, int n);
void show(struct person temp);
int main()
{
struct person arrary[3];
for (int i = 0; i < 3; i++)
{
arrary[i] = getinfo();
}
for (int i = 0; i < 3; i++)
{
show(arrary[i]);
}
return 0;
}
struct person getinfo(void)
{
struct person temp;
printf("please enter your num:\n");
scanf("%d", &temp.num);
while (getchar() != '\n') //吃掉换行符
continue;
printf("please enter your last name :\n");
s_gets(temp.handle.lname, SIZE);
printf("please enter your midname:\n");
s_gets(temp.handle.mname, SIZE);
printf("please enter your first name:\n");
s_gets(temp.handle.fname, SIZE);
printf("OK!\n\n");
return temp;
}
void show(struct person temp)
{
if (strlen(temp.handle.mname))
{
printf("%s, %c. %s -- %d\n", temp.handle.lname, temp.handle.mname[0], temp.handle.fname, temp.num);
}
else
{
printf("%s, %s -- %d\n", temp.handle.lname, temp.handle.fname, temp.num);
}
}
char *s_gets(char *st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
网友评论