简单的学生管理系统

还记得大一时有个课外的设计--学生管理系统,使用语言是C.当时不会写,拿了同学(好像是王义勇的~~~嘿嘿,勇哥)写的就忽悠了老师.(老师容易忽悠也是没写的一个原因~hoho).

没想到昨天有个朋友发短消息过来,叫我帮忙写下作业-学生管理系统~~还只能用C写。不能用C++,更不用说Java了。本来看看作业说明还挺简单的,不过写起来才发现不习惯C的IO机制,而且不能
用vector,只能采用定长的数组~~~ hoho,最后还是写完了,蛮累的,因为是作业,所以写的比较快,不怎么考虑实现细节。

 

要注意的地方:

1  在使用scanf前最好清空下标准的输入流 .

                    fflush(stdin);
                    printf("请输入姓名:");
                    char name[20];
                    scanf("%s",&name);

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LENTH_IN_CLASS 1000

#define COMMAND_NEW  1
#define COMMAND_ADD  2
#define COMMAND_DELETE 3
#define COMMAND_SEARCH 4
#define COMMAND_SORT  5
#define COMMAND_SEARCHALL 6
#define COMMAND_QUIT  7

 

 

//学生
struct Student {
    long id;
    char* name;
    int mathResult;
    int progResult;
};

struct StudentRecord {
    Student student;
    bool exist;
};


//档案
StudentRecord records[MAX_LENTH_IN_CLASS];

 

 

//初始化班级,填充一些数据
void initStudentRecord() {
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        records[i].student.id = -1;
  records[i].student.mathResult = -1;
  records[i].student.progResult = -1;
        records[i].exist = false;
    }

    records[0].student.id = 1022423;
    records[0].student.name = strdup("八红");
    records[0].student.mathResult = 100;
    records[0].student.progResult = 100;
    records[0].exist = true;
    records[1].student.id = 1022424;
    records[1].student.name = strdup("扬扬");
    records[1].student.mathResult = 40;
    records[1].student.progResult = 80;
    records[1].exist = true;
    records[2].student.id = 1022425;
    records[2].student.name = strdup("猪头冯");
    records[2].student.mathResult = 89;
    records[2].student.progResult = 90;
    records[2].exist = true;
    records[3].student.id = 1022426;
    records[3].student.name = strdup("涂马路");
    records[3].student.mathResult = 9;
    records[3].student.progResult = 8;
    records[3].exist = true;
 records[4].student.id = 1022428;
    records[4].student.name = strdup("你翠");
    records[4].student.mathResult = 79;
    records[4].student.progResult = 89;
    records[4].exist = true;
 records[5].student.id = 1022429;
    records[5].student.name = strdup("强牛");
    records[5].student.mathResult = 100;
    records[5].student.progResult = 99;
    records[5].exist = true;
 records[6].student.id = 1022430;
    records[6].student.name = strdup("胡子");
    records[6].student.mathResult = 99;
    records[6].student.progResult = 100;
    records[6].exist = true;
 
}

//#################查询档案#############################################
void showRecords() {
    printf("/n/n/n[档案查询]/n");
    printf("------------------------------------------------------------------/n");
    printf("学生档案信息/n");
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == true) {
            printf("学号:%8d      姓名:%10s       数学:%3d分      编程:%3d分/n",records[i].student.id,
                   records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
        }
    }
    printf("------------------------------------------------------------------/n");
}
//###################################################################

 

 


//#################增加记录#############################################
bool  isExistID(int newID) {
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == true) {
            if (records[i].student.id == newID) {
                return true;
            }
        }
    }
    return false;
}


void addRecord() {
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == false) {
            bool goodRecord = false;
            while (!goodRecord) {
                fflush(stdin);
                int studentID;
                printf("/n-------------------------");
                printf("/n请输入学号:");
                scanf("%d",&studentID);
                bool exist = isExistID(studentID);
                if (exist) {
                    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
                    printf("学号相同!!请重新输入!/n");
                    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
                } else {
                    records[i].student.id = studentID;
                    fflush(stdin);
                    printf("请输入姓名:");
                    char name[20];
                    scanf("%s",&name);
                    records[i].student.name = strdup(name);
                    int mathResult;
                    fflush(stdin);
                    printf("请输入数学成绩:");
                    scanf("%d",&mathResult);
                    records[i].student.mathResult = mathResult;
                    int progResult;
                    fflush(stdin);
                    printf("请输入编程成绩:");
                    scanf("%d",&progResult);
                    records[i].student.progResult = progResult;
                    records[i].exist = true;
                    goodRecord = true;
                }
            }
            printf("/n已成功添加记录:   学号:%d 姓名:%s 数学:%d分 编程:%d/n分",records[i].student.id,
                   records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
            printf("-------------------------------------/n");
            return;
        }
    }
}

