android-nuc / 18-C-Train

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

许志茹 宿舍管理系统 #34

Closed xzr912 closed 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;
    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);
    //输入两组数据后,再输入下组数据时,先输入两个空格,结束时不需要空格(不知道为什么,请解答 !)
    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;
    char x;
    printf("请输入学号、姓名、年龄、性别、宿舍号、床号(以0 0 0 0 0 0 0录入结束):\n");
    pt = creat();
    getchar();
    printf("请输入要进行的操作\n");
    x=getchar();
    if(x=='i')//插入
    {
        printf("请输入要插入学生的信息:\n");
        p=(struct Stu*)malloc(sizeof(struct Stu));
        scanf("%d%s%d%c%d%d",&p->id,&p->name,&p->age,&p->sex,&p->dor_num,&p->bed_num);
        pt=insert(pt,p);
        print(pt);
    }
    else if(x=='d')//删除
    {
        pt=del(pt);
        print(pt);
    }
    else if(x=='m')//查询
    {
        inq(pt);
    }
    sto(pt);
    print(pt);
    return 0;
}
wmpscc commented 5 years ago

点评:


Process exited after 63.21 seconds with return value 0 Press any key to continue . . .


> 修改!
xzr912 commented 5 years ago

请输入学号、姓名、年龄、性别、宿舍号、床号(以0 0 0 0 0 0 0录入结束): 101 yi 12x 216 2 103 yang 14d 218 2 0 0 0 0 0 0 0 请输入要进行的操作 i 请输入要插入学生的信息: 102 qian 16c 217 1 姓名:yi 学号:101 年龄:12 性别:x 宿舍号:216 床号:2 姓名:qian 学号:102 年龄:16 性别:c 宿舍号:217 床号:1 姓名:yang 学号:103 年龄:14 性别:d 宿舍号:218 床号:2 成功写入 姓名:yi 学号:101 年龄:12 性别:x 宿舍号:216 床号:2 姓名:qian 学号:102 年龄:16 性别:c 宿舍号:217 床号:1 姓名:yang 学号:103 年龄:14 性别:d 宿舍号:218 床号:2 请按任意键继续. . .