zoukankan      html  css  js  c++  java
  • C语言 学生管理系统

    //学生成绩管理系统
    //用户名:enjoy65
    //密码:  enjoy65
    
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    //定义全局变量x,i,j,n
    char x;      
    int i,j, n;
    
    struct student   //定义结构体
    {
        int id ;
        char name[20];
        float Math;
        float English;
        float Chinese;
        float average;
    }stu[5];
    
    
    
    /*----------------------------------
    
    
    
    
    
    ------------------------------------*/
    void my_add()     //获取数据 并输出,最后退出
    {
        FILE *fp;   //文件指针
        char t;
        int i;
        if((fp = fopen("stu_dat","w")) == NULL)
        {
            printf("cannot open file 
    ");
            return ;
        }
        for(i=0; i<5; i++)        //输入数据
        {
            printf("Please enter %d student ID:", i+1);
            scanf("%d",&stu[i].id);
            printf("Please enter %d student name:", i+1);
            scanf("%s",stu[i].name);
            printf("Please enter %d student Math score:", i+1);
            scanf("%f",&stu[i].Math);
            printf("Please enter %d student English score:", i+1);
            scanf("%f",&stu[i].English);
            printf("Please enter %d student Chinese score:", i+1);
            scanf("%f",&stu[i].Chinese);
            stu[i].average = (stu[i].Math + stu[i].English + stu[i].Chinese)/3;
            printf("
    ");
            fwrite(&stu[i], sizeof(struct student), 1, fp);   //将stu的数据写入文件中保存
    
        }
    
        fclose(fp);  //关闭文件
            system("cls");  //清屏
        printf("num	ID	name	Math	English	  Chinese	average
    ");
    
        for(i=0 ; i<5; i++)
        {
            printf("%d	%d	%s	%.2f	%.2f	  %.2f		%.2f
    ",
                    i+1 ,stu[i].id,stu[i].name,stu[i].Math,stu[i].English,
                    stu[i].Chinese,stu[i].average);
            printf("
    ");
        }         //按格式显示所输入数据
        printf("Press any key to exit : ");
        getchar();   
        scanf("%c",&x);  
    }
    
    
    /*----------------------------------
    
    
    
    ------------------------------------*/
    void my_View()          //将所得数据按平均分进行排序  从大到小 并输出排序后的数据,最后退出     
    {   char x;
        struct student temp;
        FILE *fp;
        if((fp = fopen("stu_dat","w")) == NULL)
        {
            printf("cannot open file
    ");
            return;
        }
        fread(stu, sizeof(struct student), 5, fp);       //读取硬盘文件中数据到内存
    
        for(i=0; i<4; i++)
            for(j=0; j<4-i; j++)          //进行冒泡排序   小的在最右边   从大到小  
                if(stu[j].average<stu[j+1].average)
                {
                    temp = stu[j];
                    stu[j] = stu[j+1];
                    stu[j+1] = temp;
                }
        fwrite(stu, sizeof(struct student), 5, fp);               //将更改后的数据写入硬盘文件
            fclose(fp);  //关闭文件
            system("cls");  //清空屏幕
        printf("num	ID	name	Math	English	Chinese		Ave
    ");               //按格式显示修改之后的数据
        for(i=0; i<5; i++)
        {
            printf("%d	%d	%s	%.2f	%.2f	%.2f		%.2f
    ",
                    i+1 ,stu[i].id,stu[i].name,stu[i].Math,
                    stu[i].English,stu[i].Chinese,stu[i].average);
        }
        printf("
    ");
        printf("press any key to exit 
    ");
        getchar();
        scanf("%c",&x);
    }
    
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    void average(struct student mt[5])      //求得并输出数学、英语、语文的成绩平均值,最后退出
    {
        int i;
        char x;
        float sum1 = 0, sum2 = 0, sum3 = 0;
        float ave1, ave2, ave3;
        for(i=0; i<5; i++)
        {
            sum1 = sum1 + mt[i].Math;
            sum2 = sum2 + mt[i].English;
            sum3 = sum3 + mt[i].Chinese;
        }    //分别求数学、英语、语文的成绩总和
        ave1 = sum1 / 5;
        ave2 = sum2 / 5;
        ave3 = sum3 / 5;         //分别求数学、英语、语文的成绩平均值
        printf("Average score of the class:
    ");
        printf("Math     : %.2f
    ", ave1);
        printf("English  : %.2f
    ", ave2);
        printf("Chinese  : %.2f
    ", ave3);
        printf("
    ");     //分别输出数学、英语、语文的成绩平均值
        printf("Press any key to exit : ");
        getchar();
        scanf("%c",&x);
    }
    
    
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    void my_modify_function( int n )  //    只对某位学号为n的学生的基本数据进行修改,并输出
                                      //修改后的总数据,如没有学号为n的学生则报错,最后退出
    { 
        FILE *fp;
        FILE *ofp;
        if((fp = fopen("stu_dat","w+")) == NULL)   
        {
            printf("cannot open file 
    ");
            return ;
        }
        fread(stu, sizeof(struct student), 5, fp);    //读取文件中的数据
        for(i=0; i<5; i++)
        {
            if(n == stu[i].id)  // 找到某位学号为n的学生 并对其数据进行修改
            {
    
                printf("Please enter  student name:");    //修改的数据包括:名字,数学、英语、语文成绩,三科平均分
                scanf("%s",stu[i].name);
                printf("Please enter student Math score:");
                scanf("%f",&stu[i].Math);
                printf("Please enter student English score:");
                scanf("%f",&stu[i].English);
                printf("Please enter student Chinese score:");
                scanf("%f",&stu[i].Chinese);
                stu[i].average = (stu[i].Math + stu[i].English + stu[i].Chinese)/3;
                for(i=0; i<5; i++)
                    fwrite(&stu[i],sizeof(struct student),1,fp);   //将修改后的数据重新写入文件中
                fclose(fp);   //关闭文件
                printf("
    ");   
                system("cls");   //清屏
                printf("num	ID	name	Math	Eng	Chinese		ave
    ");    //按指定格式输出总数据
                for(i=0 ; i<5; i++)
                    printf("%d	%d	%s	%.2f	%.2f	%.2f		%.2f
    ",
                             i ,stu[i].id,stu[i].name,stu[i].Math,stu[i].English,stu[i].Chinese,stu[i].average);
                printf("
    ");
                printf("Press any key to exit : ");   //按任意键退出
                getchar();
                scanf("%c",&x);
                return;
            }
        }
        fclose(fp);   //如果没有学号为n的学生   则关闭文件
        printf("cannot find the student ID!");   //报错
        printf("
    ");
        printf("Press any key to exit : ");   //退出
        getchar();
        scanf("%c",&x);
    }
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    void my_modefy()     //要求输入需要修改的学生学号n,并调用函数my_modify_function 进行具体修改
    {
        int n;
                while(1)
                {
                printf("Please enter you need modify student ID:
    ");
                scanf("%d",&n);
                my_modify_function(n);
                break;
                }
    }
    
    
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    void my_find()     //根据学号得到某位学生的基本数据并输出,最后退出
    {
        int fd;
        printf("Please enter the student ID : ");   //输入所求学生学号
        scanf("%d",&fd);
        system("cls");   //清屏
        for(i=0; fd != stu[i].id; i++)  ;                     //    如果i和学生学号不符合,则不进行任何操作,并让i继续累加
                                                              // 直至i与学生学号相等时结束for循环,即根据所输入学号fd找出所
                                                              // 求学生stu[i]。
        printf("num	ID	name	Math	English	   Chinese
    ");       
        printf("%d	%s	%.2f	%.2f	%.2f		%.2f
    ",           //按格式输出该学生的基本数据            
                    stu[i].id,stu[i].name,stu[i].Math,
                    stu[i].English,stu[i].Chinese,stu[i].average);
        printf("Press any key to exit : ");    //退出
        getchar();
        scanf("%c",&x);
    }
    
    
    
    
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    void my_exit(char *T)   //退出
    {
                printf("--------------------------------------
    
    
    
    
    
    
    
    
    
    
    ");
                printf("Whether out of this link Y/N : ");
                getchar();      //接收所输入字符
                scanf("%c",T);   
    }
    
    int my_doc()
    {
            system("cls");  //清空屏幕
            printf("	*******************************************************
    ");
            printf("	*******************************************************
    ");
            printf("	**                                                   **
    ");
            printf("	**                                                   **
    ");
            printf("	**             STUDENT MANAGEMENT SYSTEM             **
    ");
            printf("	**                                                   **
    ");
            printf("	**                   Please choose!                  **
    ");
            printf("	**          1 : Add five student information         **
    ");     //调用函数 my_add()
            printf("	**          2 : View all results                     **
    ");     //调用函数 my_View()
            printf("	**          3 : Average score of the class           **
    ");     //调用函数 average
            printf("	**          4 : Find student score                   **
    ");     //调用函数 my_find()
            printf("	**          5 : Modify student information           **
    ");     //调用函数 my_modefy()、my_modify_function
            printf("	**          0 : Exit                                 **
    ");     //调用函数 my_exit
            printf("	**                                                   **
    ");
            printf("	**                                                   **
    ");
            printf("	*******************************************************
    ");
            printf("	*******************************************************
    ");
            printf("
    
    
    
    
    
    
    ");
            printf("	you are choose:");
            scanf("%d",&n);     //接收选取结果
            return n;
    }
    
    /*----------------------------------
    
    
    
    
    ------------------------------------*/
    int main()
    {
        char name[20]="enjoy65",_name[20];
        char password[10]="enjoy65",_password[10];
        int f1,f2;
        int i;
        char T;
        printf("		  ******* 欢迎进入学生成绩管理系统 *******
    
    ");
        printf("		          用户请登录
    
    ");
        for(i=0;i!=5;)
        {
          printf("	  用户名:");
          gets(_name);
          f1=strcmp(_name,name);
          printf("	  密码:");
          gets(_password);
          f2=strcmp(_password,password);
          if(f1==0 && f2==0)
          {
            printf("				    登陆成功!");
            break;
          }
          else
          {
            printf("
    		     用户名或密码错误!");
            printf("您还有%d次机会
    
    ",4-i);
            i++;
          }
    
        }
    
        if(i==5)
        {printf("对不起,您的输入次数过多
    ");  return 0;}
        
        do     //先执行do语句
        {  
            switch(my_doc())      //switch语句:先调用函数my_doc()得到其返回值n,再判断
            
            {
                case 1:           //如果函数my_doc的返回值n为1,则调用函数my_add(), 并结束switch语句
                    my_add();
                    break;
                case 2:           
                    my_View();
                    break;
                case 3:
                    average(stu);
                    break;
                case 4:
                    my_find();
                    break;
                case 5:
                    my_modefy();
                    break;
                case 0:
                    my_exit(&T);     //函数的地址传递,可改变实参T的值
                    break;
                default:           //如果n与以上情况均不符合,则报错
                    printf("Error! Please input the new.
    ");
            }
        }while(T != 'y' && T != 'Y');     //    如果判断返回值n为0调用了my_exit函数,则字符T被赋初值,
                                          // 接着判断T是否为‘y’或‘Y’如果是,则不再执行do..while语句
                                          //    反之,如果n值不为0,而是1,2,3,4,5中任何一个,则在
                                          // 调用完对应函数后,while语句为真,返回继续执行do语句。
        system("cls");   //清屏                  
        return 0;
    }

    这是闲来无事自己注释一个学生管理系统,个人认为主要是函数调用以及利用文件指针打开保存文件,对其中的一些易错或比较难理解的点进行了较详细的注释。与大家分享!

    但在注释过程中发现一个问题,在自定义函数中如my_add、my_view,如果去掉最后几行代码,则在主函数调用时,该函数不可正常工作,目前还不知道具体应该怎么理解。

    printf("Press any key to exit : ");
        getchar();   
        scanf("%c",&x); 
  • 相关阅读:
    ASP.NET 后台页面无法识别服务器控件ID
    HTML5 <li> <ol> <ul> 用法
    【转】船体分段测量 船舶精度管理
    【转】2000国家大地控制网
    【转】水准原点
    【转】城市CORS系统建设
    【转】我国常用的高程系统
    【转】地图的分幅与编号
    【转】大地测量系统和参考框架
    【转】国家天文大地网
  • 原文地址:https://www.cnblogs.com/enjoy6577/p/4693050.html
Copyright © 2011-2022 走看看