鱼C论坛

 找回密码
 立即注册
查看: 2277|回复: 0

[见证历程] 课后作业

[复制链接]
发表于 2018-3-7 15:14:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct date)


struct date
{
        int num;
        char name[20];
        char sex;
        char job;
        union position {
                int clas;
                char prof[20];
                }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;

        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-7 02:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表