void enterAddRecord() {
    int ADD = 1;
    int SHOW = 2;
    int QUIT = 3;

    int d;
    while (d!=QUIT) {
        printf("/n/n/n子菜单---");
        printf("[添加新记录]/n");
        printf("1 [添加]/n");
        printf("2 [查询档案]/n");
        printf("3 [返回主菜单]/n");
        printf("请输入命令:");
        scanf("%d",&d);
        if (d == ADD) {
            addRecord();
        } else if (d == SHOW) {
            showRecords();
        }
    }
}

//###################################################################

 

 

//#################删除记录#############################################
int  findIndex(int oldID) {
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == true) {
            if (records[i].student.id == oldID) {
                return i;
            }
        }
    }
    return -1;
}

void deleteID(int oldID) {
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == true) {
            if (records[i].student.id == oldID) {
                records[i].exist = false;
                //此处如果不用free函数,将有内存泄露,records[i].student.name所指向的char*需要释放!!!
                free(records[i].student.name);
            }
        }
    }
}

void deleteRecord() {
    int studentID;
    printf("/n-------------------------");
    printf("/n请输入学号:");
    fflush(stdin);
    scanf("%d",&studentID);
    bool exist = isExistID(studentID);
    if (!exist) {
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
        printf("没有此学号!!请重新查询档案/n");
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
    } else {
        int i = findIndex(studentID);
        printf("/n已成功删除记录:   学号:%d 姓名:%s 数学:%d分 编程:%d/n分",records[i].student.id,
               records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
        printf("-------------------------------------/n");
        deleteID(studentID);
    }
}


void enterDeleteRecord() {
    int DELETE = 1;
    int SHOW = 2;
    int QUIT = 3;

    int d;
    while (d!=QUIT) {
        printf("/n/n/n子菜单---");
        printf("[删除记录]/n");
        printf("1 [删除]/n");
        printf("2 [查询档案]/n");
        printf("3 [返回主菜单]/n");
        printf("请输入命令:");
        scanf("%d",&d);
        if (d == DELETE) {
            deleteRecord();
        } else if (d == SHOW) {
            showRecords();
        }
    }
}
//###################################################################

 

 


//#################搜索记录#############################################

 


void searchRecord() {
    int studentID;
    printf("/n/n/n请输入要搜索的学号:");
    fflush(stdin);
    scanf("%d",&studentID);
    bool exist = isExistID(studentID);
    if (!exist) {
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
        printf("没有此学号!!请重新查询档案/n");
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
    } else {
        int i = findIndex(studentID);
        printf("-------------------------------------/n");
        printf("/n搜索的结果:   /n学号:%d /b/n姓名:%s /n数学:%d /n编程:%d/n/n",records[i].student.id,
               records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
        printf("-------------------------------------/n");
    }
}

 

void enterSearchRecord() {
    int SEARCH = 1;
    int QUIT = 2;

    int d;
    while (d!=QUIT) {
        printf("/n/n/n子菜单---");
        printf("[搜索记录]/n");
        printf("1 [搜索]/n");
        printf("2 [返回主菜单]/n");
        printf("请输入命令:");
        scanf("%d",&d);
        if (d == SEARCH) {
            searchRecord();
        }
    }
}
//#####################################################################

 


//#################记录排序#############################################

#define SORT_BY_ID 1
#define SORT_BY_MATH 2
#define SORT_BY_PROG 3

//冒泡排序
void Bubble_sort(int SORT) {
 StudentRecord t;
 int i,j;
    if (SORT == SORT_BY_ID) {
        for (j=0;j<MAX_LENTH_IN_CLASS;j++) {
            for (i=0;i<MAX_LENTH_IN_CLASS-j-1;i++) {
                if (records[i].student.id>records[i+1].student.id) {
                    t=records[i];
                    records[i]=records[i+1];
                    records[i+1]=t;
                }
            }
        }
    }else if (SORT== SORT_BY_MATH) {
        for (j=0;j<MAX_LENTH_IN_CLASS;j++) {
            for (i=0;i<MAX_LENTH_IN_CLASS-j-1;i++) {
                if (records[i].student.mathResult>records[i+1].student.mathResult) {
                    t=records[i];
                    records[i]=records[i+1];
                    records[i+1]=t;
                }
            }
        }
    }else if (SORT== SORT_BY_PROG) {
        for (j=0;j<MAX_LENTH_IN_CLASS;j++) {
            for (i=0;i<MAX_LENTH_IN_CLASS-j-1;i++) {
                if (records[i].student.progResult>records[i+1].student.progResult) {
                    t=records[i];
                    records[i]=records[i+1];
                    records[i+1]=t;
                }
            }
        }
    }
}


void sortRecordsByID() {
    int studentID;
    printf("/n/n/n请输入要搜索的学号:");
    fflush(stdin);
    scanf("%d",&studentID);
    bool exist = isExistID(studentID);
    if (!exist) {
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
        printf("没有此学号!!请重新查询档案/n");
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
    } else {
        int i = findIndex(studentID);
        printf("-------------------------------------/n");
        printf("/n搜索的结果:   /n学号:%d /b/n姓名:%s /n数学:%d /n编程:%d/n/n",records[i].student.id,
               records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
        printf("-------------------------------------/n");
    }
}

void showSortRecords() {
    printf("/n[排序结果]/n");
    printf("------------------------------------------------------------------/n");
    printf("学生档案信息/n");
    for (int i=0; i<MAX_LENTH_IN_CLASS; i++) {
        if (records[i].exist == true) {
            printf("学号:%8d      姓名:%10s       数学:%3d分      编程:%3d分/n",records[i].student.id,
                   records[i].student.name,records[i].student.mathResult,records[i].student.progResult);
        }
    }
    printf("------------------------------------------------------------------/n");
}

void enterSortRecords() {
    int QUIT = 4;

    int d;
    while (d!=QUIT) {
        printf("/n/n子菜单---");
        printf("[搜索记录]/n");
        printf("1 [按学号排序]/n");
        printf("2 [按数学成绩排序]/n");
        printf("3 [按编程成绩排序]/n");
        printf("4 [返回主菜单]/n");
        printf("请输入命令:");
        scanf("%d",&d);
        if (d == SORT_BY_ID) {
            Bubble_sort(SORT_BY_ID);
   showSortRecords();
        } else if (d == SORT_BY_MATH) {
   Bubble_sort(SORT_BY_MATH);
   showSortRecords();
  }
        else if (d == SORT_BY_PROG) {
   Bubble_sort(SORT_BY_PROG);
   showSortRecords();
  }
    }
}
//#####################################################################

 

//解析用户输入的命令
void parseCommand(int command) {
    if (command==COMMAND_SEARCHALL) {
        showRecords();
    } else if (command == COMMAND_ADD) {
        enterAddRecord();
    } else if (command == COMMAND_DELETE) {
        enterDeleteRecord();
    } else if (command == COMMAND_SEARCH) {
        enterSearchRecord();
    } else if (command == COMMAND_NEW) {
        printf("/n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
        printf("档案已存在,请直接操作!/n");
        printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n/n");
    } else if (command == COMMAND_SORT) {
        enterSortRecords();
    }
}

//main
int main() {
    //初始化
    initStudentRecord();


    //开始主循环
    int command;
    printf("学生管理系统1.00/n");
    while (command!=COMMAND_QUIT) {
        printf("/n/n/n######################/n");
        printf("主菜单/n");
        printf("1 [创建档案]/n");
        printf("2 [添加新记录]/n");
        printf("3 [删除记录]/n");
        printf("4 [搜索记录]/n");
        printf("5 [档案排序]/n");
        printf("6 [档案查询]/n");
        printf("7 [离开]/n");
        printf("请输入命令:");
        scanf("%d",&command);
        if (command!=COMMAND_QUIT) {
            parseCommand(command);
        }
    }

    printf("/n再见");
    return 0;
}

 

 


 

你可能感兴趣的:(简单的学生管理系统)