学生信息管理系统

/*编译环境为vc++6.0----VS2015*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include<ctype.h>
struct list {
    char ID[20];
    char name[20];
    char gender[5];
    float score;
    struct list *next;
};
                                        /*文件指针*/
char *filename ="'C:\\Users\\ASUS\\Desktop\\myfile.txt";/*文件存储路径*/
int re_ID = 0;                                          /*为了检验是否经过了创建学生信息的检查*/
struct list *List = NULL;                               /*表头*/
struct list *create();                                  /*创建学生链表*/
struct list *Find_Previous(struct list *p);             /*为了删除而使用的查找函数*/
struct list *Find_Previously(struct list *p);           /*为了添加而使用的查找函数*/
struct list *Selectsort(struct list *L);                /*插入排序--按照成绩进行排序*/
void Print();                                           /*打印菜单*/
void Print_Query();                                     /*打印查询菜单*/
void Exit();                                            /*退出*/
void Querying();                                        /*查询*/
void Help();                                            /*帮助手册*/
void Display(struct list *p);                           /*打印表*/
void Save(struct list *pp);                             /*将链表写入文件中*/
void Query_ID(struct list *p);                          /*按学号查询*/
void Query_Name(struct list *p);                        /*按姓名查询*/
void Query_Gender(struct list *p);                      /*按性别查询*/
void Query_Score(struct list *p);                       /*按成绩查询*/
void Addition(struct list *p);                          /*添加学生信息*/
void Delete(struct list *p);                            /*删除学生的信息*/
void Rebuild(struct list *p);                           /*删除所有信息*/
int  check_ID(char ID[]);                               /*检查学号的输入正确性*/
int main(void)  
{
    char choice = '\0';
    while (1) {
        system("cls");
        fflush(stdin);
        Print();
        scanf("%c", &choice);
        switch (choice) {
        case'1':
            List = create();
            break;
        case'2':
            Querying();
            break;
        case'3':
            Selectsort(List);
            break;
        case'4':
            Display(List);
            break;
        case'5':
            Exit();
            break;
        case'6':
            Help();
            break;
        case'7':
            Save(List);
            break;
        case'8':
            Addition(Find_Previous(List));
            break;
        case'9':
            Delete(Find_Previous(List));
            break;
        case'0':
            Rebuild(List);
            break;
        default:
            printf("没有此选择项,请重新输入!\n");
            break;
        }
    }
    return 0;
}

struct list *create() {
    char ch = '\0';
    struct list *head = NULL;
    struct list *current = NULL;
    struct list *previous = NULL;
    while (1) {
        printf("请问是否输入学生信息?(Y or N)\n");
        scanf(" %c", &ch);
        if (tolower(ch) == 'n') {
            puts("你选择了取消输入!\n");
            system("pause");
            break;
        }
        current = (struct list *)malloc(sizeof(struct list));

        if (head == NULL)
            head = current;
        if (previous != NULL)
            previous->next = current;

        puts("请输入学生姓名:");
        scanf("%s", current->name);
        while (1) {
            puts("请输入学生学号:");
            fflush(stdin);
            scanf("%s", current->ID);
            if (check_ID(current->ID) == 0)
                break;
            else
                puts("你输入的格式错误,请重新输入!\n");
        }
        while (1) {
            puts("请输入学生的性别:(注意:boy or girl)");
            fflush(stdin);
            scanf(" %s", current->gender);
            if (strcmp(current->gender, "boy") == 0 || strcmp(current->gender, "girl") == 0)
                break;
            else
                puts("你输入的格式错误,请重新输入!\n");
        }
        while (1) {
            printf("请输入学生的成绩:\n");
            fflush(stdin);
            if (scanf("%f", &current->score)!=EOF&&current->score>=0.0)
                break;
            else
                printf("你输入的格式错误,请重新输入!\n");
                fflush(stdin);
        }
        current->next = NULL;
        previous = current;
    }
    re_ID++;
    return (head);
}

