head.h
#ifndef __STU_H__
#define __STU_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
char sex;
char work;
union
{
float score;
char postion[20];
};
} *person, Person;
void output(person p);
void input(person p);
#endif
main.c
#include "head.h"
void input(person p)
{
printf("\n输入姓名:");
scanf("%s", p->name);
printf("\n输入性别(w/m):");
scanf(" %c", &p->sex);
printf("\n输入职业(s/t):");
scanf(" %c", &p->work);
if (p->work == 's')
{
printf("\n输入分数:");
scanf("%f", &p->score);
}
else if (p->work == 't')
{
printf("\n输入职务:");
scanf("%s", p->postion);
}
}
void output(person p)
{
if (p == NULL)
{
printf("输出失败\n");
}
printf("%s\t%c\t%c\t", p->name, p->sex, p->work);
if (p->work == 's')
{
printf("%f\n", p->score);
}
else if (p->work == 't')
{
printf("%s\n", p->postion);
}
}
void main()
{
person p = (person)malloc(sizeof(Person));
if (p == NULL)
{
printf("创建失败\n");
}
input(p);
printf("name\tsex\twork\tsco or pos\n");
output(p);
free(p);
p = NULL;
}