android-nuc / 18-C-Train

18级Android实验室(人工智能+移动互联) C语言培训
16 stars 10 forks source link

许志茹 (宿舍管理系统 没有弄菜单) #22

Open xzr912 opened 5 years ago

xzr912 commented 5 years ago
#include<stdio.h>
#include<stdlib.h>
struct Stu{
    char name[20];
    int id;
    int age;
    char sex;
    int dor_num;
    int bed_num;
    struct Stu *next;
};
int count = 0;
struct Stu *creat()
{
    struct Stu *head = NULL,*end,*pnew;

    printf("请输入学号、姓名、年龄、性别、宿舍号、床入号(以0结录束):\n");
    pnew = (struct Stu*)malloc(sizeof(struct Stu));
    scanf("%d",&pnew->id);
    scanf("%s",&pnew->name);
    scanf("%d",&pnew->age);
    scanf("%c",&pnew->sex);
    scanf("%d",&pnew->dor_num);
    scanf("%d",&pnew->bed_num);
    getchar();
    while(pnew->id != 0)
    {
        count ++;
        if(1 == count)
        {
            pnew->next = head;
            head = pnew;
            end = pnew;
        }
        else
        {
            end->next = pnew;
            end = pnew;
        }
        pnew = (struct Stu*)malloc(sizeof(struct Stu));
        scanf("%d%s%d%c%d%d",&pnew->id,&pnew->name,&pnew->age,&pnew->sex,&pnew->dor_num,&pnew->bed_num);
        getchar();
        getchar();
    }
    end->next = NULL;
    free(pnew);
    return head;
}
void print(struct Stu *head)
{
    struct Stu *p;
    p = head;
    while(p != NULL)
    {
        printf("姓名:%s ",p->name);
        printf("学号:%d ",p->id);
        printf("年龄:%d ",p->age);
        printf("性别:%c ",p->sex);
        printf("宿舍号:%d ",p->dor_num);
        printf("床号:%d\n",p->bed_num);
        p = p->next;
    }
}
void inq(struct Stu *head)
{
    struct Stu *p;
    int num;
    p = head;
    printf("请输入要查询的学号:\n");
    scanf("%d",&num);
    while(p != NULL)
    {
        if(p->id == num)
        {
            printf("姓名:%s ",p->name);
            printf("学号:%d ",p->id);
            printf("年龄:%d ",p->age);
            printf("性别:%c ",p->sex);
            printf("宿舍号:%d ",p->dor_num);
            printf("床号:%d\n",p->bed_num);
            break;
        }
        else
            p = p->next;
    }
}
struct Stu *insert(struct Stu *head,struct Stu *p)
{
    struct Stu *pt,*p1;
    pt = head;
    if(head == NULL)
    {
            head = p;
            p->next = NULL;
    }
    else
    {
        while((pt->id < p->id) && (pt->next != NULL))
        {
            p1 = pt;
            pt = pt->next;
        }
            if(pt->id >= p->id)
            {
                if(pt == head)
                {
                    p->next = head;
                    head = p;
                }
                else
                {
                    p1->next = p;
                    p->next = pt;
                }
            }
        else
            {
            pt->next = p;
            p->next = NULL;
            }
        count++;
    }
    return head;
}
struct Stu *del(struct Stu *head)
{
    struct Stu *p2=NULL,*p1=NULL;
    int num;
    printf("请输入要删除学生的学号:\n");
    scanf("%d",&num);
    p2 = head;
    if(head == NULL)
    {
        printf("空链表\n");
        return (head);
    }
    else
    {
        while((p2->id != num) && (p2->next != NULL))
        {
            p1 = p2;
            p2= p2->next;
        }
        if(p2->id == num)
        {
            if(p2 == head)
            {
                head = p2->next;
            }
            else
            {
                p1->next = p2->next ;
            }
        }
        count--;
    }
    return head;
}
void sto(struct Stu *head)
{
    FILE *fp;
    if((fp=fopen("D:\\dormitory.dat","w")) == NULL)
    {
        printf("打开文件失败\n");
        exit(0);
    }
    fwrite(head,sizeof(struct Stu),count,fp);
    printf("成功写入\n");
}
int main()
{
    struct Stu *pt,*p;
    pt = creat();
    printf("请输入要插入学生的信息:\n");
    p=(struct Stu*)malloc(sizeof(struct Stu));
    scanf("%d",&p->id);
    scanf("%s",&p->name);
    scanf("%d",&p->age);
    scanf("%c",&p->sex);
    scanf("%d",&p->dor_num);
    scanf("%d",&p->bed_num);
    getchar();
    pt = insert(pt,p);
    pt = del(pt);
    inq(pt);
    print(pt);
    sto(pt);
    system("pause");
    return 0;
}
wmpscc commented 5 years ago

点评:

修改!