看完书后第一次尝试敲一下链表但是发现这个程序的结构体数组的各个成员没法赋值
这是主要的代码,本来想既有输入又有输出的
struct Student
{
char name[20];
int num;
float score;
int age;
union salary
{
float have;
char unhave[20];
}a;
struct Student* next;
};
int length = sizeof(struct Student);
struct Student* lian(void)
{
struct Student*p,*p1,*p2;
p1 = p2 = (struct Student*)malloc(length);
p = NULL;
int n=0;
struct Student stu[3];
if (p1 != NULL)
{
n += 1;
int i=1;
char awnser;
if (n == 1)
{
printf("请依次输入该学生的姓名(英语),学号,总成绩,年龄\n");
scanf(" %c,%d,%f,%d", p1->name, p1->num, p1->score, p1->age);
if (p1->age > 18)
{
printf("请问是否有工作?(y/n)\n");
//getchar();
scanf("%c", &awnser);
if (awnser == 'y')
{
//float salary2;
printf("请输入工资\n");
getchar();
scanf("%f", &stu[0].a.have);
//stu[0].a.have = salary2;
}
else
{
strcpy(( stu[0].a.unhave) , "do_not_have_a_job");
}
}
p = p1;
p2 = p1;
p2->next = p1;
p1 = (struct Student*)malloc(length);
p1 = NULL;
}
else
{
char conduse;
while ( strcmp(stu[i].name,"sos")!=0)
{
//struct Student* temp;
p1 = (struct Student*)malloc(length);
printf("请依次输入该学生的姓名(英语),学号,总成绩,年龄在名字处用s代表结束输入\n");
scanf("%c,%d,%f,%d", stu[i].name, &stu[i].num, &stu[i].score, &stu[i].age);
printf("请问是否有工作?(y/n)\n");
scanf("%c", &awnser);
if (awnser == 'y')
{
float salary2;
printf("请输入工资\n");
getchar();
scanf("%f", &salary2);
stu[i].a.have = salary2;
}
else
{
strcpy(stu[i].a.unhave, "do_not_have_a_job");
}
p2 = p1;
p1 = (struct Student*)malloc(length);
}
p1 = NULL;
}
}
return (p);
}
void my_printf(struct Student*stu0)
{
struct Student* stu1, * stu2;
stu1 = stu0;
if (stu0 != NULL)
{
for (; stu1 != NULL; stu1 = stu1->next)
{
//int l;
//for (l = 0; l < 3; l++)
printf("%c,%d,%f,%d", stu1->name, stu1->num, stu1->score, stu1->age);
if (stu1->age > 18)
{
if (stu1->a.have != 0)
{
printf("该学生已在工作,且工资为%d\n", stu1->a.have);
}
else
printf("%s", stu1->a.unhave);
}
}
}
}
代码没看明白,到底是要实现链表输入,还是数组输入?一半对一半?