zoukankan      html  css  js  c++  java
  • c语言 文件写入和读取

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define N 10
    struct student{               /* 学生信息结构 */
        char no[9];              /* 学号         */
        char name[10];           /* 姓名         */
        char sex[3];             /* 性别         */
        int score[4];             /* 成绩和总分   */
    };
    
    int menu();                                /* 菜单*/
    void readsi(struct student stud[],int *n);  /* 读取数据*/
    void printsi(struct student *pstud,int n); /* 输出文件内容*/
    void ssort(struct student *pstud, int n);  /*按总分的降序排序函数 */
    void xsort(struct student *pstud, int n);/*按学号的升序排序函数 */
    void tjave(struct student *pstud, int n);/*统计各门功课的平均分函数 */
    void tjrs(struct student *pstud,int n);/*统计男女同学的人数函数 */
    void namesort(struct student *pstud,int n);/*按姓名升序排序 */
    void math_excellent(struct student *pstud,int n);/*数学考试成绩优秀(>=90)*/
    void all_excellent(struct student *pstud,int n);/*每门考试成绩优秀(>=90)或者总分》=270*/
    
    void main()                               /* 主函数 */
    { 
        struct student stud[N];
        int code,  n=0;
        readsi(stud,&n);
        printf("
      按<Enter>, 进入菜单:  ");
        scanf("%*c");                            /* 暂停 */
        while(1)
        { 
            code=menu();                          /* 调用主控菜单 */
            switch(code)
            { 
                case 0:  exit(1);
                case 1:  printsi(stud,n); 
                         printf("
      按<Enter>, 进入菜单:  ");
                         scanf("%*c");   
                         break;
                case 2:  ssort(stud,n);break;                    
                case 3:  xsort(stud,n);break;
                case 4:  tjave(stud,n);break;
                case 5:  tjrs(stud,n);break;
                case 6:  namesort(stud,n);break;
                case 7:  math_excellent(stud,n);break;
                case 8:  all_excellent(stud,n);break;
                
             }   scanf("%*c");
        }  
    }
    
    int menu()             /* 主控菜单函数 */
    { 
        int code;
        printf("            菜   单
    ");
        printf("    *****************************************************
    ");
        printf("      0.  退出                  1.  显示学生信息 
    ");
        printf("      2.  按总分排序            3.  按学号排序
    ");
        printf("      4.  统计各门功课平均分    5.  统计男女同学人数
    ");
        printf("      6.  按姓名排序            7.  数学考试成绩优秀人数
    ");
        printf("      8.  考试成绩优秀人数    
    ");
        printf("    *****************************************************
    ");
        printf("     请按序号选择:
    ");
        scanf("%d",&code);
        return code;
    } 
    
    
    void readsi(struct student stud[],int *n)    /* 读数据函数 */  //int *n;n需要返回        
    {
        FILE *fp;
        int i;
    //    if((fp=fopen("studf.txt","r"))==NULL)
        if((fp=fopen("C:/Users/minmin/Desktop/studf.txt","r"))==NULL)//文件存放在指定路径,把路径写上就可以了
        {
            printf("Cannot open file!
    ");     
            exit(1);     
        }
        for(i=0;!feof(fp);i++)
        { 
            (*n)++;
            fscanf(fp,"%s %s %s %d %d %d %d",    stud[i].no,stud[i].name,stud[i].sex,
            &stud[i].score[0],  &stud[i].score[1],  &stud[i].score[2],  &stud[i].score[3]);
        
            stud[i].score[3]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
        }     
        fclose(fp);   
    }
    
    
    void printsi(struct student *pstud, int n)         /* 输出数据函数 */               
    {  
        int i;
        printf(" 学号    姓名  性别   数学  英语  C  总分
    ");
        printf("******************************************************
    ");
        for(i=0;i<n;i++)
        { 
            printf("%-8s %-8s %-2s  %4d %4d %4d %4d
    ", pstud[i].no,pstud[i].name,pstud[i].sex,
            pstud[i].score[0],  pstud[i].score[1],  pstud[i].score[2],  pstud[i].score[3]);
        }  
    }
    
    
    void ssort(struct student *pstud,int n)       /*按总分的降序排序函数 */
    { 
        struct student temp;
        int i,j,min;
        for(i=0;i<n;i++)
        {
            min=i;                             /* 找最小值元素的下标*/
            for(j=i+1;j<n;j++)
                if(pstud[j].score[3]>pstud[min].score[3]) min=j;
            if(min!=i)                          /* 交换 */
            { 
                temp=pstud[i];  pstud[i]=pstud[min];   pstud[min]=temp;
            } 
        }  
    }
    
    void xsort(struct student *pstud,int n)     /*按学号的升序排序函数 */
    { 
        struct student temp;
        int  i, j;
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(strcmp(pstud[i].no,pstud[j].no)>0)
                {
                    temp=pstud[i];   
                    pstud[i]=pstud[j];   
                    pstud[j]=temp;
                } 
            }
        } 
    }
    
    
    void tjave(struct student *pstud,  int n)     /*统计各门功课的平均分函数 */
    { 
        float avemath=0,aveeng=0,avec=0,avesum=0;
        int i;
        for(i=0;i<n;i++)
        { 
            avemath+=pstud[i].score[0];
            aveeng+=pstud[i].score[1];
            avec+=pstud[i].score[2];
            avesum+=pstud[i].score[3];
        }
        avemath/=n; aveeng/=n; avec/=n; avesum/=n;
        printf("共有%d个同学,各门功课及总分的平均分为:
    ",n);
        printf(" 数学   英语   C   总分
    ");
        printf("%5.2f %5.2f %5.2f %5.2f
    ",avemath,aveeng,avec,avesum);
    }
    
    
    void tjrs(struct student *pstud,int n)        /*统计男女同学的人数函数 */
    { 
        int i, nummen=0, numwomen=0;
        for(i=0;i<n;i++)
        {
            if(strcmp(pstud[i].sex,"")==0) nummen++;
            else numwomen++;
        }
        printf("     共有%d个同学: 
    ",n);
        printf("     其中男同学有%d个,女同学有%d个
    ",nummen,numwomen);
    }
    
    void namesort(struct student *pstud,int n)/*按姓名升序排序 */
    {
        struct student temp;
        int  i, j;
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(strcmp(pstud[i].name,pstud[j].name)>0)
                {
                    temp=pstud[i];   
                    pstud[i]=pstud[j];   
                    pstud[j]=temp;
                } 
            }
        } 
    }
    
    void math_excellent(struct student *pstud,int n)/*数学考试成绩优秀(>=90)*/
    {
        int  i, num = 0;
        for(i=0;i<n;i++)
        {
            if(pstud[i].score[0]>=90)
            {
                num++;
                printf("%-8s %-8s %-2s  %4d %4d %4d %4d
    ", pstud[i].no,pstud[i].name,pstud[i].sex,
                pstud[i].score[0],  pstud[i].score[1],  pstud[i].score[2],  pstud[i].score[3]);
            }
        } 
        printf("数学优秀的人数为:%d
    ",num);
    }
    
    void all_excellent(struct student *pstud,int n)/*每门考试成绩优秀(>=90)或者总分》=270*/
    {
        int  i, num = 0;
        for(i=0;i<n;i++)
        {
            if(((pstud[i].score[0]>=90)&&(pstud[i].score[1]>=90)&&(pstud[i].score[2]>=90))||(pstud[i].score[3]>=270))
            {
                num++;
                printf("%-8s %-8s %-2s  %4d %4d %4d %4d
    ", pstud[i].no,pstud[i].name,pstud[i].sex,
                pstud[i].score[0],  pstud[i].score[1],  pstud[i].score[2],  pstud[i].score[3]);
            }
        } 
        printf("优秀的人数为:%d
    ",num);
    }
  • 相关阅读:
    json参数http post请求
    获取文本的节点数据
    mongodb robo3t 查询所有 更改固定的50一页
    mongdb 更新字段类型
    数据库表的统计表更新 解决Sql Timeout 时间已到的问题
    html背景图圆角图片设置方法
    abp.vnext vue 跨域设置
    Springboot结合ESAPI——配置XSS过滤
    centos docker安装rabbitmq
    JAVA byte[]转String 中文问题
  • 原文地址:https://www.cnblogs.com/minmsy/p/6209336.html
Copyright © 2011-2022 走看看