C/C++知识点之c语言 文件写入和读取
小标 2018-09-19 来源 : 阅读 1319 评论 0

摘要:本文主要向大家介绍了C/C++知识点之c语言 文件写入和读取,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之c语言 文件写入和读取,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。        

#include
#include
#include
#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("\n  按, 进入菜单:  ");
    scanf("%*c");                            /* 暂停 */
    while(1)
    { 
        code=menu();                          /* 调用主控菜单 */
        switch(code)
        { 
            case 0:  exit(1);
            case 1:  printsi(stud,n); 
                     printf("\n  按, 进入菜单:  ");
                     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("            菜   单\n");
    printf("    *****************************************************\n");
    printf("      0.  退出                  1.  显示学生信息 \n");
    printf("      2.  按总分排序            3.  按学号排序\n");
    printf("      4.  统计各门功课平均分    5.  统计男女同学人数\n");
    printf("      6.  按姓名排序            7.  数学考试成绩优秀人数\n");
    printf("      8.  考试成绩优秀人数    \n");
    printf("    *****************************************************\n");
    printf("     请按序号选择:\n");
    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!\n");     
        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  总分\n");
    printf("******************************************************\n");
    for(i=0;i<n;i++)
    { 
        printf("%-8s %-8s %-2s  %4d %4d %4d %4d\n", 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",n);
    printf(" 数学   英语   C   总分\n");
    printf("%5.2f %5.2f %5.2f %5.2f\n",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",n);
    printf("     其中男同学有%d个,女同学有%d个\n",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\n", 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\n",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\n", 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\n",num);
}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 1
看完这篇文章有何感觉?已经有2人表态,50%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程