课后作业
#include<stdio.h>#include<stdlib.h>
#define LEN sizeof(struct date)
struct date
{
int num;
char name;
char sex;
char job;
union position {
int clas;
char prof;
}z;
struct date *next;
};
int main()
{
struct date *temp;
struct date *input();
void print(struct date *);
temp = input();
print(temp);
}
struct date *input()
{
char x;
int n = 0;
struct date *head, *p1, *p2=NULL;
p2 = p1 = head = (struct date *) malloc(LEN);
printf("please input the date:\n");
printf("input the num:\n");
scanf_s("%d", &p1->num);
printf("input the name:\n");
scanf_s("%s", p1->name, 20);
getchar();
printf("input the sex:\n");
scanf_s("%c", &p1->sex, 1);
getchar();
printf("input the job:\n");
scanf_s("%c", &p1->job, 1);
getchar();
if (p1->job == 's')
{
printf("input the clas:\n");
scanf_s("%d", &p1->z.clas);
getchar();
}
else if (p1->job == 't')
{
printf("input the position:\n");
scanf_s("%s", &p1->z.prof,20);
getchar();
}
else
{
printf("error\n");
}
while (0==n)
{
printf("是否继续输入数据?Y/N\n");
scanf_s("%c", &x,1);
if (x == 'Y')
{
p1 = (struct date *)malloc(LEN);
printf("please input the date:\n");
printf("input the num:\n");
scanf_s("%d", &p1->num);
printf("input the name:\n");
scanf_s("%s", p1->name, 20);
getchar();
printf("input the sex:\n");
scanf_s("%c", &p1->sex, 1);
getchar();
printf("input the job:\n");
scanf_s("%c", &p1->job, 1);
getchar();
if (p1->job == 's')
{
printf("input the clas:\n");
scanf_s("%d", &p1->z.clas);
getchar();
}
else if (p1->job == 't')
{
printf("input the position:\n");
scanf_s("%s", &p1->z.prof,20);
getchar();
}
else
{
printf("error\n");
}
p2->next = p1;
p2 = p1;
}
else if (x == 'N')
{
n = 1;
p2->next = NULL;
}
else
{
printf("error\n");
n = 1;
p2->next = NULL;
}
}
return head;
}
void print(struct date *stu)
{
struct date *p1,*p2;
p2 = p1 = stu;
while (p2->next != NULL)
{
printf("学号:%d\n", p1->num);
printf("姓名:%s\n", p1->name);
printf("性别:%c\n", p1->sex);
printf("职业:%c\n", p1->job);
if (p1->job == 's')
{
printf("班级为:%d\n", p1->z.clas);
}
else {
printf("职务为:%s\n", p1->z.prof);
}
p2 = p1;
p1 = p1->next;
}
}
页:
[1]