zoukankan      html  css  js  c++  java
  • 学生成绩管理系统

         第一次用线性表充满了恐惧,新的东西,用的总是不顺手,加上老师布置的学生管理系统(系统....听着都头大,还用顺序表),不过任务已经有啦,硬着头皮也要上。

    随便到网上找了下实现的基本结构,还是没有什么头绪,依旧到楼下简单吃了一碗面(三四天没有吃饭,表示很受伤啊)

          不说太多,时间只有一天,七点吃完干活。一下是劳动成果

    include<stdio.h>
    #include<stdlib.h>
    #define MAXSIZE 50

    /****************************************顺序表的创建以及相关操作的实现,参考了内部讲义,省了好多时间哈哈,不过还是要理解好********************************************************/

    typedef struct student
    {
    char name[10];
    int num;
    char sex[5];
    int english;
    int math;
    int total ;
    int rank; //为什么不能初始化
    }student_t;

    typedef struct
    {
    student_t stu[MAXSIZE];
    int length;
    }seqlist;

    //初始化顺序表seqlist * init_list(){ seqlist *list; list=(seqlist*)malloc(sizeof(seqlist));    //为整个顺序表分配内存空间 list->length=0;                                      // 长度初始化为0 return (list);}


    //顺序表的插入
    int insert_list(seqlist *plist, int i, student_t stu)
    {
    int j;
    if(plist->length ==MAXSIZE)
    {
    printf("the list is full !!\n");
    }
    if(i<0 ||i>MAXSIZE)
    {
    printf("the position is error !!\n");
    return -1;
    }
    for(j=plist->length-1; j>=i; j--)
    {
    plist->stu[j+1]=plist->stu[j];
    }
    plist->stu[i]=stu;
    plist->length++;
    return 0;
    }


    //删除数据元素
    int delete_list(seqlist *plist, int i)
    {
    int j;
    if(i<0 ||i>MAXSIZE)
    {
    printf("the position is error !!\n");
    return -1;
    }
    for(j=i+1; j<=plist->length-1; j++)
    {
    plist->stu[j-1] = plist->stu[j];
    }
    plist->length--;
    return 0;
    }

    //以姓名查找学生,返回位置
    int search_list(seqlist *plist, char arrary[])
    {
    int i;
    for(i=0; i<plist->length; i++)
    {
    if(strcmp(plist->stu[i].name, arrary)==0)
    break;
    }
    if(i>plist->length-1)
    return -1;
    else
    return i;
    }
    /**********************************1、input***************************************/
    int inputinfo(seqlist *plist)
    {
    student_t stu;
    while(1)
    {
    printf("*******************please input the one student info********************\n");
    printf("student no. :");
    scanf("%d",&stu.num);
    if(stu.num==0)     //当输入学号是0 的时候,退出输入模块
    {
    printf("quit now !\n");
    break;
    }

    printf("student name :");
    scanf("%s",stu.name);

    printf("student sex :");
    scanf("%s",stu.sex);

    printf("student english score :");
    scanf("%d",&stu.english);

    printf("student math score :");
    scanf("%d",&stu.math);

    stu.total = stu.english + stu.math;     //总成绩用english和math成绩相加得到,不用输入
    stu.rank = 0;            //每个学生的排名都初始化为0,排序后面再实现

    insert_list(plist, plist->length, stu);
    }
    return 0;
    }
    /***************************************2、display***************************************************/
    void display_info(seqlist *plist)
    {
    int i;
    printf("\n");
    printf("the student information :\n");
    printf("num name sex english math total rank\n");
    for(i=0; i<plist->length; i++)                           //依次输出顺序表中每个学生的相关信息,注意界面排版,,,
    {
    printf("%d\t%s\t %s\t ",plist->stu[i].num,plist->stu[i].name,plist->stu[i].sex);
    printf("%d\t\t%d\t %d\t %d\n",plist->stu[i].english,plist->stu[i].math,plist->stu[i].total,plist->stu[i].rank);
    }
    }
    /*************************************3、sort**************************************************/
    void sort_info(seqlist *plist)
    {
    int i,j,k;
    student_t temp;
    for(i=0; i<plist->length-1; i++)        //经典选择法,越用越好用,
    {
    k=i;
    for(j=i+1; j<plist->length; j++)
    {
    if(plist->stu[i].total < plist->stu[j].total)
    k=j;
    }
    if(k!= j)
    {
    temp = plist->stu[i];
    plist->stu[i]= plist->stu[k];
    plist->stu[k] = temp;
    }
    plist->stu[i].rank=i+1;                         //给学生排名赋值,
    plist->stu[plist->length-1].rank=plist->length;    //给最后一个学生排名赋值,因为选择法没有操作到最后一个,所以要特别照顾,排名是顺序表的长度
    }
    display_info(plist);
    }
    /***************************************4、search**************************************************/
    int search_info(seqlist *plist)
    {
    int n;
    char arr[10];
    printf("please input the name :");
    scanf("%s",arr);
    n = search_list(plist, arr);          //调用顺序表的查找方法,返回符合姓名条件的学生    其在顺序表中的位置
    if(n<0)                                               //根据返回值可以判断是否找到
    {
    printf("can not find student !\n");                      
    }
    else
    {
    printf("num name sex english math total rank\n");
    printf("%d\t%s\t %s\t ",plist->stu[n].num,plist->stu[n].name,plist->stu[n].sex);                                         //依次输出查找到的学生的相关信息,
    printf("%d\t\t%d\t %d\t %d\n",plist->stu[n].english,plist->stu[n].math,plist->stu[n].total,plist->stu[n].rank);
    }
    }
    /**************************************5、delete*******************************************************/
    int delete_info(seqlist *plist)
    {
    char del_name[10];
    int n,m;
    printf("please input the name :");
    scanf("%s",del_name);        
    n=search_list(plist, del_name);                //调用顺序表 的查找方法,根据姓名找到位置
    m=delete_list(plist, n);                              //然后调用顺序表的删除操作,根据查找到的位置去删除
    if(m==0)                                                    //删除操作返回0,表示删除失败了。。
    {
    printf("delete student success !\n");
    }
    else
    {
    printf("can not delete student !\n");
    }
    }

    /************************************6、save***************************************************/
    int save_info(seqlist *plist)                               //文件操作还不是很顺手,花了最多时间,参考了前几天的劳动成果
    {
    FILE *fp;
    int count;
    int j;
    if((fp=fopen("student.dat","wb+"))==NULL)
    {
    printf("open file error and save error !\n");
    exit(0);
    }
    for(j=0; j<plist->length; j++)
    {
    if(fwrite(plist,sizeof(student_t),1,fp)!=1)
    {
    printf("write error\n");
    }
    }
    printf("save success!\n");
    fclose(fp);
    }

    /***************************************7、load********************************************************/
    int load_info(seqlist *plist)
    {
    student_t stu; //不能定义成指针,因为这是用来存放读取到的学生的全部信息,所以要用这块内存,当然用指针指向一块内存也行,以后试试
    FILE *fp;
    int j;
    printf("num name sex english math total rank\n");
    if((fp=fopen("student.dat","rb+"))==NULL)
    {
    printf("open file error !\n");
    }
    for(j=0; j<plist->length; j++)
    {
    fread(&stu,sizeof(student_t),1,fp);
    printf("%d\t%s\t %s\t ",stu.num,stu.name,stu.sex);
    printf("%d\t\t%d\t %d\t %d\n",stu.english,stu.math,stu.total,stu.rank);
    }
    /*
    while(!feof(fp))
    {
    fread(stu, sizeof(student_t), 1, fp);
    printf("%d\t%s\t %s\t ",stu->num,stu->name,stu->sex);
    printf("%d\t\t%d\t %d\t %d\n",stu->english,stu->math,stu->total,stu->rank);
    }*/
    fclose(fp);
    }
    int main(int argc, char* argv[])
    {
    int sele;
    student_t stu;
    seqlist *list = init_list();
    while(1)
    {
    //界面
    printf("*****************************************************************************\n");
    printf("\t\tstudent score manager syster\n");
    printf("*****************************************************************************\n");
    printf("1\tinput student score info\n");
    printf("2\tdisplay student score info\n");
    printf("3\tsort student score info\n");
    printf("4\tfind student score info\n");
    printf("5\tdelete student score info\n");
    printf("6\tsave student score info\n");
    printf("7\tload student score info\n");
    printf("8\texit\n");
    printf("\n");
    printf("please input your selection : ");

    scanf("%d",&sele);  //输入选择操作,实现相关功能
    switch(sele)
    {
    case 1:
    inputinfo(list);
    break;
    case 2:
    display_info(list);
    break;
    case 3:
    sort_info(list);
    break;
    case 4:
    search_info(list);
    break;
    case 5:
    delete_info(list);
    break;
    case 6:
    save_info(list);
    break;
    case 7:
    load_info(list);      //就是这里了,忘了在这里调用功能函数,坑到十一点二十,晚上回去楼道还是挺阴森的!!
    break;
    case 8:
    return 0;
    default:
    return 0;
    }
    }
    }

    ---恢复内容结束---

      修改后的load函数,完善了加载后可以在其他模块也可以操作链表,使用了全局变量代替局部变量

    int load_info(seqlist *plist)
    {
    student_t stu; //不能定义成指针
    FILE *fp;
    int i = 0; //stu的下标,必须初始化为0,表示第一个节点,否则会有段错误
    int j;
    int k; //判断标志

    printf("num name sex english math total rank\n");

    if((fp=fopen("student.dat","rb+"))==NULL)
    {
    printf("open file error !\n");
    }
    printf("%d\n",plist->length);

    /*
    while(!feof(fp))
    {
    i = fread(&stu, sizeof(student_t), 1, fp);
    if(i==0) //如果没有读到内容,就跳出循环,否则就插入了一个空节点
    break;
    printf("%d\t%s\t %s\t ",stu.num,stu.name,stu.sex);
    printf("%d\t\t%d\t %d\t %d\n",stu.english,stu.math,stu.total,stu.rank);

    }
    */

    while(feof(fp) == 0)
    {
    k = fread(&plist->stu[i], sizeof(student_t), 1, fp);
    if(k==0) //如果没有读到内容,就跳出循环,否则就插入了一个空节点
    break;

    plist->length++;
    i++;
    }

    fclose(fp);

    //printf("plist length = %d\n",plist->length);
    // printf("%d\n",i);

    for(j=0; j<plist->length; j++)
    {
    printf("load success!\n");
    //memset(&pplist->stu[j], 0, sizeof(student_t));
    printf("%d\t%s\t %s\t ",plist->stu[j].num,plist->stu[j].name,plist->stu[j].sex);
    printf("%d\t\t%d\t %d\t %d\n",plist->stu[j].english,plist->stu[j].math,plist->stu[j].total,plist->stu[j].rank);
    }
    return 0;
    }

  • 相关阅读:
    vue环境搭建
    'vue' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    文件上传
    json字符串解析为java对象
    自定义登录控制类Demo
    分页后台代码Demo
    主键回显
    angularjs变量的三种表示方式
    js往后台传参的方式
    同一路径下jsp能访问到,html不能访问到
  • 原文地址:https://www.cnblogs.com/ygy1784717631/p/4741363.html
Copyright © 2011-2022 走看看