void Exit() {   /*这个是用来退出*/
    int i = 0;
    printf("退出中");
    for (i = 4; i > 0; --i) {
        Sleep(200);
        printf(".");
    }
    exit(0);
}
void Save(struct list *pp)
{
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *head = NULL;
    head = pp;
    FILE *fp = NULL;
    if (!(fp = fopen(filename, "wb"))) {
        fprintf(stderr,"\n打开失败\n");
        system("pause");
        return;
    }
    while (head->next != NULL) {
        fwrite(head,sizeof(struct list),1,fp);
        head = head->next;
    }
    fclose(fp);
    printf("数据读入文件完毕\n");
    system("pause");
}


void Display(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    temp = p;
    while (temp != NULL) {
        printf("-----/*姓名*/--------/*学号*/-----/*性别*/-----/*成绩*/-----\n");
        printf("       %s              %s           %s            %.2f\n", temp->name, temp->ID,temp->gender,temp->score);
        temp = temp->next;
    }
    system("pause");
}
void Print() {
    printf("-----主菜单功能如下:\n");
    printf("-----1.输入学生信息\n");
    printf("-----2.查询\n");
    printf("-----3.排序\n");
    printf("-----4.打印学生信息\n");
    printf("-----5.退出\n");
    printf("-----6.帮助\n");
    printf("-----7.数据写入文件\n");
    printf("-----8.插入学生信息\n");
    printf("-----9.删除学生信息\n");
    printf("-----0.重建学生信息\n");
    printf("请输入你的选择!\n");
}
void Help() {
    puts("Ver3.0-学生信息管理系统\n");
    puts("由于本人是新手,所以一定会有很多的问题\n");
    puts("如果能告诉我,在下不胜感激\n");
    system("pause");
}
void Print_Query() {
    printf("-----查询菜单功能如下:\n");
    printf("-----1.按学号查询\n");
    printf("-----2.按姓名查询\n");
    printf("-----3.按性别查询\n");
    printf("-----4.按成绩查询\n");
    printf("-----5.退出查询\n");
    printf("请输入你的选择:\n");
}
void Querying() {
    char cho = '\0';
    while (1) {
        system("cls");
        fflush(stdin);
        Print_Query();
        scanf("%c", &cho);
        switch (cho) {
        case'1':
            Query_ID(List);
            break;
        case'2':
            Query_Name(List);
            break;
        case'3':
            Query_Gender(List);
            break;
        case'4':
            Query_Score(List);
            break;
        case'5':
            return;
            break;
        default:
            printf("无此选择项!\n");
            system("pause");
            break;
        }
    }
}
void Query_Name(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    char na[20];
    temp = p;
    printf("请输入你想要查询的姓名:\n");
    scanf("%s", na);
    while (temp != NULL) {
        if (strcmp(temp->name, na) == 0) {
            printf("你的查找如下:\n");
            printf("-----/*姓名*/--------/*学号*/-----/*性别*/-----/*成绩*/-----\n");
            printf("        %s              %s          %s            %.2f\n", temp->name, temp->ID, temp->gender, temp->score);
            temp = temp->next;
        }
        else
            temp = temp->next;
    }
    system("pause");
}
void Query_ID(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    char id[20];
    temp = p;
    while (1) {
        printf("请输入你想要查询的学号:\n");
        fflush(stdin);
        scanf("%s", id);
        if (check_ID(id) == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!");
    }
    while (temp != NULL) {
        if (strcmp(temp->ID, id) == 0) {
            printf("你的查找如下:\n");
            printf("-----/*姓名*/--------/*学号*/-----/*性别*/-----/*成绩*/-----\n");
            printf("       %s              %s          %s            %.2f\n", temp->name, temp->ID, temp->gender, temp->score);
            temp = temp->next;
        }
        else
            temp = temp->next;
    }
    system("pause");
}
void Query_Gender(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    char ge[5];
    temp = p;
    while (1) {
        puts("请输入学生的性别:(注意:boy or girl)");
        fflush(stdin);
        scanf(" %s", ge);
        if (strcmp(ge, "boy") == 0 || strcmp(ge, "girl") == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (temp != NULL) {
        if (strcmp(temp->gender,ge) == 0) {
            printf("你的查找如下:\n");
            printf("-----/*姓名*/--------/*学号*/-----/*性别*/-----/*成绩*/-----\n");
            printf("       %s              %s          %s            %.2f\n", temp->name, temp->ID, temp->gender, temp->score);
            temp = temp->next;
        }
        else
            temp = temp->next;
    }
    system("pause");
}
void Query_Score(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    float sc = 0;
    temp = p;
    while (1) {
        puts("请输入学生的成绩:");
        fflush(stdin);
        if (scanf("%f", &sc) != EOF&&sc >= 0.0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (temp != NULL) {
        if (temp->score == sc) {
            printf("你的查找如下:\n");
            printf("-----/*姓名*/--------/*学号*/-----/*性别*/-----/*成绩*/-----\n");
            printf("       %s             %s          %s            %.2f\n", temp->name, temp->ID, temp->gender, temp->score);
            temp = temp->next;
        }
        else
            temp = temp->next;
    }
    system("pause");
}

struct list *Find_Previously(struct list *p){
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return NULL;
    }
    struct list *temp = NULL;
    temp = p;
    char iidd[20];
    while (1) {
        printf("请输入你想要查询的学号:(注意:第一条信息之前不能插入信息)\n");
        fflush(stdin);
        scanf("%s", iidd);
        if (check_ID(iidd) == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (temp != NULL) {
        if (strcmp(temp->ID, iidd) == 0) {
            break;
        }
        else
            temp = temp->next;
    }
    return temp;
}
struct list *Find_Previous(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return NULL;
    }
    struct list *temp = NULL;
    temp = p;
    char iidd[20];
    while (1) {
        Display(List);
        printf("请输入你想要插入之前的学号:\n");
        fflush(stdin);
        scanf("%s", iidd);
        if (check_ID(iidd) == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (temp != NULL) {
        if (strcmp(temp->next->ID, iidd) == 0) {
            break;
        }
        else
            temp = temp->next;
    }
    return temp;
}

void Delete(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    temp = p->next;
    p->next = temp->next;
    free(temp);
}
void Rebuild(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        return;
    }
    struct list *temp = NULL;
    struct list *tt = NULL;
    temp = p;
    p->next = NULL;
    while (temp != NULL) {
        tt = temp->next;
        free(temp);
        temp = tt;
    }
    puts("重建学生信信息完成!\n");
    re_ID = 0;
    system("pause");
}
void Addition(struct list *p) {
    if (re_ID == 0) {
        printf("请先创建学生信息!\n");
        system("pause");
        return;
    }
    struct list *temp = NULL;
    temp = (struct list *)malloc(sizeof(struct list));
    if (temp == NULL) {
        fprintf(stderr, "空间不足!\n");
        exit(0);
    }
    puts("请输入学生姓名:");
    scanf("%s", temp->name);
    while (1) {
        puts("请输入学生学号:");
        fflush(stdin);
        scanf("%s", temp->ID);
        if (check_ID(temp->ID) == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (1) {
        puts("请输入学生的性别:(注意:boy or girl)");
        fflush(stdin);
        scanf("%s", temp->gender);
        if (strcmp(temp->gender, "boy") == 0 || strcmp(temp->gender, "girl") == 0)
            break;
        else
            puts("你输入的格式错误,请重新输入!\n");
    }
    while (1) {
        printf("请输入学生的成绩:");
        fflush(stdin);
        if (scanf("%f", &temp->score) != EOF&&temp->score >= 0.0)
            break;
        else
            printf("你输入的格式错误,请重新输入!\n");
        continue;
    }
    temp->next = p->next;
    p->next = temp;
}
int check_ID(char ID[]) {
    int i = 0, flag = 0;
    while (ID[i] != '\0') {
        if (ID[i]<'0' || ID[i]>'9') {
            flag = 1;
            break;
        }
        else
            i++;
    }
    return flag;
}

struct list *Selectsort(struct list *L)
{
    float temp;
    struct list *p, *q, *m;
    if (!L->next || !L->next->next)return L;
    p = L->next;
    while (p->next)
    {
        m = p;
        q = p->next;
        while (q)
        {
            if (q->score<m->score)m = q;
            q = q->next;

        }
        if (p != m)
        {
            temp = m->score;
            m->score = p->score;
            p->score = temp;
        }
        p = p->next;
    }

    return L;
}

最后,附上几张测试图:
这个是开始界面
这个是输入界面
这个是查询界面

posted @ 2017-05-22 20:43  Chasssser  阅读(182)  评论(0编辑  收藏  举报