用顺序表完成简单的学生成绩管理系统

来源:互联网 发布:单片机方案开发 编辑:程序博客网 时间:2024/05/29 06:38

“舜发于畎亩之中,傅说举于版筑之中,胶鬲举于鱼盐之中,管夷吾举于士,孙叔敖举于海,百里奚举于市。”


#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>#include<conio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -1#define LIST_INCREMENT 2#define LIST_INIT_SIZE 10typedef int Status;typedef int ElemType;typedef struct{    int no;    char name[10];    char sex;    int eng;    int math;} stu;//以结构体为基本单元 大概就是stu替代之前的ElemTypetypedef struct{    stu *elem;    int length;    int listsize;} SqList;void InitStuList(SqList *L){    (*L).elem = (stu *) malloc(LIST_INIT_SIZE * sizeof(stu));    if(!(*L).elem)        exit(OVERFLOW);    (*L).length = 0;    (*L).listsize = LIST_INIT_SIZE;}Status StuListInsert(SqList *L, int i, stu e){    stu *newbase, *p, *q;    if(i < 1 || i > (*L).length + 1)        return ERROR;    if((*L).length >= (*L).listsize)    {        newbase = (stu*)realloc((*L).elem, ((*L).listsize + LIST_INCREMENT) * sizeof(stu));        if(!newbase)            exit(OVERFLOW);        (*L).elem = newbase;        (*L).listsize += LIST_INCREMENT;    }    q = (*L).elem + i - 1;    for(p = (*L).elem + (*L).length - 1; p >= q; --p)        *(p + 1) = *p;    *q = e;    ++(*L).length;    return OK;}void StuInforAdd(SqList *L){    //录入学生信息    stu e;    printf("录入学生信息\n结束录入输入‘0’\n依次输入学号  姓名  性别  大学英语成绩  高等数学成绩\n");    while(1)    {        scanf("%d", &e.no);        if(e.no == 0)            break;        scanf("%*c%s%*c%c%*c%d%d", &e.name, &e.sex, &e.eng, &e.math);        StuListInsert(L, 1, e);    }}Status equal_no(int c1, int c2){    //比较学号    if(c1 == c2)        return TRUE;    else        return FALSE;}int StuInforQuery(SqList L, int e, Status(*compare)(int, int)){    //根据学号查询    stu *p, *q;    int i = 1;    p = L.elem;    while(i <= L.length && !compare((*p++).no, e))        ++i;    if(i <= L.length)    {        //printf("查找成功\n");        q = L.elem + i - 1;        printf("学号:%d  姓名:%s  性别:%c  大学英语:%d  高等数学:%d\n", (*q).no, (*q).name, (*q).sex, (*q).eng, (*q).math);        return i;    }    else        printf("查找失败\n");    return 0;}void StuInforChange(SqList *L, int i){    //修改学生信息    stu *p = (*L).elem + StuInforQuery(*L, i, equal_no) - 1;    if(p >= (*L).elem)    {        printf("输入新的学生成绩");        printf("\n大学英语");        scanf("%d", &(*p).eng);        printf("高等数学");        scanf("%d", &(*p).math);    }}Status StuInforDelete(SqList *L, int i){    //删除学生信息    stu *p, *q;    i = StuInforQuery(*L, i, equal_no);    if(i < 1 || i > (*L).length)        return ERROR;    p = (*L).elem + i - 1;    q = ((*L).elem + (*L).length - 1);    for(++p; p <= q; ++p)        *(p - 1) = *p;    (*L).length--;    return OK;}void StuSort(SqList *L){    int i, j;    stu *p, *q;    for(i = 1; i < (*L).length; i++)    {        for(j = 0; j <= (*L).length - i - 1; j++)        {            p = (*L).elem + j;            q = (*L).elem + j + 1;            if((*p).no > (*q).no)            {                stu temp;                temp = *p;                *p = *q;                *q = temp;            }        }    }}void StuPrintf(stu *q){    printf("%d %s %c %d %d\n", (*q).no, (*q).name, (*q).sex, (*q).eng, (*q).math);}Status StuListTraverse(SqList L, void(*vi)(stu*)){    StuSort(&L);    stu *p;    int i;    p = L.elem;    for(i = 1; i <= L.length; i++)        vi(p++);    printf("\n");    return OK;}int main(){    SqList L;    int i;    InitStuList(&L);    StuInforAdd(&L);    printf("按任意键进行查询操作");    getch();    system("cls");    printf("结束查询输入0\n");    while(1)    {        printf("\n输入要查询的学号\n");        scanf("%d", &i);        if(i == 0) break;        StuInforQuery(L, i, equal_no);    }    printf("按任意键进行修改操作\n");    getch();    system("cls");    printf("结束修改输入0\n");    while(1)    {        printf("\n输入要修改的学号");        int i;        scanf("%d", &i);        if(i == 0) break;        printf("查找学号为:%d学生信息\n", i);        StuInforChange(&L, i);    }    printf("修改后的学生信息\n");    StuListTraverse(L, StuPrintf);    printf("按任意键进行插入操作");    getch();    system("cls");    printf("结束插入操作输入0\n");    while(1)    {        stu e;        printf("输入插入学生的信息(默认插在首位)\n");        scanf("%d", &e.no);        if(e.no == 0)            break;        scanf("%*c%s%*c%c%*c%d%d", &e.name, &e.sex, &e.eng, &e.math);        StuListInsert(&L, 1, e);    }    printf("插入后的学生信息\n");    StuListTraverse(L, StuPrintf);    printf("按任意键进行删除操作\n");    getch();    system("cls");    printf("结束删除输入0\n");    while(1)    {        int i;        printf("输入要删除的学号");        scanf("%d", &i);        if(i == 0) break;        printf("查找这名学生信息");        Status flag;        flag = StuInforDelete(&L, i);        if(flag == 0)            break;    }    printf("删除后的学生信息\n");    StuListTraverse(L, StuPrintf);    return 0;}

之前的ElemType指的是整型数据类型,现在改变为结构体数组;
ElemType等价于stu
由于在c语言中结构体可以进行赋值元素这样大大简化的代码量。
易错点就是函数参数有时是指针类型,有时不是…思绪严谨一点就没问题啦!!!

原创粉丝点击