android-nuc / 18-C-Train

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

王名珺 宿舍管理系统(待改) #26

Closed Dolly5 closed 5 years ago

Dolly5 commented 5 years ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct Student)

struct Student {
    char name[20];
    int ID;
    int age;
    char sex;
    int dorm;
    int num;
    struct Student *next;
};

//void addNode();
struct Student *creat();
void FindID();
struct Student *insert(struct Student *head, struct Student *stud);
struct Student *del(struct Student *head, long long ID);
void print(struct Student *head);
void Menu();

int main()
{
    struct Student *head = NULL, stu;
    long del_ID;
    int menu;
    while (1)
    {   
        int x = 0;
        Menu();
        printf("请输入要进行的操作:");
        scanf_s("%d", &menu);
        switch (menu)
        {
        case 0:
            exit(0);
            break;
        case 1:
            system("cls");
            //struct Student *head;
            printf("请输入学生信息,等输入数据为0时,输入结束:\n");   //错误:跳不出来,无法返回Menu :(
            head = creat();
            print(head);
            system("cls");
            Menu();
            break;
        case 2:
            system("cls");
            FindID();
            system("cls");
            Menu();
            break;
        case 3:
            system("cls");
            printf("input the insert number:");
            scanf_s("%ld", &stu.ID);
            head = insert(head, &stu);
            print(head);
            system("cls");
            Menu();
            break;
        case 4:
            system("cls");
            printf("input the delete number:");
            scanf_s("%ld", &del_ID);
            head = del(head, del_ID);
            print(head);
            system("cls");
            Menu();
            break;
        case 5:
            system("cls");
            print(head);
            system("cls");
            Menu();
        default:
            printf("输入有误!请重新输入!\n");
        }
    }
    return 0;
}

int n;

struct Student *creat(){

        struct Student * head;
        struct Student *p1, *p2;
        n = 0;
        p1 = p2 = (struct Student *)malloc(LEN);
        scanf_s("%s%d%d%s%d%d", p1->name,20, &p1->ID, &p1->age, &p1->sex, sizeof(p1->sex), &p1->dorm, &p1->num);
        head = NULL;
        while (p1->ID != 0) {
            n = n + 1;
            if (n == 1) head = p1;
            else p2->next = p1;
            p2 = p1;
            p1 = (struct Student *)malloc(LEN);
            scanf_s("%s%d%d%s%d%d", p1->name,20, &p1->ID, &p1->age, &p1->sex,sizeof(p1->sex), &p1->dorm, &p1->num);
        }
        p2->next = NULL;
        return (head);
}

void print(struct Student *head){
    struct Student *p;
    printf("\nname:    ID:      age:   sex:   dorm:    num:\n");
    p = head;
    if (head != NULL)
        do {
            printf("%s\t%d\t%d\t%c\t%d\t%d", p->name, p->ID, p->age, p->sex, p->dorm, p->num);
            p = p->next;
        } while (p != NULL);
    else {
        printf("error!\n");
    }
}

void FindID(){
    struct Student *head = NULL;
    struct Student *p ;
    p = head;
    long long studentID = 0;
    if (head == NULL)
    {
        printf("没有数据可供查找!");
        return;
    }
    printf("请输入你要查找的学生ID:");
    scanf_s("%lld", &studentID);
    while (studentID != p->ID && p->next != NULL)
    {
        p = p->next;
    }
    if (studentID == studentID)
    {
        printf("%s", p->name);
        printf("%11d", p->ID);
        printf("%5d", p->age);
        printf("%c", p->sex);
        printf("%8d", p->dorm);
        printf("%5d", p->num);
    }
    else {
        printf("没有你要查找的人!");
    }
    return;
}

struct Student *insert(struct Student *head, struct Student *stud) {
    struct Student *p0, *p1, *p2 = NULL;
    p1 = head;
    p0 = stud;
    if (head == NULL) {
        head = p0; p0->next = NULL;
    }
    else {
        while ((p0->num > p1->num) && (p1->next != NULL)) {
            p2 = p1;
            p1 = p1->next;
        }
        if (p0->num <= p1->num) {
            if (head == p1) head = p0;
            else p2->next = p0;
            p0->next = p1;
        }
        else {
            p1->next = p0; p0->next = NULL;
        }
    }
    n = n + 1;
    return (head);
}

struct Student *del(struct Student *head, long long ID) {
    struct Student *p1, *p2 = NULL;
    if (head == NULL) {
        printf("\nList NULL!\n");
        return (head);
    }
    p1 = head;
    while (ID != p1->ID && p1->next == NULL) {
        p2 = p1; p1 = p1->next;
    }
    if (ID == p1->ID) {
        if (p1 == head) head = p1->next;
        else p1->next = p2->next;
        printf("delete:%lld\n", ID);
        n = n - 1;
    }
    else printf("%lld not been found!\n", ID);
    return (head);
}

void Menu(){ 
    printf("\t 0.返回学生信息表。\n");
    printf("\t 1.输入学生信息。\n");
    printf("\t 2.查询学生(按学号)信息。\n");
    printf("\t 3.插入学生信息(按学号)。\n");
    printf("\t 4.删除学生成绩(按学号)信息。\n");
    printf("\t 5.输出学生信息。\n");
}
wmpscc commented 5 years ago

点评:

修改!