码迷,mamicode.com
首页 > 编程语言 > 详细

c语言 文件写入和读取

时间:2016-12-21 23:36:00      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:core   lib   blog   struct   null   pst   交换   class   min   

#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("\n  按<Enter>, 进入菜单:  ");
    scanf("%*c");                            /* 暂停 */
    while(1)
    { 
        code=menu();                          /* 调用主控菜单 */
        switch(code)
        { 
            case 0:  exit(1);
            case 1:  printsi(stud,n); 
                     printf("\n  按<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("            菜   单\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语言 文件写入和读取

标签:core   lib   blog   struct   null   pst   交换   class   min   

原文地址:http://www.cnblogs.com/minmsy/p/6209336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!